Skip to content

Commit 0cce06f

Browse files
authored
test(core): add wildcard content type tests and response coverage (orval-labs#2662)
- Test wildcard */* media type generates Blob for request/response - Add wildcard to isBinaryContentType test - Add wildcardFile field to comprehensive FormData test - Test both request and response for core type generation rules
1 parent b5eba61 commit 0cce06f

File tree

1 file changed

+76
-3
lines changed

1 file changed

+76
-3
lines changed

packages/core/src/getters/res-req-types.test.ts

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { describe, expect, it } from 'vitest';
33
import type {
44
ContextSpec,
55
OpenApiRequestBodyObject,
6+
OpenApiResponseObject,
67
OpenApiSchemaObject,
78
} from '../types';
89
import { getResReqTypes, isBinaryContentType } from './res-req-types';
@@ -67,6 +68,7 @@ describe('isBinaryContentType', () => {
6768
expect(isBinaryContentType('image/jpeg')).toBe(true);
6869
expect(isBinaryContentType('audio/mp3')).toBe(true);
6970
expect(isBinaryContentType('video/mp4')).toBe(true);
71+
expect(isBinaryContentType('*/*')).toBe(true); // Unknown type - using Blob for now since it handles both text and binary
7072
});
7173

7274
it('should return false for text-based content types', () => {
@@ -81,7 +83,7 @@ describe('isBinaryContentType', () => {
8183
describe('getResReqTypes (content type handling)', () => {
8284
describe('media key precedence (type generation)', () => {
8385
it('binary media key → Blob; text/JSON media key ignores contentMediaType', () => {
84-
// Binary media key overrides schema to Blob
86+
// Binary media key overrides schema to Blob (request)
8587
const binaryReq: [string, OpenApiRequestBodyObject][] = [
8688
[
8789
'requestBody',
@@ -95,7 +97,22 @@ describe('getResReqTypes (content type handling)', () => {
9597
];
9698
expect(getResReqTypes(binaryReq, 'Body', context)[0].value).toBe('Blob');
9799

98-
// Text/JSON media key ignores contentMediaType (stays string)
100+
// Binary media key overrides schema to Blob (response)
101+
const binaryRes: [string, OpenApiResponseObject][] = [
102+
[
103+
'200',
104+
{
105+
content: {
106+
'application/octet-stream': { schema: { type: 'string' } },
107+
},
108+
},
109+
],
110+
];
111+
expect(getResReqTypes(binaryRes, 'Response', context)[0].value).toBe(
112+
'Blob',
113+
);
114+
115+
// Text/JSON media key ignores contentMediaType (request)
99116
const jsonReq: [string, OpenApiRequestBodyObject][] = [
100117
[
101118
'requestBody',
@@ -110,6 +127,58 @@ describe('getResReqTypes (content type handling)', () => {
110127
],
111128
];
112129
expect(getResReqTypes(jsonReq, 'Body', context)[0].value).toBe('string');
130+
131+
// Text/JSON media key ignores contentMediaType (response)
132+
const jsonRes: [string, OpenApiResponseObject][] = [
133+
[
134+
'200',
135+
{
136+
content: {
137+
'application/json': {
138+
schema: { type: 'string', contentMediaType: 'image/png' },
139+
},
140+
},
141+
},
142+
],
143+
];
144+
expect(getResReqTypes(jsonRes, 'Response', context)[0].value).toBe(
145+
'string',
146+
);
147+
});
148+
149+
it('wildcard */* media → Blob for both request and response', () => {
150+
// Wildcard accepts any content type, generates Blob
151+
const reqWithCmt: [string, OpenApiRequestBodyObject][] = [
152+
[
153+
'requestBody',
154+
{
155+
content: {
156+
'*/*': {
157+
schema: { type: 'string', contentMediaType: '*/*' },
158+
},
159+
},
160+
required: true,
161+
},
162+
],
163+
];
164+
expect(getResReqTypes(reqWithCmt, 'Body', context)[0].value).toBe('Blob');
165+
166+
// Response
167+
const resWithCmt: [string, OpenApiResponseObject][] = [
168+
[
169+
'200',
170+
{
171+
content: {
172+
'*/*': {
173+
schema: { type: 'string', contentMediaType: '*/*' },
174+
},
175+
},
176+
},
177+
],
178+
];
179+
expect(getResReqTypes(resWithCmt, 'Response', context)[0].value).toBe(
180+
'Blob',
181+
);
113182
});
114183
});
115184

@@ -145,6 +214,7 @@ describe('getResReqTypes (content type handling)', () => {
145214
type: 'object',
146215
properties: { name: { type: 'string' } },
147216
},
217+
wildcardFile: { type: 'string', contentMediaType: '*/*' },
148218
},
149219
required: [
150220
'encBinary',
@@ -155,6 +225,7 @@ describe('getResReqTypes (content type handling)', () => {
155225
'formatBinary',
156226
'base64Field',
157227
'metadata',
228+
'wildcardFile',
158229
],
159230
},
160231
encoding: {
@@ -173,7 +244,7 @@ describe('getResReqTypes (content type handling)', () => {
173244
it('generates correct types and FormData for all content type combinations', () => {
174245
const result = getResReqTypes(reqBody, 'Body', context)[0];
175246

176-
// encBinary/cmtBinary/formatBinary: Blob (binary)
247+
// encBinary/cmtBinary/formatBinary/wildcardFile: Blob (binary)
177248
// encText/cmtText/encOverride: Blob | string (text file)
178249
// base64Field: string (contentEncoding means not a file)
179250
// metadata: object (named type)
@@ -186,6 +257,7 @@ describe('getResReqTypes (content type handling)', () => {
186257
formatBinary: Blob;
187258
base64Field: string;
188259
metadata: BodyRequestBodyMetadata;
260+
wildcardFile: Blob;
189261
}`);
190262

191263
expect(result.formData).toBe(`const formData = new FormData();
@@ -197,6 +269,7 @@ formData.append(\`encOverride\`, bodyRequestBody.encOverride instanceof Blob ? b
197269
formData.append(\`formatBinary\`, bodyRequestBody.formatBinary);
198270
formData.append(\`base64Field\`, bodyRequestBody.base64Field);
199271
formData.append(\`metadata\`, new Blob([JSON.stringify(bodyRequestBody.metadata)], { type: 'application/json' }));
272+
formData.append(\`wildcardFile\`, bodyRequestBody.wildcardFile);
200273
`);
201274
});
202275
});

0 commit comments

Comments
 (0)