springdoc-openapi icon indicating copy to clipboard operation
springdoc-openapi copied to clipboard

How to change the "generated Servers" dropdown values

Open Azyl opened this issue 7 years ago • 7 comments

Hello,

I am having an issue as due to our environment only https calls are permitted. A proxy is handling the conversion from https to http

As the springboot project is deployed as http the generated swagger-ui points all URLs to http although it is accessed through https.

I have tried to add a new server to the OpenApi Bean in order define a HTTPS version but to no avail.

Could you please let me know how can I change the basePath so that we are using HTTPS

image

Thank you for the help

Azyl avatar Sep 19 '19 22:09 Azyl

Hi,

You have different ways for setting serverUrl: For example:

  • You can set them on the OpenAPI definition Bean
@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")));
}
  • You can use the @OpenAPIDefinition swagger annotation
@OpenAPIDefinition(servers = { @Server(url = "https://myserver1.com"),
		@Server(url = "https://myserver2.com") }, info = @Info(title = "the title", version = "v1", description = "My API", license = @License(name = "Apache 2.0", url = "http://foo.bar"), contact = @Contact(url = "http://gigantic-server.com", name = "Fred", email = "[email protected]")))

ghost avatar Sep 21 '19 09:09 ghost

Hi,

Thank you for taking the time to educate others.

I have tried the first approach before writing here and it does not work, the servers values do not appear to be used. I have even used the code snippet you provided above.

The end result is the same:

image

The second option with the annotations is working:

image

So perhaps there is something here, perhaps a Bug.

If needed I can try to recreate and submit a test project for you.

Thank you and have a nice weekend.

Azyl avatar Sep 21 '19 11:09 Azyl

Hi,

Overriding OpenAPI Bean Definition should also work. Can you please add/upload an example, of the code that is not working ?

ghost avatar Sep 21 '19 12:09 ghost

Hi,

I uploaded it to github at https://github.com/Azyl/springdocserverlistfrombeanissue.git

Let me know if the format is ok.

It is just the starter springboot project with springdoc and a configuration class with the OpenApi bean.

the result is:

image

Thanks for the help

Azyl avatar Sep 21 '19 17:09 Azyl

Hi,

On your class OpenApiSwaggerConfig, just remove @OpenAPIDefinition annotation, and it should work, also while overriding OpenAPI Bean Definition :

@Configuration
public class OpenApiSwaggerConfig 

ghost avatar Sep 22 '19 11:09 ghost

How is the url generated in case the application is behind the reverse proxy? it does not takes into account the prefix path set by the proxy.

kaus02 avatar Jul 13 '21 10:07 kaus02

How is the url generated in case the application is behind the reverse proxy? it does not takes into account the prefix path set by the proxy.

Read the documentation.

bnasslahsen avatar Jul 13 '21 11:07 bnasslahsen