Skip to content

Commit

Permalink
Merge pull request #58 from lifeomic/inline-schemas
Browse files Browse the repository at this point in the history
fix: always inline reused schemas when introspecting
  • Loading branch information
swain authored Jan 19, 2023
2 parents add44e8 + c8792a0 commit 0e967a6
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
70 changes: 70 additions & 0 deletions src/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,76 @@ describe('introspection', () => {

expect(formattedDeclaration).toMatchSnapshot();
});

test('when using the same schema reference multiple times, it is always resolved inline', async () => {
const reusedSchema = z.object({ id: z.string() });

const router = OneSchemaRouter.create({
using: new Router(),
introspection: {
route: '/private/introspection',
serviceVersion: '123',
},
}).declare({
route: 'POST /items',
name: 'createItem',
request: z.object({}),
response: z.object({
resProp1: reusedSchema,
resProp2: reusedSchema,
}),
});

const { client } = serve(router);

const introspectionResult = await client.get('/private/introspection');

console.log(JSON.stringify(introspectionResult.data, null, 2));

expect(introspectionResult.data.schema).toStrictEqual({
Endpoints: {
'POST /items': {
Name: 'createItem',
Request: {
type: 'object',
properties: {},
additionalProperties: false,
$schema: 'http://json-schema.org/draft-07/schema#',
},
Response: {
type: 'object',
properties: {
// resProp1 and resProp2 should be inlined, even though they have identical
// schemas defined using the same reference.
resProp1: {
type: 'object',
properties: {
id: {
type: 'string',
},
},
required: ['id'],
additionalProperties: false,
},
resProp2: {
type: 'object',
properties: {
id: {
type: 'string',
},
},
required: ['id'],
additionalProperties: false,
},
},
required: ['resProp1', 'resProp2'],
additionalProperties: false,
$schema: 'http://json-schema.org/draft-07/schema#',
},
},
},
});
});
});

test('declaring multiple routes with the same name results in an error', () => {
Expand Down
8 changes: 6 additions & 2 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,13 @@ const convertRouterSchemaToJSONSchemaStyle = <Schema extends ZodSchema>(
// The JSONSchema types are very slightly different between packages. We just
// trust that the interop will work fine, and use "as any" here.
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Request: zodToJsonSchema(definition.request) as any,
Request: zodToJsonSchema(definition.request, {
$refStrategy: 'none',
}) as any,
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument
Response: zodToJsonSchema(definition.response) as any,
Response: zodToJsonSchema(definition.response, {
$refStrategy: 'none',
}) as any,
};
}

Expand Down

0 comments on commit 0e967a6

Please sign in to comment.