Skip to content

Commit 0e967a6

Browse files
author
Swain Molster
authored
Merge pull request #58 from lifeomic/inline-schemas
fix: always inline reused schemas when introspecting
2 parents add44e8 + c8792a0 commit 0e967a6

File tree

2 files changed

+76
-2
lines changed

2 files changed

+76
-2
lines changed

src/router.test.ts

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,76 @@ describe('introspection', () => {
390390

391391
expect(formattedDeclaration).toMatchSnapshot();
392392
});
393+
394+
test('when using the same schema reference multiple times, it is always resolved inline', async () => {
395+
const reusedSchema = z.object({ id: z.string() });
396+
397+
const router = OneSchemaRouter.create({
398+
using: new Router(),
399+
introspection: {
400+
route: '/private/introspection',
401+
serviceVersion: '123',
402+
},
403+
}).declare({
404+
route: 'POST /items',
405+
name: 'createItem',
406+
request: z.object({}),
407+
response: z.object({
408+
resProp1: reusedSchema,
409+
resProp2: reusedSchema,
410+
}),
411+
});
412+
413+
const { client } = serve(router);
414+
415+
const introspectionResult = await client.get('/private/introspection');
416+
417+
console.log(JSON.stringify(introspectionResult.data, null, 2));
418+
419+
expect(introspectionResult.data.schema).toStrictEqual({
420+
Endpoints: {
421+
'POST /items': {
422+
Name: 'createItem',
423+
Request: {
424+
type: 'object',
425+
properties: {},
426+
additionalProperties: false,
427+
$schema: 'http://json-schema.org/draft-07/schema#',
428+
},
429+
Response: {
430+
type: 'object',
431+
properties: {
432+
// resProp1 and resProp2 should be inlined, even though they have identical
433+
// schemas defined using the same reference.
434+
resProp1: {
435+
type: 'object',
436+
properties: {
437+
id: {
438+
type: 'string',
439+
},
440+
},
441+
required: ['id'],
442+
additionalProperties: false,
443+
},
444+
resProp2: {
445+
type: 'object',
446+
properties: {
447+
id: {
448+
type: 'string',
449+
},
450+
},
451+
required: ['id'],
452+
additionalProperties: false,
453+
},
454+
},
455+
required: ['resProp1', 'resProp2'],
456+
additionalProperties: false,
457+
$schema: 'http://json-schema.org/draft-07/schema#',
458+
},
459+
},
460+
},
461+
});
462+
});
393463
});
394464

395465
test('declaring multiple routes with the same name results in an error', () => {

src/router.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,13 @@ const convertRouterSchemaToJSONSchemaStyle = <Schema extends ZodSchema>(
127127
// The JSONSchema types are very slightly different between packages. We just
128128
// trust that the interop will work fine, and use "as any" here.
129129
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
130-
Request: zodToJsonSchema(definition.request) as any,
130+
Request: zodToJsonSchema(definition.request, {
131+
$refStrategy: 'none',
132+
}) as any,
131133
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
132-
Response: zodToJsonSchema(definition.response) as any,
134+
Response: zodToJsonSchema(definition.response, {
135+
$refStrategy: 'none',
136+
}) as any,
133137
};
134138
}
135139

0 commit comments

Comments
 (0)