Skip to content

springfox ApiModelProperty position sorting not working

In my spring boot application, I cannot be able to manage my swagger JSON with fields ordered properly on @ApiModel annotated class.

Firstly, I imported springfox lib into my pom.xml:

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

Secondly, I created SwaggerConfig.java:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket swaggerSpringMvcPlugin() {
        return new Docket(DocumentationType.SWAGGER_2)  
                  .select()                                  
                  .apis(RequestHandlerSelectors.basePackage("my.package.to.enable.swagger.doc"))           
                  .paths(PathSelectors.any())       
                  .build()
                  .host("http://localhost:8080");
    }
}

Thirdly, I created my PersonDTO annotated by @ApiModel:

@ApiModel(value = "Person", description = "Person entity definition")
public class PersonDTO {

    @ApiModelProperty(value="Entity unique ID", position=0)
    public Long id;

    @ApiModelProperty(value="Person's number, useful to provide a convenient way to quickly communicate a person's reference", position=1)
    public Integer number;
    
    @ApiModelProperty(value="Person's first name", position=2)
    public String firstName;
    
    @ApiModelProperty(value="Person's last name", position=3)
    public String lastName;
    
}

Then, when I requesting for Json output at url http://localhost:[port]/[servlet-context-path]/v2/api-docs, it seems that the position order is not working:

"definitions": {
    "Person": {
      "type": "object",
      "properties": {
        "firstName": {
          "type": "string",
          "description": "Person's first name"
        },
        "id": {
          "type": "integer",
          "format": "int64",
          "description": "Entity unique ID"
        },
        "lastName": {
          "type": "string",
          "description": "Person's last name"
        },
        "number": {
          "type": "integer",
          "format": "int32",
          "description": "Person's number, useful to provide a convenient way to quickly communicate a person's reference"
        }
      },
      "title": "Person",
      "description": "Person entity definition"
    }
}

Answer

This is currently broken in 3.0.0 see https://github.com/springfox/springfox/issues/3391