swagger-codegen icon indicating copy to clipboard operation
swagger-codegen copied to clipboard

[Java-Spring] Incorrect annotation generated when response contains multiple media types

Open jose-hernandez-001 opened this issue 8 years ago • 2 comments

Description

Swagger codegen generates an incorrect 'produces' annotation when the response contains multiple media types (https://swagger.io/docs/specification/describing-responses/). More specifically, the comma meant to be separating the different media types is missing from the the generated code.

produces = { "application/vnd.iop.content.format.asset+xml""application/vnd.iop.content.format.meta+xml" },
Swagger-codegen version

3.0.0-SNAPSHOT built from source

Swagger declaration file content or url
      responses:
        '200':
          description: Asset was found and its information is being returned.
          content:
            application/vnd.iop.content.format.asset+xml:
              schema:
                type: string
                example: |
                  TODO: add an example of asset data
            application/vnd.iop.content.format.meta+xml:
              schema:
                type: string
                example: |
                  TODO: add an example of asset metadata
        '400':
          description: Bad request. I.e. one or more of the required request parameters are absent.
        '404':
          description: An asset with the given ID could not be found.
Command line used for generation

The code was generated with Maven

Steps to reproduce

Generate the code using the 'spring' language option.

<language>spring</language>
<configOptions>
	<sourceFolder>src/main/java</sourceFolder>
	<modelPackage>${defaultPackage}.model</modelPackage>
	<apiPackage>${defaultPackage}.api</apiPackage>
	<invokerPackage>${defaultPackage}</invokerPackage>
	<configPackage>${defaultPackage}.configuration</configPackage>
	<delegatePattern>true</delegatePattern>
	<java8>true</java8>
	<dateLibrary>java</dateLibrary>
</configOptions>
Related issues/PRs

No

Suggest a fix/enhancement

Modify the template to add a command in between the different response media types. e.g.

produces = { "application/vnd.iop.content.format.asset+xml", "application/vnd.iop.content.format.meta+xml" },

or generate an error message to indicate that the singleContentTypes option is set to false but multiple media types were encountered.

jose-hernandez-001 avatar Jul 27 '18 08:07 jose-hernandez-001

The following patch for swagger-codegen-generators fixes the problem for now though it adds a spurious comma to the generated code:

diff --git a/src/main/resources/v2/JavaSpring/api.mustache b/src/main/resources/v2/JavaSpring/api.mustache
index bd444d2..d153a6a 100644
--- a/src/main/resources/v2/JavaSpring/api.mustache
+++ b/src/main/resources/v2/JavaSpring/api.mustache
@@ -97,8 +97,8 @@ public interface {{classname}} {
     @RequestMapping(value = "{{{path}}}",{{#if singleContentTypes}}{{#has this 'produces'}}
         produces = "{{{vendorExtensions.x-accepts}}}", {{/has}}{{#has this 'consumes'}}
         consumes = "{{{vendorExtensions.x-contentType}}}",{{/has}}{{/if}}{{#unless singleContentTypes}}{{#has this 'produces'}}
-        produces = { {{#produces}}"{{{mediaType}}}"{{#has ../this 'more'}}, {{/has}}{{/produces}} }, {{/has}}{{#has this 'consumes'}}
-        consumes = { {{#consumes}}"{{{mediaType}}}"{{#has ../this 'more'}}, {{/has}}{{/consumes}} },{{/has}}{{/unless}}
+        produces = { {{#produces}}"{{{mediaType}}}",{{#has ../this 'more'}}, {{/has}}{{/produces}} }, {{/has}}{{#has this 'consumes'}}
+        consumes = { {{#consumes}}"{{{mediaType}}}",{{#has ../this 'more'}}, {{/has}}{{/consumes}} },{{/has}}{{/unless}}
         method = RequestMethod.{{httpMethod}})
     {{#jdk8}}default {{/jdk8}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}ResponseEntity<{{>returnTypes}}>{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}({{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{#has this 'more'}},{{/has}}{{/allParams}}){{^jdk8}};{{/jdk8}}{{#jdk8}} {
         {{#delegate-method}}

Generates:

        produces = { "application/json","application/xml","text/plain", }, 
        method = RequestMethod.GET)

After this change, the swagger UI presents three content types but there appears to be more to this issue since only one of the types is ever sent with the request when the "try it out button" clicked

jose-hernandez-001 avatar Aug 09 '18 21:08 jose-hernandez-001

this has been fixed by #258

HugoMario avatar Jun 08 '20 11:06 HugoMario