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

[JAVA] isJsonMime should support vendor specific JSON MIME type

Open davidharrigan opened this issue 9 years ago • 2 comments

Description

We support APIs that return JSON encoded responses with vendor specific mime types. These MIME types look something like:

application/vnd.company+json

Which is a valid MIME-type. A response with the above mime-type as its content-type indicates that the response is JSON encoded. When the swagger-client attempts to deserialize such response, it throws an exception due to a method responsible for determining whether the given MIME is a JSON MIME. The method is isJsonMime and it checks whether the mime type matches the following regex:

(?i)application\\/json(;.*)?

This results in the following error:

Content type "application/vnd.company+json" is not supported for type: class com.somemodel Model

Not tested, but I think the above regex should be updated to something like:

(?i)application\\/((\w\.)*\w\+)?json(;.*)?"

so that MIME types like application/vnd.company+json are recognized as a valid JSON MIME.

I think it would be valid to have some additional tests here that checks for MIME types such as the one mentioned above.

Swagger-codegen version

v2.2.1

Swagger declaration file content or url

N/A

Command line used for generation
java -jar /opt/swagger-codegen/modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i /mnt/swagger.yaml -l java -o /mnt/clients/java -c /mnt/clients/configs/java.json
Steps to reproduce
  • Generate a client for some API
  • Have the API return a vendor JSON MIME type as the content header
  • use the client to retrieve from the endpoint which produces a vendor JSON MIME type
  • watch it scream
Related issues

This is a simpler fix, but I believe https://github.com/swagger-api/swagger-codegen/issues/3870 is related.

Suggest a Fix

Mentioned in the description.

davidharrigan avatar Mar 22 '17 21:03 davidharrigan

isJsonMime should accept anything matching application/json or ending with +json (cf. RFC 6839), in both cases case-insensitive and after stripping off everything at or after the first semicolon and any (remaining) leading/trailing whitespace (cf. RFC 7231 and RFC 7230). In code, something like

mime.matches("(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$")

http://ideone.com/MCsVFO

gibson042 avatar Mar 23 '17 20:03 gibson042

Closed via #5189

wing328 avatar Apr 27 '17 13:04 wing328