Springdoc-openapi: using https

Created on 16 Oct 2019  路  16Comments  路  Source: springdoc/springdoc-openapi

Given that the current url for swagger-ui uses https.
Then the generated server url still uses http.

Bildschirmfoto von 2019-10-16 11-58-42

So executing an api-call fails.
The Request URL uses http.
TypeError: Failed to fetch

Bildschirmfoto von 2019-10-16 12-07-39

Any idea how to get it work?

Most helpful comment

I fixed this with this:

@Configuration
public class OpenAPIConfiguration {

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .addServersItem(new Server().url("/"));
    }
}

All 16 comments

Hi,

We are not able to repoduce your issue.
The swagger-ui works without any issue on https;
Can you please describe the way to reproduce it ?

  1. Configure spring-boot-app to run with http
  2. Deploy the app to kubernetes
  3. Make the app reachable from outside with ingress and https
  4. Open the swagger-ui of the deployed app
  5. Execute an api-call

For a quick workaround (if resources and docs are accessed from the same base URL) disable org.springdoc.api.OpenApiResource (springdoc.api-docs.enabled=false) and to replace it with an alternative where calculateServerUrl is defined as:

    private void calculateServerUrl() {
        generalInfoBuilder.setServerBaseUrl("/");
    }

It's more likely than not that the application serving the docs is used through some (TLS terminating, path rewriting etc) reverse proxy or other complex setup, so would be maybe useful support configuring the base URL freely (i.e could be both relative or absolute) from properties?

Related specification describing the server URL configuration options:
https://swagger.io/docs/specification/api-host-and-base-path/

hi @kvedel,

thanks for hints.

    private void calculateServerUrl() {
        generalInfoBuilder.setServerBaseUrl("/");
    }

i am not sure where to implement that.

Related specification describing the server URL configuration options:
https://swagger.io/docs/specification/api-host-and-base-path/

okay. i have to configure servers.
i am not sure... does springdoc support openapi.yaml? Or is springdoc annotation-driven only?

nevertheless i tried

@OpenAPIDefinition(servers = {
        @Server(url = "http://localhost:8002/preis-api", description = "docker"),
        @Server(url = "https://localhost/preis-api", description = "kubernetes")
})

and i works.

but

  • now i have to select the right server from Servers list in ui.
  • the source code knows where it should run. this hurts a lot!
  • i wonder why i have to select an url from Servers list because the url is already given by url in browser.

is there any possibility to configure servers list by injecting them while deployment? so is there a way of configuration other than annotation in code?

Hi,

You can also use: The OpenAPI definition Bean, and inject the value of your serversUrl from your configuration files.

@Bean
public OpenAPI customOpenAPI() {
    return new OpenAPI().addServersItem(new Server().url("https://myserver.com"))
            .components(new Components().addSecuritySchemes("basicScheme",
                    new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")))
            .info(new Info().title("SpringShop API").version("V0")
                    .license(new License().name("Apache 2.0").url("http://springdoc.org")));
}

Thanks! Works like a charm!

We had the same issue and we didn't want to have environment specific URLs in our config. Therefore we tried to configuring the context path as serverUrl and it works like a charm (even the generated curl statement is correct):

@Bean 
public OpenAPI springShopOpenAPI(
         @Value("${info.app.version}") String appVersion,
         @Value("${server.servlet.context-path}") String contextPath
) {
    return new OpenAPI()
            .addServersItem(new Server().url(contextPath))
            .info(new Info()
                    .title("Lala")
                    .description("Lala")
                    .version(appVersion))
            .components(new Components()
                   .addSecuritySchemes("basicScheme",
                    new SecurityScheme().type(SecurityScheme.Type.HTTP).scheme("basic")));
}

I fixed this with this:

@Configuration
public class OpenAPIConfiguration {

    @Bean
    public OpenAPI openAPI() {
        return new OpenAPI()
                .addServersItem(new Server().url("/"));
    }
}

Hi, sorry for bumping. Anyone know how to remove only the server description "Generated server url"? I see that I can set a new server url, but I do not want to manually add the server url, because for deployment the baseurl will change.

@Cytor,

You just use the standard swagger annotation @OpenAPIDefinition (ideally, in a spring Bean):

https://github.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations#OpenAPIDefinition

@OpenAPIDefinition(info = @Info(title = "API Examples", version = "1.0"),
        servers = @Server(url = "http://toto.com", description = "your descrption"),
        tags = @Tag(name = "Operations"))
@Component
public class MyConfig {

}

@bnasslahsen Thanks. I am familiar with the code config you placed and also via Spring Bean method. This way unfortunately I also need to define the URL address and I would like to only edit the description URL, ideally remove it and not mess with the URL which is automatically generated by springdoc.

@Cytor,

For changing only the description you have OpenApiCustomiser:

    @Bean
    public OpenApiCustomiser serverOpenApiCustomiser1() {
        return openAPI -> openAPI.getServers().forEach(server -> server.setDescription("my new description"));
    }

@bnasslahsen The code does not change the description of current base Url.

@Cytor,

It replaces the "Generated server url" description.
But if you need more assistance, please add sample HelloController with the expected OpenAPI description.

@bnasslahsen This is the code I am using and it doesn't change the default description to "my new description".

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Bean
    public OpenApiCustomiser serverOpenApiCustomiser1() {
        return openAPI -> openAPI.getServers().forEach(server -> server.setDescription("my new description"));
    }
}

@Cytor,

Here is a full working code.

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }


    @Bean
    public OpenApiCustomiser serverOpenApiCustomiser1() {
        return openAPI -> openAPI.getServers().forEach(server -> server.setDescription("my new description"));
    }

    @RestController
    public class HelloController {

        @GetMapping("/hello")
        String hello( ) {
            return "Hello";
        }

    }

}

And this is the generated spec:

openapi: 3.0.1
info:
  title: OpenAPI definition
  version: v0
servers:
  - url: 'http://localhost:8080'
    description: my new description
paths:
  /hello:
    get:
      tags:
        - hello-controller
      operationId: hello
      responses:
        '200':
          description: OK
          content:
            '*/*':
              schema:
                type: string
components: {}

Can you test and confirm that you are getting the same result.

If the following code doesn't work on your local environment, then you will have to add the link to a minimal reproducible sample.

Was this page helpful?
0 / 5 - 0 ratings