@@ -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+
21742218describe ( 'issue#208' , ( ) => {
21752219 it ( 'should use deepObject style for query parameters that are objects by default' , async ( ) => {
21762220 class QueryParamsDto extends createZodDto ( z . object ( {
0 commit comments