Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Duplicate description on schemas used in parameters #200

Merged
merged 2 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions src/create/parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,43 @@ describe('createParametersObject', () => {

expect(result).toStrictEqual(expectedResult);
});

it('should extract the description from the underlying schema', () => {
const expectedResult: oas31.BaseParameterObject = {
schema: {
type: 'string',
description: 'foo',
},
description: 'foo',
required: true,
};
const result = createBaseParameter(
z.string().describe('foo'),
getDefaultComponents(),
['query'],
);

expect(result).toStrictEqual(expectedResult);
});

it('should allow overriding the description using .openapi.param in the underlying schema', () => {
const expectedResult: oas31.BaseParameterObject = {
schema: {
type: 'string',
description: 'foo',
},
description: 'boo',
required: true,
};
const result = createBaseParameter(
z
.string()
.describe('foo')
.openapi({ param: { description: 'boo' } }),
getDefaultComponents(),
['query'],
);

expect(result).toStrictEqual(expectedResult);
});
});
4 changes: 4 additions & 0 deletions src/create/parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ export const createBaseParameter = (
'schema',
]);
const required = !isOptionalSchema(schema, state);
const description =
schema._def.openapi?.description ?? schema._def.description;

return {
...(description && { description }),
...rest,
...(schema && { schema: schemaObject }),
...(required && { required }),
Expand Down