Skip to content

Malformed (base64 encoded) api-docs JSON when MappingJackson2HttpMessageConverter is active #2143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
lbenedetto opened this issue Mar 16, 2023 · 3 comments
Labels
question Further information is requested

Comments

@lbenedetto
Copy link

lbenedetto commented Mar 16, 2023

To Reproduce
Steps to reproduce the behavior:

  • Spring Boot 3.0.4 or 2.7.8
  • springdoc-openapi-starter-webmvc-ui:2.0.4 or 1.6.15
@Configuration
public class ApiWebMvcConfiguration extends DelegatingWebMvcConfiguration {

  @Autowired
  Jackson2ObjectMapperBuilder jacksonBuilder;

  @Override
  protected void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    var applicationContext = this.getApplicationContext();
    if (applicationContext != null) {
      jacksonBuilder.applicationContext(applicationContext);
    }
    converters.add(new MappingJackson2HttpMessageConverter(jacksonBuilder.build()));
  }
}

Expected behavior

Expected:

{
    "openapi": "3.0.1",
    "info": {
        "title": "OpenAPI definition",
        "version": "v0"
    },
    "servers": [
        {
            "url": "http://localhost:8080",
            "description": "Generated server url"
        }
    ],
    "paths": {
        ...
    },
    "components": {}
}

Actual:

"eyJvcGVuYXBpIjoiMy4wLjEiLCJpbmZvIjp7InRpdGxlIjoiT3BlbkFQSSBkZWZpbml0aW9uIiwidmVyc2lvbiI6InYwIn0sInNlcnZlcnMiOlt7InVybCI6Imh0dHA6Ly9sb2NhbGhvc3Q6ODA4MCIsImRlc2NyaXB0aW9uIjoiR2VuZXJhdGVkIHNlcnZlciB1cmwifV0sInBhdGhzIjp7Ii9pZC92MS8iOnsiZ2V0Ijp7InRhZ3MiOlsiZGVtby1jb250cm9sbGVyIl0sInN1bW1hcnkiOiJPSyIsIm9wZXJhdGlvbklkIjoiaW5zdGFuY2VDb25maWciLCJyZXNwb25zZXMiOnsiMjAwIjp7ImRlc2NyaXB0aW9uIjoiT0siLCJjb250ZW50Ijp7IiovKiI6eyJzY2hlbWEiOnsidHlwZSI6InN0cmluZyJ9fX19fX19fSwiY29tcG9uZW50cyI6e319"

This causes swagger-ui to be unable to parse the response

Additional context
I believe this is caused by the changes required for fixing #2051. I tried both adding and removing StringHttpMessageConverter, but the only thing that helps is not registering MappingJackson2HttpMessageConverter

@ctruzzi
Copy link

ctruzzi commented Mar 16, 2023

We are facing the same issue, not sure if this was an expected regression or not but seems like more than a patch version change.

@lbenedetto lbenedetto changed the title /v3/api-docs returns base64 encoded Json Malformed (base64 encoded) api-docs JSON when MappingJackson2HttpMessageConverter is active Mar 17, 2023
@bnasslahsen
Copy link
Collaborator

@lbenedetto,

As you are overriding the default spring registered HttpMessageConverter, you should have ByteArrayHttpMessageConverter registered as well!

		converters.add(new ByteArrayHttpMessageConverter());
		converters.add(new MappingJackson2HttpMessageConverter(jacksonBuilder.build()));

Please note that order is very important, when registering HttpMessageConverters.

@bnasslahsen bnasslahsen added the question Further information is requested label Mar 18, 2023
@lbenedetto
Copy link
Author

Ah, I had addDefaultHttpMessageConverters(converters) (which adds a ByteArrayHttpMessageConverter) but it was registered after the Jackson converter. Thanks

@springdoc springdoc locked as resolved and limited conversation to collaborators Mar 18, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants