Skip to content

Wrong behaviour when using mime type api versioning #965

Closed
@codependent

Description

@codependent

We are starting with our REST API versioning and after having read a lot about the different options (URI versioning, mime type versioning) have decided to use the latter approach.

We were expecting that Springfox generates the following doc:

v1:

get /api/architecture/mails - application/vnd.arch.mails.v1+json 
get /api/architecture/services - application/vnd.arch.service.v1+json

v2:

get /api/architecture/services - application/vnd.arch.service.v2+json

However, in the v2 I also get this:

get /api/architecture/services - application/vnd.arch.service.v1+json

It shouldn't be there since I configured the v2 Docklet with .produces(new HashSet<String>(Arrays.asList(new String[]{"application/vnd.arch.service.v2+json"}))) so that it filters the services according to the versioned mime type.

Here is our springfox config:

@Bean
    public Docket arqV1Api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.regex("/api/architecture/.*"))               
            .build()
            .apiInfo(new ApiInfo("Architecture Rest Api","Architecture REST Services","v1","","","",""))
            .produces(new HashSet<String>(Arrays.asList(new String[]{"application/vnd.arch.service.v1+json","application/vnd.arch.mail.v1+json"})))
            .securitySchemes(newArrayList(apiKey()))
            .securityContexts(newArrayList(securityContext()))
            .groupName("Arq v1 group");
    }
    @Bean
    public Docket arqV2Api() {
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(PathSelectors.regex("/api/architecture/.*"))               
            .build()
            .apiInfo(new ApiInfo("Architecture Rest Api","Architecture REST Services","v2","","","",""))
            .produces(new HashSet<String>(Arrays.asList(new String[]{"application/vnd.arch.service.v2+json"})))
            .securitySchemes(newArrayList(apiKey()))
            .securityContexts(newArrayList(securityContext()))
            .groupName("Arq v2 group");
    }

Spring MVC Rest Controller:

private static final String serviceArqV1MediaType = "application/vnd.arch.service.v1+json";
private static final String serviceArqV2MediaType = "application/vnd.arch.service.v2+json";
private static final String mailsArqV1MediaType   = "application/vnd.arch.mail.v1+json";

@ApiOperation(value = "Gets architecture services", 
              notes = "",
              produces = serviceArqV1MediaType)
@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Request OK"),
        @ApiResponse(code = 400, message = "Bad Request")})
@RequestMapping(value = {"/services"}, method = RequestMethod.GET, 
                produces = serviceArqV1MediaType)
public List<ServicioArquitectura> getServices() {
    return Arrays.asList(new ServiceArch[]{new ServicioArquitectura("Support"), new ServicioArquitectura("Kickoff")});
}

@ApiOperation(value = "Gets architecture services", 
          notes = "",
          produces = serviceArqV2MediaType)
@ApiResponses(value = {
      @ApiResponse(code = 200, message = "Request OK"),
      @ApiResponse(code = 400, message = "Bad Request")})
@RequestMapping(value = {"/services"}, method = RequestMethod.GET, 
                produces = {serviceArqV2MediaType})
public List<ServicioArquitecturaV2> getServicesV2() {
    return Arrays.asList(new ServiceArchV2[]{new ServiceArchV2("Support", Boolean.TRUE), new ServiceArchV2("Kickoff", Boolean.FALSE)});
}

@ApiOperation(value = "Gets mails", 
          produces = mailsArqV1MediaType)
@ApiResponses(value = {
      @ApiResponse(code = 200, message = "Request OK"),
      @ApiResponse(code = 400, message = "Bad Request")})
@RequestMapping(value = {"/mails"}, method = RequestMethod.GET, 
                produces = {mailsArqV1MediaType})
public List<String> getMails() {
    return Arrays.asList(new String[]{"xxxcompany.com"});
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions