question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ApiModelProperty always sets required:true when custom type provided

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:8 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
darioxtxcommented, Feb 22, 2018

You should use @ApiModelPropertyOptional()

1reaction
NiklasMenckecommented, May 23, 2018

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”.

Read more comments on GitHub >

github_iconTop Results From Across the Web

java - Swagger not scanning ApiModel and ApiModelProperty ...
I found the solution. It turned out to be a class loading issue. I had an ear library that I was using to...
Read more >
Setting Example and Description with Swagger - Baeldung
The @ApiModelProperty annotation defines the properties of the fields. We used this annotation on each field to set its notes (description), ...
Read more >
io.swagger.annotations.ApiModelProperty Java Examples
ALWAYS ) public Boolean isBoolItem() { return boolItem; }. Example #2 ... @return uuid **/ @ApiModelProperty(required = true, value = "Object UUID.
Read more >
Spring Rest API with Swagger – Creating documentation
Tutorial on how to create API documentation using Swagger in Spring application.
Read more >
ApiModelProperty (swagger-annotations 1.5.0 API)
Limits the acceptable values for this parameter. There are three ways to describe the allowable values: To set a list of values, provide...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found