Skip to content

Commit 4dd6c9b

Browse files
author
bnasslahsen
committed
Generate empty scopes object. Fixes #893.
1 parent 0574ad1 commit 4dd6c9b

File tree

5 files changed

+167
-7
lines changed

5 files changed

+167
-7
lines changed

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

+4-7
Original file line numberDiff line numberDiff line change
@@ -119,16 +119,16 @@ public io.swagger.v3.oas.annotations.security.SecurityRequirement[] getSecurityR
119119
HandlerMethod handlerMethod) {
120120
// class SecurityRequirements
121121
Class<?> beanType = handlerMethod.getBeanType();
122-
Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> allSecurityTags = getSecurityRequirementsForClass(beanType);
122+
Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> allSecurityTags = getSecurityRequirementsForClass(beanType);
123123

124124
// handlerMethod SecurityRequirements
125125
Method method = handlerMethod.getMethod();
126-
allSecurityTags = getSecurityRequirementsForMethod(method,allSecurityTags);
126+
allSecurityTags = getSecurityRequirementsForMethod(method, allSecurityTags);
127127

128128
return (allSecurityTags != null) ? allSecurityTags.toArray(new io.swagger.v3.oas.annotations.security.SecurityRequirement[0]) : null;
129129
}
130130

131-
private Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> getSecurityRequirementsForMethod(Method method,Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> allSecurityTags) {
131+
private Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> getSecurityRequirementsForMethod(Method method, Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> allSecurityTags) {
132132
io.swagger.v3.oas.annotations.security.SecurityRequirements methodSecurity = AnnotatedElementUtils.findMergedAnnotation(method, io.swagger.v3.oas.annotations.security.SecurityRequirements.class);
133133
if (methodSecurity != null)
134134
allSecurityTags = addSecurityRequirements(allSecurityTags, new HashSet<>(Arrays.asList(methodSecurity.value())));
@@ -143,7 +143,7 @@ private Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> getSecur
143143
}
144144

145145
public Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> getSecurityRequirementsForClass(Class<?> beanType) {
146-
Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> allSecurityTags =null;
146+
Set<io.swagger.v3.oas.annotations.security.SecurityRequirement> allSecurityTags = null;
147147
io.swagger.v3.oas.annotations.security.SecurityRequirements classSecurity = AnnotatedElementUtils.findMergedAnnotation(beanType, io.swagger.v3.oas.annotations.security.SecurityRequirements.class);
148148
if (classSecurity != null)
149149
allSecurityTags = new HashSet<>(Arrays.asList(classSecurity.value()));
@@ -322,9 +322,6 @@ private Optional<OAuthFlow> getOAuthFlow(io.swagger.v3.oas.annotations.security.
322322
* @return the scopes
323323
*/
324324
private Optional<Scopes> getScopes(OAuthScope[] scopes) {
325-
if (isEmpty(scopes))
326-
return Optional.empty();
327-
328325
Scopes scopesObject = new Scopes();
329326
Arrays.stream(scopes).forEach(scope -> scopesObject.addString(scope.name(), scope.description()));
330327
return Optional.of(scopesObject);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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.app137;
20+
21+
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
22+
23+
import org.springframework.web.bind.annotation.GetMapping;
24+
import org.springframework.web.bind.annotation.RestController;
25+
26+
@RestController
27+
@SecurityRequirement(name = "security_auth")
28+
public class HelloController {
29+
30+
@GetMapping("/test")
31+
public void test(String hello) {
32+
}
33+
}
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.app137;
20+
21+
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
22+
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
23+
import io.swagger.v3.oas.annotations.info.Info;
24+
import io.swagger.v3.oas.annotations.security.OAuthFlow;
25+
import io.swagger.v3.oas.annotations.security.OAuthFlows;
26+
import io.swagger.v3.oas.annotations.security.SecurityScheme;
27+
28+
@OpenAPIDefinition(info = @Info(title = "My App",
29+
description = "Some long and useful description", version = "v1"))
30+
@SecurityScheme(name = "security_auth", type = SecuritySchemeType.OAUTH2,
31+
flows = @OAuthFlows(authorizationCode = @OAuthFlow(
32+
authorizationUrl = "http://authorization.url"
33+
, tokenUrl = "http://token.url", scopes = { })))
34+
public class OpenApiConfig {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * * Copyright 2019-2020 the original author or authors.
6+
* * * *
7+
* * * * Licensed under the Apache License, Version 2.0 (the "License");
8+
* * * * you may not use this file except in compliance with the License.
9+
* * * * You may obtain a copy of the License at
10+
* * * *
11+
* * * * https://www.apache.org/licenses/LICENSE-2.0
12+
* * * *
13+
* * * * Unless required by applicable law or agreed to in writing, software
14+
* * * * distributed under the License is distributed on an "AS IS" BASIS,
15+
* * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* * * * See the License for the specific language governing permissions and
17+
* * * * limitations under the License.
18+
* * *
19+
* *
20+
*
21+
*
22+
*/
23+
package test.org.springdoc.api.app137;
24+
25+
import test.org.springdoc.api.AbstractSpringDocTest;
26+
27+
import org.springframework.boot.autoconfigure.SpringBootApplication;
28+
29+
30+
/**
31+
* Tests Spring meta-annotations as method parameters
32+
*/
33+
public class SpringDocApp137Test extends AbstractSpringDocTest {
34+
35+
@SpringBootApplication
36+
static class SpringDocTestApp {}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "My App",
5+
"description": "Some long and useful description",
6+
"version": "v1"
7+
},
8+
"servers": [
9+
{
10+
"url": "http://localhost",
11+
"description": "Generated server url"
12+
}
13+
],
14+
"paths": {
15+
"/test": {
16+
"get": {
17+
"tags": [
18+
"hello-controller"
19+
],
20+
"operationId": "test",
21+
"parameters": [
22+
{
23+
"name": "hello",
24+
"in": "query",
25+
"required": true,
26+
"schema": {
27+
"type": "string"
28+
}
29+
}
30+
],
31+
"responses": {
32+
"200": {
33+
"description": "OK"
34+
}
35+
},
36+
"security": [
37+
{
38+
"security_auth": []
39+
}
40+
]
41+
}
42+
}
43+
},
44+
"components": {
45+
"securitySchemes": {
46+
"security_auth": {
47+
"type": "oauth2",
48+
"flows": {
49+
"authorizationCode": {
50+
"authorizationUrl": "http://authorization.url",
51+
"tokenUrl": "http://token.url",
52+
"scopes": {}
53+
}
54+
}
55+
}
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)