Forum Discussion

PeterP's avatar
PeterP
Occasional Visitor
6 years ago

swagger-codegen-maven-plugin ignores multiple occurences of a path (with different content types)

Imagine you would like to return data either in JSON or CSV format based on the provided "accepts" header parameter. In Spring you can implement 2 different methods with almost the same @GetMapping annotation... and it would work.

@GetMapping(value = "/data", produces = {"application/JSON"})
public ResponseEntity<SomeDtoWithJsonAnnotations> getDataAsJson() {...}

@GetMapping(value = "/data", produces = {"text/csv"})
public ResponseEntity<byte[]> getDataAsCsv() {...}

If you try to achieve smtg similar in YAML and then to generate Spring source code (swagger-codegen-maven-plugin, language: spring, version: 2.4.6):

/data:  
get: operationId: getDataAsJson consumes: - application/json; charset=utf-8 produces: - application/json; charset=utf-8 responses: 200: schema: $ref: "#/definitions/DataResponse" /data: get: operationId: getDataAsCsv consumes: - application/json; charset=utf-8 produces: - text/csv responses: 200: schema: type: string format: byte


The first definition (operationId = 'getDataAsJson") is ignored and only the second definition (operationId = "getDataAsCsv") is generated.
On console there is no error, maven build is successful.
Is it a defect?
I would at least expect a build failure saying the path is being used twice. However, a total ignorance of the first definition is a surprise to me.
What do you think, guys?


  • The same problem occurs, if you specify multiple content types to a path. Separate service methods will be generated but all of them will have all content types in their Consumes annotations instead of just the matching ones. This gets even worse, if you try to use content type versioning.

     

    For example you want to allow for two content type versions like:

     

        content:
          application/json;version=1.0.0:
            schema:
              $ref: '#/components/schemas/MyDataV1'
          application/json;version=2.0.0:
            schema:
              $ref: '#/components/schemas/MyDataV2'

     

    You will end up with two methods generated (one for each content type) but both will have same consumes annotation like:

     

     

    @Consumes({ "application/json;version=1.0.0", "application/json;version=2.0.0" })
    ...
    public void myOperation(MyDataV1 body);
    
    @Consumes({ "application/json;version=1.0.0", "application/json;version=2.0.0" })
    ...
    public void myOperation(MyDataV2 body);

     

    While I would actually expect:

     

    @Consumes({ "application/json;version=1.0.0" })
    ...
    public void myOperation(MyDataV1 body);
    
    @Consumes({ "application/json;version=2.0.0" })
    ...
    public void myOperation(MyDataV2 body);

     

    PeterP Did you manage to solve this?

     

    My last resort would be mangling with the code generation templates. But to be honest I would like to avoid that.