Skip to content

Commit

Permalink
Cannot override servers in api-docs.yaml using OpenApiCustomiser; Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
bnasslahsen committed Jun 30, 2021
1 parent bae53c3 commit edf76e8
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,16 +455,32 @@ private void buildOpenAPIWithOpenAPIDefinition(OpenAPI openAPI, OpenAPIDefinitio
AnnotationsUtils.getTags(apiDef.tags(), false).ifPresent(tags -> openAPI.setTags(new ArrayList<>(tags)));
// OpenApiDefinition servers
Optional<List<Server>> optionalServers = AnnotationsUtils.getServers(apiDef.servers());
if (optionalServers.isPresent()) {
openAPI.setServers(optionalServers.get());
this.isServersPresent = true;
}
optionalServers.map(this::resolveProperties).ifPresent(servers -> {
this.isServersPresent = true;
openAPI.servers(servers);
}
);
// OpenApiDefinition extensions
if (apiDef.extensions().length > 0) {
openAPI.setExtensions(AnnotationsUtils.getExtensions(apiDef.extensions()));
}
}

/**
* Resolve properties info.
*
* @param servers the servers
* @return the servers
*/
private List<Server> resolveProperties(List<Server> servers) {
PropertyResolverUtils propertyResolverUtils = context.getBean(PropertyResolverUtils.class);
servers.forEach(server -> {
resolveProperty(server::getUrl, server::url, propertyResolverUtils);
resolveProperty(server::getDescription, server::description, propertyResolverUtils);
});
return servers;
}

/**
* Resolve properties info.
*
Expand Down Expand Up @@ -618,7 +634,7 @@ private Set<io.swagger.v3.oas.annotations.security.SecurityScheme> getSecuritySc
io.swagger.v3.oas.annotations.security.SecurityScheme.class));
SecuritySchemes apiSecuritySchemes
= AnnotationUtils.findAnnotation(Class.forName(bd.getBeanClassName()), io.swagger.v3.oas.annotations.security.SecuritySchemes.class);
if (apiSecuritySchemes!=null && !ArrayUtils.isEmpty(apiSecuritySchemes.value()))
if (apiSecuritySchemes != null && !ArrayUtils.isEmpty(apiSecuritySchemes.value()))
Arrays.stream(apiSecuritySchemes.value()).forEach(apiSecurityScheme::add);
}
catch (ClassNotFoundException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
import io.swagger.v3.oas.annotations.security.OAuthFlow;
import io.swagger.v3.oas.annotations.security.OAuthFlows;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.annotations.servers.Server;

@OpenAPIDefinition(info = @Info(title = "My App",
@OpenAPIDefinition(servers = @Server(url = "${test.server}", description = "${test.desc}"), info = @Info(title = "My App",
description = "Some long and useful description", version = "v1"))
@SecurityScheme(name = "security_auth", type = SecuritySchemeType.OAUTH2,
flows = @OAuthFlows(authorizationCode = @OAuthFlow(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
import test.org.springdoc.api.AbstractSpringDocTest;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.test.context.TestPropertySource;


/**
* Tests Spring meta-annotations as method parameters
*/
@TestPropertySource(properties = {"test.server=http://test.toto.com", "test.desc=toto desc"})
public class SpringDocApp137Test extends AbstractSpringDocTest {

@SpringBootApplication
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
},
"servers": [
{
"url": "http://localhost",
"description": "Generated server url"
"url": "http://test.toto.com",
"description": "toto desc",
"variables": {}
}
],
"paths": {
Expand Down

0 comments on commit edf76e8

Please sign in to comment.