Skip to content

Commit 382f009

Browse files
committed
Multiple @Securityscheme inside a @SecuritySchemes are not present in the openapi spec. Fixes #1125
1 parent 27cbb4b commit 382f009

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/OpenAPIService.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.lang.reflect.Method;
2424
import java.util.ArrayList;
25+
import java.util.Arrays;
2526
import java.util.Collections;
2627
import java.util.HashMap;
2728
import java.util.HashSet;
@@ -39,6 +40,7 @@
3940
import io.swagger.v3.core.util.AnnotationsUtils;
4041
import io.swagger.v3.oas.annotations.Hidden;
4142
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
43+
import io.swagger.v3.oas.annotations.security.SecuritySchemes;
4244
import io.swagger.v3.oas.annotations.tags.Tag;
4345
import io.swagger.v3.oas.annotations.tags.Tags;
4446
import io.swagger.v3.oas.models.Components;
@@ -51,6 +53,7 @@
5153
import io.swagger.v3.oas.models.media.Schema;
5254
import io.swagger.v3.oas.models.security.SecurityScheme;
5355
import io.swagger.v3.oas.models.servers.Server;
56+
import org.apache.commons.lang3.ArrayUtils;
5457
import org.apache.commons.lang3.StringUtils;
5558
import org.slf4j.Logger;
5659
import org.slf4j.LoggerFactory;
@@ -526,6 +529,7 @@ private void calculateSecuritySchemes(Components components) {
526529
else {
527530
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(
528531
false);
532+
scanner.addIncludeFilter(new AnnotationTypeFilter(io.swagger.v3.oas.annotations.security.SecuritySchemes.class));
529533
scanner.addIncludeFilter(
530534
new AnnotationTypeFilter(io.swagger.v3.oas.annotations.security.SecurityScheme.class));
531535
if (AutoConfigurationPackages.has(context)) {
@@ -612,6 +616,10 @@ private Set<io.swagger.v3.oas.annotations.security.SecurityScheme> getSecuritySc
612616
try {
613617
apiSecurityScheme.add(AnnotationUtils.findAnnotation(Class.forName(bd.getBeanClassName()),
614618
io.swagger.v3.oas.annotations.security.SecurityScheme.class));
619+
SecuritySchemes apiSecuritySchemes
620+
= AnnotationUtils.findAnnotation(Class.forName(bd.getBeanClassName()), io.swagger.v3.oas.annotations.security.SecuritySchemes.class);
621+
if (apiSecuritySchemes!=null && !ArrayUtils.isEmpty(apiSecuritySchemes.value()))
622+
Arrays.stream(apiSecuritySchemes.value()).forEach(apiSecurityScheme::add);
615623
}
616624
catch (ClassNotFoundException e) {
617625
LOGGER.error("Class Not Found in classpath : {}", e.getMessage());
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app154;
20+
21+
import java.time.Instant;
22+
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
@RestController
27+
public class HelloController {
28+
29+
@GetMapping(path = "/")
30+
public String hello() {
31+
return "Hello world at " + Instant.now().toString();
32+
}
33+
34+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package test.org.springdoc.api.app154;
2+
3+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
4+
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
5+
import io.swagger.v3.oas.annotations.info.Info;
6+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
7+
import io.swagger.v3.oas.annotations.security.SecurityScheme;
8+
import io.swagger.v3.oas.annotations.security.SecuritySchemes;
9+
10+
@OpenAPIDefinition(info = @Info(title = "toto",version = "1.0"),
11+
security = {@SecurityRequirement(name = "basicAuth"), @SecurityRequirement(name = "bearerToken")}
12+
)
13+
@SecuritySchemes({
14+
@SecurityScheme(
15+
name = "basicAuth",
16+
type = SecuritySchemeType.HTTP,
17+
scheme = "basic"
18+
),
19+
@SecurityScheme(
20+
name = "bearerToken",
21+
type = SecuritySchemeType.HTTP,
22+
scheme = "bearer",
23+
bearerFormat = "JWT"
24+
)
25+
})
26+
public class OpenApiConfiguration {
27+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
*
3+
* * Copyright 2019-2020 the original author or authors.
4+
* *
5+
* * Licensed under the Apache License, Version 2.0 (the "License");
6+
* * you may not use this file except in compliance with the License.
7+
* * You may obtain a copy of the License at
8+
* *
9+
* * https://www.apache.org/licenses/LICENSE-2.0
10+
* *
11+
* * Unless required by applicable law or agreed to in writing, software
12+
* * distributed under the License is distributed on an "AS IS" BASIS,
13+
* * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* * See the License for the specific language governing permissions and
15+
* * limitations under the License.
16+
*
17+
*/
18+
19+
package test.org.springdoc.api.app154;
20+
21+
22+
import test.org.springdoc.api.AbstractSpringDocTest;
23+
24+
import org.springframework.boot.autoconfigure.SpringBootApplication;
25+
26+
/**
27+
* Tests Spring meta-annotations as method parameters
28+
*/
29+
public class SpringDocApp154Test extends AbstractSpringDocTest {
30+
31+
@SpringBootApplication
32+
static class SpringDocTestApp {}
33+
34+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "toto",
5+
"version": "1.0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"security": [
14+
{
15+
"basicAuth": []
16+
},
17+
{
18+
"bearerToken": []
19+
}
20+
],
21+
"paths": {
22+
"/": {
23+
"get": {
24+
"tags": [
25+
"hello-controller"
26+
],
27+
"operationId": "hello",
28+
"responses": {
29+
"200": {
30+
"description": "OK",
31+
"content": {
32+
"*/*": {
33+
"schema": {
34+
"type": "string"
35+
}
36+
}
37+
}
38+
}
39+
}
40+
}
41+
}
42+
},
43+
"components": {
44+
"securitySchemes": {
45+
"bearerToken": {
46+
"type": "http",
47+
"scheme": "bearer",
48+
"bearerFormat": "JWT"
49+
},
50+
"basicAuth": {
51+
"type": "http",
52+
"scheme": "basic"
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)