Skip to content

Commit aa6a240

Browse files
author
bnasslahsen
committed
Add Suppot for Hiding org.springframework.security.core.Authentication on ServerHttpResponse. Fixes #423.
1 parent 1c138d6 commit aa6a240

File tree

9 files changed

+173
-15
lines changed

9 files changed

+173
-15
lines changed

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/GenericResponseBuilder.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import org.springframework.web.method.HandlerMethod;
5555

5656
import static org.springdoc.core.Constants.DEFAULT_DESCRIPTION;
57+
import static org.springdoc.core.converters.ConverterUtils.isResponseTypeToIgnore;
5758

5859
@SuppressWarnings("rawtypes")
5960
public class GenericResponseBuilder {
@@ -256,7 +257,7 @@ private Schema<?> calculateSchema(Components components, Type returnType, JsonVi
256257
return null;
257258
}
258259
Schema<?> schemaN = SpringDocAnnotationsUtils.extractSchema(components, returnType, jsonView);
259-
if (schemaN == null && returnType instanceof Class) {
260+
if (schemaN == null && returnType instanceof Class && !isResponseTypeToIgnore((Class) returnType)) {
260261
schemaN = AnnotationsUtils.resolveSchemaFromType((Class) returnType, null, jsonView);
261262
}
262263
return schemaN;

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ConverterUtils.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class ConverterUtils {
3131
private ConverterUtils() { }
3232

3333
private static final List<Class<?>> RESULT_WRAPPERS_TO_IGNORE = new ArrayList<>();
34+
private static final List<Class<?>> RESPONSE_TYPES_TO_IGNORE = new ArrayList<>();
3435

3536
static {
3637
RESULT_WRAPPERS_TO_IGNORE.add(Callable.class);
@@ -42,8 +43,16 @@ private ConverterUtils() { }
4243
public static void addResponseWrapperToIgnore(Class<?> cls){
4344
RESULT_WRAPPERS_TO_IGNORE.add(cls);
4445
}
45-
46+
47+
public static void addResponseTypeToIgnore(Class<?> cls){
48+
RESPONSE_TYPES_TO_IGNORE.add(cls);
49+
}
50+
4651
public static boolean isResponseTypeWrapper(Class<?> rawClass) {
4752
return RESULT_WRAPPERS_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
4853
}
54+
55+
public static boolean isResponseTypeToIgnore(Class<?> rawClass){
56+
return RESPONSE_TYPES_TO_IGNORE.stream().anyMatch(clazz -> clazz.isAssignableFrom(rawClass));
57+
}
4958
}

Diff for: springdoc-openapi-common/src/main/java/org/springdoc/core/converters/ResponseSupportConverter.java

+3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import io.swagger.v3.oas.models.media.Schema;
3030
import io.swagger.v3.oas.models.media.StringSchema;
3131

32+
import static org.springdoc.core.converters.ConverterUtils.isResponseTypeToIgnore;
3233
import static org.springdoc.core.converters.ConverterUtils.isResponseTypeWrapper;
3334

3435
public class ResponseSupportConverter implements ModelConverter {
@@ -51,6 +52,8 @@ else if (innerType.getBindings() != null && isResponseTypeWrapper(innerType.getR
5152
type = new AnnotatedType(innerType).jsonViewAnnotation(type.getJsonViewAnnotation()).resolveAsRef(true);
5253
}
5354
}
55+
else if (isResponseTypeToIgnore(cls))
56+
return null;
5457
}
5558
if (chain.hasNext()) {
5659
return chain.next().resolve(type, context, chain);

Diff for: springdoc-openapi-security/src/main/java/org/springdoc/core/IgnoredParameterAnnotationsWithSecurity.java renamed to springdoc-openapi-security/src/main/java/org/springdoc/core/IgnoredParameterWithSecurity.java

+10-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,18 @@
1818

1919
package org.springdoc.core;
2020

21+
import org.springframework.security.core.Authentication;
2122
import org.springframework.security.core.annotation.AuthenticationPrincipal;
2223

23-
public class IgnoredParameterAnnotationsWithSecurity implements IgnoredParameterAnnotations {
24+
import static org.springdoc.core.AbstractRequestBuilder.addRequestWrapperToIgnore;
25+
import static org.springdoc.core.converters.ConverterUtils.addResponseTypeToIgnore;
26+
27+
public class IgnoredParameterWithSecurity implements IgnoredParameterAnnotations {
28+
29+
static {
30+
addRequestWrapperToIgnore(Authentication .class);
31+
addResponseTypeToIgnore(Authentication.class);
32+
}
2433

2534
@Override
2635
public boolean isAnnotationToIgnore(java.lang.reflect.Parameter parameter) {

Diff for: springdoc-openapi-security/src/main/java/org/springdoc/core/SpringDocSecurityConfiguration.java

+2-7
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,8 @@ public class SpringDocSecurityConfiguration {
3333

3434
@Bean
3535
@Primary
36-
IgnoredParameterAnnotationsWithSecurity ignoredParameterAnnotationsWithSecurity() {
37-
return new IgnoredParameterAnnotationsWithSecurity();
38-
}
39-
40-
@Bean
41-
IgnoredParameterTypes ignoredParameterTypes() {
42-
return new IgnoredParameterTypes();
36+
IgnoredParameterWithSecurity ignoredParameterAnnotationsWithSecurity() {
37+
return new IgnoredParameterWithSecurity();
4338
}
4439

4540
@Configuration

Diff for: springdoc-openapi-security/src/main/java/org/springdoc/core/IgnoredParameterTypes.java renamed to springdoc-openapi-security/src/test/java/test/org/springdoc/api/app5/HelloController.java

+15-5
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@
1616
*
1717
*/
1818

19-
package org.springdoc.core;
19+
package test.org.springdoc.api.app5;
2020

2121
import org.springframework.security.core.Authentication;
22+
import org.springframework.web.bind.annotation.GetMapping;
23+
import org.springframework.web.bind.annotation.PostMapping;
24+
import org.springframework.web.bind.annotation.RestController;
2225

23-
public class IgnoredParameterTypes {
26+
@RestController
27+
public class HelloController {
2428

25-
public IgnoredParameterTypes() {
26-
AbstractRequestBuilder.addRequestWrapperToIgnore(Authentication.class);
29+
30+
@GetMapping
31+
public Authentication doGet() {
32+
return null;
2733
}
2834

29-
}
35+
@PostMapping
36+
public Sample doPost() {
37+
return null;
38+
}
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.app5;
20+
21+
import org.springframework.security.core.Authentication;
22+
23+
public class Sample {
24+
private String toto;
25+
private Authentication authentication;
26+
27+
public String getToto() {
28+
return toto;
29+
}
30+
31+
public void setToto(String toto) {
32+
this.toto = toto;
33+
}
34+
35+
public Authentication getAuthentication() {
36+
return authentication;
37+
}
38+
39+
public void setAuthentication(Authentication authentication) {
40+
this.authentication = authentication;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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.app5;
20+
21+
import test.org.springdoc.api.AbstractSpringDocTest;
22+
23+
import org.springframework.boot.autoconfigure.SpringBootApplication;
24+
25+
public class SpringDocApp5Test extends AbstractSpringDocTest {
26+
27+
@SpringBootApplication(scanBasePackages = { "test.org.springdoc.api.configuration,test.org.springdoc.api.app5" })
28+
static class SpringDocTestApp {}
29+
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
{
2+
"openapi": "3.0.1",
3+
"info": {
4+
"title": "OpenAPI definition",
5+
"version": "v0"
6+
},
7+
"servers": [
8+
{
9+
"url": "http://localhost",
10+
"description": "Generated server url"
11+
}
12+
],
13+
"paths": {
14+
"/": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "doGet",
20+
"responses": {
21+
"200": {
22+
"description": "default response",
23+
"content": {}
24+
}
25+
}
26+
},
27+
"post": {
28+
"tags": [
29+
"hello-controller"
30+
],
31+
"operationId": "doPost",
32+
"responses": {
33+
"200": {
34+
"description": "default response",
35+
"content": {
36+
"*/*": {
37+
"schema": {
38+
"$ref": "#/components/schemas/Sample"
39+
}
40+
}
41+
}
42+
}
43+
}
44+
}
45+
}
46+
},
47+
"components": {
48+
"schemas": {
49+
"Sample": {
50+
"type": "object",
51+
"properties": {
52+
"toto": {
53+
"type": "string"
54+
}
55+
}
56+
}
57+
}
58+
}
59+
}

0 commit comments

Comments
 (0)