Skip to content

Commit b4e0690

Browse files
authored
fix: handle OpenAPI 3.0 const conversion for falsy literals (0, false, "") (#343)
1 parent 450c0ba commit b4e0690

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

packages/nestjs-zod/src/__e2e_tests__/openapi.test.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2171,6 +2171,50 @@ describe('issue#220', () => {
21712171
})
21722172
})
21732173

2174+
describe('issue#342', () => {
2175+
it('should convert const to enum for falsy values like 0', async () => {
2176+
2177+
const LiteralZeroFieldSchema = z.object({
2178+
price: z.string().nullable(),
2179+
paymentFrequency: z.union([
2180+
z.literal(0),
2181+
z.literal(3),
2182+
z.literal(12),
2183+
z.literal(36),
2184+
]),
2185+
emptyString: z.literal(''),
2186+
falseBoolean: z.literal(false),
2187+
});
2188+
2189+
2190+
class NullableFieldAndUnionFieldDto extends createZodDto(LiteralZeroFieldSchema) { }
2191+
2192+
@Controller()
2193+
class ThingController {
2194+
@Post('thing')
2195+
async thing(@Body() body: NullableFieldAndUnionFieldDto) {
2196+
return body;
2197+
}
2198+
}
2199+
2200+
const doc = await getSwaggerDoc(ThingController);
2201+
2202+
// Check that the DTO schema is referenced in the request body
2203+
expect(get(doc, 'paths./thing.post.requestBody.content.application/json.schema.$ref')).toEqual('#/components/schemas/NullableFieldAndUnionFieldDto');
2204+
2205+
// Check the schema is generated correctly with enum values for literals including falsy values
2206+
expect(get(doc, 'components.schemas.NullableFieldAndUnionFieldDto.properties.price.type')).toEqual('string');
2207+
expect(get(doc, 'components.schemas.NullableFieldAndUnionFieldDto.properties.price.nullable')).toEqual(true);
2208+
2209+
expect(get(doc, 'components.schemas.NullableFieldAndUnionFieldDto.properties.paymentFrequency.anyOf.0.type')).toEqual('number');
2210+
expect(get(doc, 'components.schemas.NullableFieldAndUnionFieldDto.properties.paymentFrequency.anyOf.0.enum')).toEqual([0]);
2211+
expect(get(doc, 'components.schemas.NullableFieldAndUnionFieldDto.properties.emptyString.enum')).toEqual(['']);
2212+
expect(get(doc, 'components.schemas.NullableFieldAndUnionFieldDto.properties.falseBoolean.enum')).toEqual([false]);
2213+
2214+
expect(JSON.stringify(doc)).not.toContain(PREFIX);
2215+
})
2216+
})
2217+
21742218
describe('issue#208', () => {
21752219
it('should use deepObject style for query parameters that are objects by default', async () => {
21762220
class QueryParamsDto extends createZodDto(z.object({

packages/nestjs-zod/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export function convertToOpenApi3Point0(schema: JSONSchema.BaseSchema) {
7171
}
7272
}
7373

74-
if (s.const) {
74+
if (typeof s.const !== "undefined") {
7575
s.enum = [s.const];
7676
delete s.const;
7777
}

0 commit comments

Comments
 (0)