When you define something like this:
@ApiModelProperty({required: false, type: MyCustomModel})
myProperty: MyCustomModel;
the swagger will always result this property as required: true
it's only for custom models, works ok for all other types
I did quick debugging and it's simple to fix
problem is in line 101 inside api-parameters.explorer.ts file
https://github.com/nestjs/swagger/blob/master/lib/explorers/api-parameters.explorer.ts#L101
return { name: key, $ref };
should be:
return { ...metadata, name: key, $ref };
hope you can include this quick fix on next update
You should use @ApiModelPropertyOptional()
@darioxtx it's up to you, it's the same but just shorter notation, anyway @ApiModelPropertyOptional() has the same issue be course it's just a wrapper around @ApiModelProperty()
In our project we discovered the same issue and would be very happy about a fix
Thanks, fixed. I'll publish it soon. 馃敟
Thanks for looking into the issue. Unfortunately the fix doesn't work properly and Custom Object Models are still always shown as "Required" (marked with a red star) in the SwaggerUI. I checked the commits on master and apparently you changed the code to the suggestion which was provided - so maybe this suggestion isnt correct?
@ApiModelProperty({ description: 'Process Metadata', required: true,
type: ProcessData, isArray: false })
@ValidateNested()
@Type(() => ProcessData)
processData: ProcessData;
@ApiModelPropertyOptional({ description: 'Customer Data',
type: CustomerData, isArray: false })
@ValidateNested()
@Type(() => CustomerData)
customerData: CustomerData;
@ApiModelPropertyOptional({ description: 'Vehicle Data',
type: VehicleData, isArray: false })
@ValidateNested()
@Type(() => VehicleData)
vehicleData: VehicleData;
All three attributes are still shown as "required".
I confirm the issue outlined by @NiklasMencke
Can a fix be provided for this too ? Basically required is still appearing for objects, eg:
@ApiModelPropertyOptional({type: ChannelDto})
channel: ChannelDto;
Fixed in 2.1.0
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
Most helpful comment
You should use
@ApiModelPropertyOptional()