Springdoc-openapi: Reuse configured JSON convertor

Created on 3 Dec 2019  路  4Comments  路  Source: springdoc/springdoc-openapi

Hi

Thank you for the great library.

However, we have faced an issue due to this code:

@Operation(hidden = true) @GetMapping(value = API_DOCS_URL, produces = MediaType.APPLICATION_JSON_VALUE) public String openapiJson(HttpServletRequest request, @Value(API_DOCS_URL) String apiDocsUrl) throws JsonProcessingException { calculateServerUrl(request, apiDocsUrl); OpenAPI openAPI = this.getOpenApi(); return Json.mapper().writeValueAsString(openAPI); }

The problem is that we have configured our own custom MappingJackson2HttpMessageConverter.
And there are reasons behind it, like we have some classes like ObjectId ( from MongoDB ) which we want to be returned as String.

Making Json.mapper().writeValueAsString(openAPI) causes multiple issues:

  • Objects like ObjectId are serialized incorrectly

  • Json returned is escaped, ie it looks like "{\"openapi\":\"3.0.1\"

I believe that you should return OpenAPI object directly and let the Spring handle serialization.

Regards

Most helpful comment

Hi again

I have researched the topic even further and I believe that io.swagger.v3.core.util.Json should be made configurable. So we could configure additional modules to for a proper time format, ObjectId issue etc.

Still haven't figured out why with standard Spring message converters the json is properly returned and why if we override MappingJackson2HttpMessageConverter, then json is escaped. Will research more on that.

All 4 comments

Hi again

I have researched the topic even further and I believe that io.swagger.v3.core.util.Json should be made configurable. So we could configure additional modules to for a proper time format, ObjectId issue etc.

Still haven't figured out why with standard Spring message converters the json is properly returned and why if we override MappingJackson2HttpMessageConverter, then json is escaped. Will research more on that.

Hi,

We rely for our library on the official swagger-annotations jars.
We do not override any behaviour of jackson.
You can investigate more on your side and let us know.

I was having the exact same problem with the api-docs JSON being contained in quotes.

In my case, I solved it in my Spring configuration file that extends WebMvcConfigurerAdapter by switching from overriding configureMessageConverters to extendMessageConverters while leaving alone the custom MappingJackson2HttpMessageConverter I was building in the method.

I also believe the JSON mapper used in spring doc should me made accessible, so that we could customize the serialization as we want (using mixin for example)

Actually it's accessible through:

io.swagger.v3.core.util.Json.mapper()
Was this page helpful?
0 / 5 - 0 ratings

Related issues

amorenew picture amorenew  路  5Comments

andygoossens picture andygoossens  路  6Comments

bfs-natewallman picture bfs-natewallman  路  4Comments

vilmosnagy picture vilmosnagy  路  6Comments

riimeik picture riimeik  路  4Comments