OpenAPI escapes JSON response in Spring Boot application
Asked Answered
H

3

6

I recently tried to generate OpenAPI documentation for my Sprint Boot application. I added the following lines to my pom.xml

<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.2.30</version>
</dependency>

But when I hit the swagger-ui URL on my localhost I get this page enter image description here

I checked the JSON at /v3/api-docs and I get a response that starts:

"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"OpenAPI definition\",\"version\":\"v0\"},\"servers\":[{\"url\":\"https://localhost:8900/tds\",

I can see that the openapi field is being specified, but it looks like the entire response is stringified instead of just being JSON. Since there doesn't seem to be any configuration for OpenAPI, I assume it's getting that from something in my Spring Boot configuration, but I have no idea where.

Huberthuberto answered 3/2, 2020 at 18:10 Comment(4)
what you get when you hit localhost:8080/swagger-ui.html? are you able to open the swagger-ui.htmlFaliscan
@Faliscan The screenshot is of swagger-ui.html on my localhostHuberthuberto
I have the same problem. Did you ever find a solution?Comminute
@TobiasLott No, I haven’t had much time to investigate further. If you find a solution please add an answer belowHuberthuberto
C
5

I had the same problem. In my case, I have overridden configureMessageConverters(List<HttpMessageConverter<?>> converters) in the class extending WebMvcConfigurer. Springdoc uses the converters that are configured there.

First I had some custom converter added to the converters first, and then the StringHttpMessageConverter. The canWrite(Class<?> clazz, MediaType mediaType) method of both converters returned true for clazz = String.class and mediaType = application/json. Thus the first one was used, which escaped the JSON as string.

When I changed it so that the StringHttpMessageConverter is added first, that one is used instead, and serializes the JSON object without escaping.

Comminute answered 12/5, 2020 at 8:40 Comment(1)
What helped me was using extendMessageConverters instead of configureMessageConverters in the WebMvcConfigurer. Recommendation found here: github.com/springdoc/springdoc-openapi/issues/…Afternoon
G
3

This is a small workaround, just point to yaml version: springdoc.swagger-ui.url=/v3/api-docs.yaml

gson is configured as the default JSON parser

Grammar answered 12/4, 2022 at 11:42 Comment(1)
thanks a lot, this workaround is really helped me. Cheers!Shriver

© 2022 - 2024 — McMap. All rights reserved.