The provided definition does not specify a valid version field when render openapi

9k views Asked by At

I am integrate the OpenAPI with my project, when I access the url: http://127.0.0.1:11014/swagger-ui/index.html, shows error like this:

Unable to render this definition
The provided definition does not specify a valid version field.

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).

 

this is the OpenAPI config:

package misc.config.openapi;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import io.swagger.v3.oas.models.info.License;
import org.springdoc.core.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * https://springdoc.org/
 * https://github.com/springdoc/springdoc-openapi
 */
@Configuration
public class OpenApiConfig {

    @Bean
    public GroupedOpenApi fortuneApi() {
        GroupedOpenApi.Builder builder = GroupedOpenApi.builder()
                .pathsToMatch("/fortune/**")
                .group("dddd");
        GroupedOpenApi groupedOpenApi = builder.build();
        return groupedOpenApi;
    }

    @Bean
    public OpenAPI fortuneAPI() {
        return new OpenAPI()
                .info(new Info().title("Fortune API")
                        .description("Spring shop sample application")
                        .version("v0.0.1")
                        .license(new License().name("Apache 2.0").url("http://springdoc.org")))
                .externalDocs(new ExternalDocumentation()
                        .description("SpringShop Wiki Documentation")
                        .url("https://springshop.wiki.github.org/docs"));
    }

}

I have read the question Swagger..Unable to render this definition The provided definition does not specify a valid version field and tried the answer, both did not work. what should I do to specify the version? This is the dependencies looks like:

    api "org.springdoc:springdoc-openapi-ui:1.6.9"

I make a minimal reproduce and found the normal response is json object, but the problem response return string. This is the correct response:

{
    "openapi": "3.0.1",
    "info": {
        "title": "Fortune API",
        "description": "Spring shop sample application",
        "license": {
            "name": "Apache 2.0",
            "url": "http://springdoc.org"
        },
        "version": "v0.0.1"
    },
    "externalDocs": {
        "description": "SpringShop Wiki Documentation",
        "url": "https://springshop.wiki.github.org/docs"
    },
    "servers": [
        {
            "url": "http://127.0.0.1:11018",
            "description": "Generated server url"
        }
    ],
    "paths": {},
    "components": {}
}

this is the problem response in my project:

"{\"openapi\":\"3.0.1\",\"info\":{\"title\":\"Fortune API\",\"description\":\"Spri......
2

There are 2 answers

2
Dolphin On

Finally I found that I changed the json converter cause this problem, this is a solved way:

@EnableWebMvc
@Configuration
public class WebConvertConfig implements WebMvcConfigurer {

    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jackson2HttpMessageConverter = new MappingJackson2HttpMessageConverter();
        converters.add(new StringHttpMessageConverter());
    }
}

this StringHttpMessageConverter should be the first one added into converter. I think this is the openapi design problem to make it hard to use. More information from here:

0
Nothing4ever On

I encountered the same thing but for a different reason. The endpoint v3/api-docs return a base64 string instead of json. (Something like: eyJvcGVuYXBpIjoiMy4wLjEiLCJpbmZvIjp7InRpdGxlIjoiT3BlbkFQSSBkZWZpbml0aW9uIiwid..).

After searching a bit, i found that when overriding the default spring-boot registered HttpMessageConverter, you should have ByteArrayHttpMessageConverter registered as well to have proper springdoc-openapi support.

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

Note: Order is very important, when registering HttpMessageConverters.

References: