Skip to content

Commit 9111358

Browse files
committed
fix(mock): include import alias in binary type rewrite
1 parent 438a14d commit 9111358

2 files changed

Lines changed: 38 additions & 3 deletions

File tree

packages/mock/src/msw/index.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,38 @@ describe('generateMSW', () => {
355355
expect(result.implementation.function).not.toContain(': TestPdfFile');
356356
});
357357

358+
it('should rewrite aliased ref imports for $ref binary schemas', () => {
359+
const aliasedVerbOptions = {
360+
...mockVerbOptions,
361+
response: {
362+
imports: [],
363+
definition: { success: '__TestPdfFile' },
364+
types: {
365+
success: [
366+
{
367+
key: '200',
368+
value: '__TestPdfFile',
369+
contentType: '*/*',
370+
originalSchema: { type: 'string', format: 'binary' },
371+
imports: [{ name: 'TestPdfFile', alias: '__TestPdfFile' }],
372+
schemas: [],
373+
type: 'string',
374+
isEnum: false,
375+
isRef: true,
376+
hasReadonlyProps: false,
377+
},
378+
],
379+
},
380+
contentTypes: ['*/*'],
381+
},
382+
} as unknown as GeneratorVerbOptions;
383+
384+
const result = generateMSW(aliasedVerbOptions, baseOptions);
385+
386+
expect(result.implementation.function).toContain(': ArrayBuffer');
387+
expect(result.implementation.function).not.toContain(': __TestPdfFile');
388+
});
389+
358390
it('should not force binary path when preferredContentType narrows to a non-binary success variant', () => {
359391
const mixedVerbOptions = {
360392
...mockVerbOptions,

packages/mock/src/msw/index.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,11 +158,14 @@ function generateDefinition(
158158
const isBinaryResponse =
159159
contentTypesByPreference.some((ct) => isBinaryLikeContentType(ct)) ||
160160
responsesByPreference.some((r) => isSchemaBinary(r));
161-
// Bare ref names of schema-binary responses (from imports, which is the canonical place).
161+
// Bare ref names of schema-binary responses (include alias for collision-renamed imports).
162162
const binaryRefNames = responsesByPreference
163163
.filter((r) => isSchemaBinary(r))
164-
.flatMap((r) => r.imports.map((imp) => imp.name))
165-
.filter(Boolean);
164+
.flatMap((r) =>
165+
r.imports.flatMap((imp) =>
166+
imp.alias ? [imp.name, imp.alias] : [imp.name],
167+
),
168+
);
166169
const isReturnHttpResponse = value && value !== 'undefined';
167170

168171
const getResponseMockFunctionName = `${getResponseMockFunctionNameBase}${pascal(

0 commit comments

Comments
 (0)