2

I am getting the error - "Please indicate a valid Swagger or OpenAPI version field. Supported version fields are swagger: "2.0" and those that match openapi: 3.0.n (for example, openapi: 3.0.0)." when I paste the generated json from /api-docs to editor.swagger.io. I have some observations.

  1. localhost:8080/v3/api-docs/ yields - "{\"openapi\":\"3.0.1\", "\info\":{ ---------------- This json gives the mentioned error in editor.swagger.io.

  2. On manually removing the initial double quotes and escape character i.e. {"openapi":"3.0.1", "info":{ ---------------- the error goes away i.e. UI is rendered without any issue.

  3. My project springboot version is 2.2.13.RELEASE, springdoc-openapi-ui version is 1.5.8, jackson-databind version is 2.10.5.1

  4. OpenAPI config class -

@Configuration
public class Config1 {
@Bean
public GroupedOpenApi publicApi() {
    return GroupedOpenApi.builder()
            .group("user")
            .pathsToMatch("/v1/**")
            .build();
}

@Bean
public OpenAPI customOpenAPI() {
    return new OpenAPI()
        .info(new Info().title("title1").description("test").version("1.0.0"));
}

Please let me know if any further information is required from my end. Any inputs regarding the issue will be helpful. Thank you.

  • Please post your Springdoc configs and any code that could be related to how the `/api-docs/` content is generated & served. – Helen Aug 03 '21 at 17:30
  • @Helen I have added Springdoc config as point 4 in the question. As per my understanding /api-docs/ content is generated by default from springdoc-openapi-ui package. Please let me know if you need any further input. Thank you. – Divyans Mahansaria Aug 04 '21 at 06:00

2 Answers2

2

I had the same issue with spring-boot-2.6.4, springdoc-openapi-1.6.6. The problem is localhost:8080/v3/api-docs/ responses "{"openapi":"3.0.1", "\info":{..." which is a String, NOT a valid JSON as expected.

It turns out in my WebMvcConfig implements WebMvcConfigurer, it doesn't have a StringHttpMessageConverter, so the solution is to add it to converters:

  @Override
  public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
    converters.add(stringHttpMessageConverter());
    ...
  }

  @Bean
  public StringHttpMessageConverter stringHttpMessageConverter() {
    StringHttpMessageConverter messageConverter = new StringHttpMessageConverter();
    messageConverter.setSupportedMediaTypes(List.of(MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN, MediaType.ALL));
    return messageConverter;
  }

If you're extending WebMvcConfigurationSupport instead of implementing WebMvcConfigurer, probably you'll not get this error as the StringHttpMessageConverter is added by default.

dhqvinh
  • 337
  • 4
  • 9
-3

You can use .version("v0.0.1") in your customOpenAPI or publicApi like this

@Bean
  public OpenAPI springShopOpenAPI() {
      return new OpenAPI()
              .version("v0.0.1"));
  }

For more details see docs https://springdoc.org/#migrating-from-springfox

kameshsr
  • 327
  • 3
  • 13