Skip to content

Commit 8a8ae05

Browse files
author
bnasslahsen
committed
Added support for @hidden on response class. Fixes #826
1 parent add8fe1 commit 8a8ae05

File tree

16 files changed

+241
-61
lines changed

16 files changed

+241
-61
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,7 +353,7 @@ protected Parameter customiseParameter(Parameter parameter, ParameterInfo parame
353353
* @return the boolean
354354
*/
355355
public boolean isParamToIgnore(MethodParameter parameter) {
356-
if (parameterBuilder.isAnnotationToIgnore(parameter))
356+
if (SpringDocAnnotationsUtils.isAnnotationToIgnore(parameter))
357357
return true;
358358
if ((parameter.getParameterAnnotation(PathVariable.class) != null && parameter.getParameterAnnotation(PathVariable.class).required())
359359
|| (parameter.getParameterAnnotation(RequestParam.class) != null && parameter.getParameterAnnotation(RequestParam.class).required())

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

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import com.fasterxml.jackson.annotation.JsonView;
3535
import io.swagger.v3.core.util.AnnotationsUtils;
3636
import io.swagger.v3.core.util.Json;
37-
import io.swagger.v3.oas.annotations.Hidden;
3837
import io.swagger.v3.oas.annotations.enums.Explode;
3938
import io.swagger.v3.oas.annotations.media.ExampleObject;
4039
import io.swagger.v3.oas.models.Components;
@@ -51,9 +50,7 @@
5150

5251
import org.springframework.core.MethodParameter;
5352
import org.springframework.core.ResolvableType;
54-
import org.springframework.core.annotation.AnnotationUtils;
5553
import org.springframework.core.io.Resource;
56-
import org.springframework.web.bind.annotation.RequestAttribute;
5754
import org.springframework.web.multipart.MultipartFile;
5855

5956
/**
@@ -68,11 +65,6 @@ public class GenericParameterBuilder {
6865
*/
6966
private static final List<Class<?>> FILE_TYPES = new ArrayList<>();
7067

71-
/**
72-
* The constant ANNOTATIOSN_TO_IGNORE.
73-
*/
74-
private static final List<Class> ANNOTATIOSN_TO_IGNORE = new ArrayList<>();
75-
7668
/**
7769
* The constant LOGGER.
7870
*/
@@ -81,8 +73,6 @@ public class GenericParameterBuilder {
8173
static {
8274
FILE_TYPES.add(MultipartFile.class);
8375
FILE_TYPES.add(Resource.class);
84-
ANNOTATIOSN_TO_IGNORE.add(Hidden.class);
85-
ANNOTATIOSN_TO_IGNORE.add(RequestAttribute.class);
8676
}
8777

8878
/**
@@ -108,26 +98,6 @@ public static void addFileType(Class<?>... classes) {
10898
FILE_TYPES.addAll(Arrays.asList(classes));
10999
}
110100

111-
/**
112-
* Add annotations to ignore.
113-
*
114-
* @param classes the classes
115-
*/
116-
public static void addAnnotationsToIgnore(Class<?>... classes) {
117-
ANNOTATIOSN_TO_IGNORE.addAll(Arrays.asList(classes));
118-
}
119-
120-
/**
121-
* Remove annotations to ignore.
122-
*
123-
* @param classes the classes
124-
*/
125-
public static void removeAnnotationsToIgnore(Class<?>... classes) {
126-
List classesToIgnore = Arrays.asList(classes);
127-
if (ANNOTATIOSN_TO_IGNORE.containsAll(classesToIgnore))
128-
ANNOTATIOSN_TO_IGNORE.removeAll(Arrays.asList(classes));
129-
}
130-
131101
/**
132102
* Is file boolean.
133103
*
@@ -345,19 +315,6 @@ else if (schemaN instanceof FileSchema || schemaN instanceof ArraySchema && ((Ar
345315
return schemaN;
346316
}
347317

348-
/**
349-
* Is annotation to ignore boolean.
350-
*
351-
* @param parameter the parameter
352-
* @return the boolean
353-
*/
354-
@SuppressWarnings("unchecked")
355-
public boolean isAnnotationToIgnore(MethodParameter parameter) {
356-
return ANNOTATIOSN_TO_IGNORE.stream().anyMatch(
357-
annotation -> parameter.getParameterAnnotation(annotation) != null
358-
|| AnnotationUtils.findAnnotation(parameter.getParameterType(), annotation) != null);
359-
}
360-
361318
/**
362319
* Sets examples.
363320
*

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,9 @@ private Type getReturnType(MethodParameter methodParameter) {
414414
* @return the schema
415415
*/
416416
private Schema<?> calculateSchema(Components components, Type returnType, JsonView jsonView, Annotation[] annotations) {
417-
return !isVoid(returnType) ? extractSchema(components, returnType, jsonView,annotations) : null;
417+
if (!isVoid(returnType) && !SpringDocAnnotationsUtils.isAnnotationToIgnore(returnType))
418+
return extractSchema(components, returnType, jsonView, annotations);
419+
return null;
418420
}
419421

420422
/**

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

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

2323
import java.lang.annotation.Annotation;
2424
import java.lang.reflect.Type;
25+
import java.util.ArrayList;
26+
import java.util.Arrays;
2527
import java.util.LinkedHashMap;
2628
import java.util.List;
2729
import java.util.Map;
@@ -32,6 +34,7 @@
3234
import io.swagger.v3.core.converter.ModelConverters;
3335
import io.swagger.v3.core.converter.ResolvedSchema;
3436
import io.swagger.v3.core.util.AnnotationsUtils;
37+
import io.swagger.v3.oas.annotations.Hidden;
3538
import io.swagger.v3.oas.annotations.media.ExampleObject;
3639
import io.swagger.v3.oas.models.Components;
3740
import io.swagger.v3.oas.models.media.ArraySchema;
@@ -45,7 +48,10 @@
4548
import org.slf4j.Logger;
4649
import org.slf4j.LoggerFactory;
4750

51+
import org.springframework.core.MethodParameter;
52+
import org.springframework.core.annotation.AnnotationUtils;
4853
import org.springframework.util.CollectionUtils;
54+
import org.springframework.web.bind.annotation.RequestAttribute;
4955

5056
/**
5157
* The type Spring doc annotations utils.
@@ -59,6 +65,16 @@ public class SpringDocAnnotationsUtils extends AnnotationsUtils {
5965
*/
6066
private static final Logger LOGGER = LoggerFactory.getLogger(SpringDocAnnotationsUtils.class);
6167

68+
/**
69+
* The constant ANNOTATIOSN_TO_IGNORE.
70+
*/
71+
private static final List<Class> ANNOTATIONS_TO_IGNORE = new ArrayList<>();
72+
73+
static {
74+
ANNOTATIONS_TO_IGNORE.add(Hidden.class);
75+
ANNOTATIONS_TO_IGNORE.add(RequestAttribute.class);
76+
}
77+
6278
/**
6379
* Resolve schema from type schema.
6480
*
@@ -208,6 +224,51 @@ public static void mergeSchema(Content existingContent, Schema<?> schemaN, Strin
208224
existingContent.addMediaType(mediaTypeStr, new io.swagger.v3.oas.models.media.MediaType().schema(schemaN));
209225
}
210226

227+
/**
228+
* Is annotation to ignore boolean.
229+
*
230+
* @param parameter the parameter
231+
* @return the boolean
232+
*/
233+
@SuppressWarnings("unchecked")
234+
public static boolean isAnnotationToIgnore(MethodParameter parameter) {
235+
return ANNOTATIONS_TO_IGNORE.stream().anyMatch(
236+
annotation -> parameter.getParameterAnnotation(annotation) != null
237+
|| AnnotationUtils.findAnnotation(parameter.getParameterType(), annotation) != null);
238+
}
239+
240+
/**
241+
* Is annotation to ignore boolean.
242+
*
243+
* @param type the type
244+
* @return the boolean
245+
*/
246+
public static boolean isAnnotationToIgnore(Type type) {
247+
return ANNOTATIONS_TO_IGNORE.stream().anyMatch(
248+
annotation -> (type instanceof Class
249+
&& AnnotationUtils.findAnnotation((Class<?>) type, annotation) != null));
250+
}
251+
252+
/**
253+
* Add annotations to ignore.
254+
*
255+
* @param classes the classes
256+
*/
257+
public static void addAnnotationsToIgnore(Class<?>... classes) {
258+
ANNOTATIONS_TO_IGNORE.addAll(Arrays.asList(classes));
259+
}
260+
261+
/**
262+
* Remove annotations to ignore.
263+
*
264+
* @param classes the classes
265+
*/
266+
public static void removeAnnotationsToIgnore(Class<?>... classes) {
267+
List classesToIgnore = Arrays.asList(classes);
268+
if (ANNOTATIONS_TO_IGNORE.containsAll(classesToIgnore))
269+
ANNOTATIONS_TO_IGNORE.removeAll(Arrays.asList(classes));
270+
}
271+
211272
/**
212273
* Add encoding to media type.
213274
*

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public SpringDocUtils removeResponseTypeToIgnore(Class<?> cls) {
207207
* @return the spring doc utils
208208
*/
209209
public SpringDocUtils addAnnotationsToIgnore(Class<?>... classes) {
210-
GenericParameterBuilder.addAnnotationsToIgnore(classes);
210+
SpringDocAnnotationsUtils.addAnnotationsToIgnore(classes);
211211
return this;
212212
}
213213

@@ -218,7 +218,7 @@ public SpringDocUtils addAnnotationsToIgnore(Class<?>... classes) {
218218
* @return the spring doc utils
219219
*/
220220
public SpringDocUtils removeAnnotationsToIgnore(Class<?>... classes) {
221-
GenericParameterBuilder.removeAnnotationsToIgnore(classes);
221+
SpringDocAnnotationsUtils.removeAnnotationsToIgnore(classes);
222222
return this;
223223
}
224224

springdoc-openapi-ui/src/test/resources/results/app1

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app10

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app7

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region

springdoc-openapi-ui/src/test/resources/results/app9

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region

springdoc-openapi-webflux-ui/src/test/resources/results/index5

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region

springdoc-openapi-webflux-ui/src/test/resources/results/index6

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region

springdoc-openapi-webflux-ui/src/test/resources/results/index7

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@
3333
<body>
3434
<div id="swagger-ui"></div>
3535

36-
<script src="./swagger-ui-bundle.js"> </script>
37-
<script src="./swagger-ui-standalone-preset.js"> </script>
36+
<script src="./swagger-ui-bundle.js" charset="UTF-8"> </script>
37+
<script src="./swagger-ui-standalone-preset.js" charset="UTF-8"> </script>
3838
<script>
3939
window.onload = function() {
4040
// Begin Swagger UI call region
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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.app130;
20+
21+
import org.springframework.web.bind.annotation.GetMapping;
22+
import org.springframework.web.bind.annotation.PostMapping;
23+
import org.springframework.web.bind.annotation.RestController;
24+
25+
@RestController
26+
public class HelloController {
27+
28+
@PostMapping(value = "/values/data")
29+
TrackerData list(TrackerData toto) {
30+
return toto;
31+
}
32+
33+
@GetMapping(value = "/values/datakk")
34+
TrackerData get(TrackerData toto) {
35+
return toto;
36+
}
37+
}
Lines changed: 38 additions & 0 deletions
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.app130;
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 SpringDocApp130Test extends AbstractSpringDocTest {
34+
35+
@SpringBootApplication
36+
static class SpringDocTestApp {}
37+
38+
}

0 commit comments

Comments
 (0)