Search code examples
javaspring-bootswaggerswagger-uispringfox

Why does @ApiModelProperty "name" attribute has no effect?


In my spring boot app, I have a DTO object with a nested list of DTO objects. class:

@Getter
@Setter
@Builder
@NoArgsConstructor
@AllArgsConstructor
@ApiModel(value = "contact")
public class ContactDTO {
  @ApiModelProperty(value = "id", example = "1", hidden = true)
  private Long id;

  @ApiModelProperty(value = "first name", example = "John")
  private String firstName;

  @ApiModelProperty(value = "last name", example = "Doe")
  private String lastName;

  @Builder.Default
  @ApiModelProperty(value = "list of phone numbers", name = "phonenumbers")
  List<PhoneNumberDTO> phoneNumberDTOList = new ArrayList<>();
}

the swagger example value for a post request:

{
  "firstName": "John",
  "lastName": "Doe",
  "phoneNumberDTOList": [
    {
      "label": "Company",
      "number": "put number here"
    }
  ]
}


I thought the name = ... property in @ApiModelProperty overwrites the variable name phoneNumberDTOList, but that doesn't work :(

I use springfox-swagger 2.9.2

  implementation 'io.springfox:springfox-swagger2:2.9.2'
  implementation 'io.springfox:springfox-swagger-ui:2.9.2'


What am I doing wrong?


Solution

  • Please check out this issue:

    @ApiModelProperty "name" attribute has no effect

    • We don't want ever to have a case where the serialization model is different from whats being documented.

    • Actually the existence of @ApiModelProperty is explained by the fact that we want to use the same annotations that swagger-core uses. However we take the philosophy of using the annotations just to supplement documentation. If for e.g. you've annotated your models with @JsonProperty etc we don't want to duplicate that using @ApiModelProperty as it is very easy to get out of sync.

    There is a workaround with @JsonProperty annotation:

    ...
    
    @JsonProperty("phonenumbers")
    @ApiModelProperty(value = "list of phone numbers")
    List<PhoneNumberDTO> phoneNumberDTOList = new ArrayList<>();