Skip to content

Commit e29ec31

Browse files
committed
reverting changes from PR #2788
1 parent e490ad3 commit e29ec31

File tree

3 files changed

+22
-143
lines changed

3 files changed

+22
-143
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/extractor/DelegatingMethodParameter.java

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* * * *
2222
* * *
2323
* *
24-
*
24+
*
2525
*/
2626
package org.springdoc.core.extractor;
2727

@@ -93,11 +93,6 @@ public class DelegatingMethodParameter extends MethodParameter {
9393
*/
9494
private final Annotation[] methodAnnotations;
9595

96-
/**
97-
* The annotations to mask from the list of annotations on this method parameter.
98-
*/
99-
private final Annotation[] maskedAnnotations;
100-
10196
/**
10297
* The Is not required.
10398
*/
@@ -110,19 +105,17 @@ public class DelegatingMethodParameter extends MethodParameter {
110105
* @param parameterName the parameter name
111106
* @param additionalParameterAnnotations the additional parameter annotations
112107
* @param methodAnnotations the method annotations
113-
* @param maskedAnnotations any annotations that should not be included in the final list of annotations
114108
* @param isParameterObject the is parameter object
115109
* @param isNotRequired the is required
116110
*/
117-
DelegatingMethodParameter(MethodParameter delegate, String parameterName, Annotation[] additionalParameterAnnotations, Annotation[] methodAnnotations, Annotation[] maskedAnnotations, boolean isParameterObject, boolean isNotRequired) {
111+
DelegatingMethodParameter(MethodParameter delegate, String parameterName, Annotation[] additionalParameterAnnotations, Annotation[] methodAnnotations, boolean isParameterObject, boolean isNotRequired) {
118112
super(delegate);
119113
this.delegate = delegate;
120114
this.additionalParameterAnnotations = additionalParameterAnnotations;
121115
this.parameterName = parameterName;
122116
this.isParameterObject = isParameterObject;
123117
this.isNotRequired = isNotRequired;
124-
this.methodAnnotations = methodAnnotations;
125-
this.maskedAnnotations = maskedAnnotations;
118+
this.methodAnnotations =methodAnnotations;
126119
}
127120

128121
/**
@@ -145,15 +138,15 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
145138
boolean hasNotFlatAnnotation = Arrays.stream(p.getParameterAnnotations())
146139
.anyMatch(annotation -> Arrays.asList(RequestBody.class, RequestPart.class).contains(annotation.annotationType()));
147140
if (!MethodParameterPojoExtractor.isSimpleType(paramClass)
148-
&& (hasFlatAnnotation || (defaultFlatParamObject && !hasNotFlatAnnotation && !AbstractRequestService.isRequestTypeToIgnore(paramClass)))) {
141+
&& (hasFlatAnnotation || (defaultFlatParamObject && !hasNotFlatAnnotation && !AbstractRequestService.isRequestTypeToIgnore(paramClass)))) {
149142
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(methodParameter -> {
150143
optionalDelegatingMethodParameterCustomizers.ifPresent(delegatingMethodParameterCustomizers -> delegatingMethodParameterCustomizers.forEach(customizer -> customizer.customize(p, methodParameter)));
151144
explodedParameters.add(methodParameter);
152145
});
153146
}
154147
else {
155148
String name = pNames != null ? pNames[i] : p.getParameterName();
156-
explodedParameters.add(new DelegatingMethodParameter(p, name, null, null, null, false, false));
149+
explodedParameters.add(new DelegatingMethodParameter(p, name, null, null, false, false));
157150
}
158151
}
159152
return explodedParameters.toArray(new MethodParameter[0]);
@@ -186,15 +179,7 @@ public static MethodParameter changeContainingClass(MethodParameter methodParame
186179
@NonNull
187180
public Annotation[] getParameterAnnotations() {
188181
Annotation[] methodAnnotations = ArrayUtils.addAll(delegate.getParameterAnnotations(), this.methodAnnotations);
189-
methodAnnotations = ArrayUtils.addAll(methodAnnotations, additionalParameterAnnotations);
190-
if (maskedAnnotations == null) {
191-
return methodAnnotations;
192-
} else {
193-
List<Annotation> maskedAnnotationList = List.of(maskedAnnotations);
194-
return Arrays.stream(methodAnnotations)
195-
.filter(annotation -> !maskedAnnotationList.contains(annotation))
196-
.toArray(Annotation[]::new);
197-
}
182+
return ArrayUtils.addAll(methodAnnotations, additionalParameterAnnotations);
198183
}
199184

200185
@Override

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/extractor/MethodParameterPojoExtractor.java

Lines changed: 13 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* * * *
2222
* * *
2323
* *
24-
*
24+
*
2525
*/
2626
package org.springdoc.core.extractor;
2727

@@ -50,13 +50,10 @@
5050
import java.util.concurrent.atomic.AtomicInteger;
5151
import java.util.concurrent.atomic.AtomicLong;
5252
import java.util.function.Predicate;
53-
import java.util.stream.Collectors;
5453
import java.util.stream.Stream;
5554

5655
import io.swagger.v3.core.util.PrimitiveType;
5756
import io.swagger.v3.oas.annotations.Parameter;
58-
import io.swagger.v3.oas.annotations.media.Schema;
59-
import org.springdoc.core.service.AbstractRequestService;
6057

6158
import org.springframework.core.GenericTypeResolver;
6259
import org.springframework.core.MethodParameter;
@@ -66,7 +63,7 @@
6663
/**
6764
* The type Method parameter pojo extractor.
6865
*
69-
* @author bnasslahsen, michael.clarke
66+
* @author bnasslahsen
7067
*/
7168
public class MethodParameterPojoExtractor {
7269

@@ -116,21 +113,20 @@ private MethodParameterPojoExtractor() {
116113
* @return the stream
117114
*/
118115
static Stream<MethodParameter> extractFrom(Class<?> clazz) {
119-
return extractFrom(clazz, "", true);
116+
return extractFrom(clazz, "");
120117
}
121118

122119
/**
123120
* Extract from stream.
124121
*
125122
* @param clazz the clazz
126123
* @param fieldNamePrefix the field name prefix
127-
* @param parentRequired whether the field that hold the class currently being inspected was required or optional
128124
* @return the stream
129125
*/
130-
private static Stream<MethodParameter> extractFrom(Class<?> clazz, String fieldNamePrefix, boolean parentRequired) {
126+
private static Stream<MethodParameter> extractFrom(Class<?> clazz, String fieldNamePrefix) {
131127
return allFieldsOf(clazz).stream()
132128
.filter(field -> !field.getType().equals(clazz))
133-
.flatMap(f -> fromGetterOfField(clazz, f, fieldNamePrefix, parentRequired))
129+
.flatMap(f -> fromGetterOfField(clazz, f, fieldNamePrefix))
134130
.filter(Objects::nonNull);
135131
}
136132

@@ -140,96 +136,20 @@ private static Stream<MethodParameter> extractFrom(Class<?> clazz, String fieldN
140136
* @param paramClass the param class
141137
* @param field the field
142138
* @param fieldNamePrefix the field name prefix
143-
* @param parentRequired whether the field that holds the class currently being examined was required or optional
144139
* @return the stream
145140
*/
146-
private static Stream<MethodParameter> fromGetterOfField(Class<?> paramClass, Field field, String fieldNamePrefix, boolean parentRequired) {
141+
private static Stream<MethodParameter> fromGetterOfField(Class<?> paramClass, Field field, String fieldNamePrefix) {
147142
Class<?> type = extractType(paramClass, field);
148143

149144
if (Objects.isNull(type))
150145
return Stream.empty();
151146

152147
if (isSimpleType(type))
153-
return fromSimpleClass(paramClass, field, fieldNamePrefix, parentRequired);
148+
return fromSimpleClass(paramClass, field, fieldNamePrefix);
154149
else {
155-
Parameter parameter = field.getAnnotation(Parameter.class);
156-
Schema schema = field.getAnnotation(Schema.class);
157-
boolean visible = resolveVisible(parameter, schema);
158-
if (!visible) {
159-
return Stream.empty();
160-
}
161-
String prefix = fieldNamePrefix + resolveName(parameter, schema).orElse(field.getName()) + DOT;
162-
Set<String> annotationSimpleNames = Arrays.stream(field.getDeclaredAnnotations())
163-
.map(Annotation::annotationType)
164-
.map(Class::getSimpleName)
165-
.collect(Collectors.toSet());
166-
boolean notNullAnnotationsPresent = AbstractRequestService.hasNotNullAnnotation(annotationSimpleNames);
167-
return extractFrom(type, prefix, resolveRequired(schema, parameter, !notNullAnnotationsPresent));
168-
}
169-
}
170-
171-
private static Optional<String> resolveName(Parameter parameter, Schema schema) {
172-
if (parameter != null) {
173-
return resolveNameFromParameter(parameter);
174-
}
175-
if (schema != null) {
176-
return resolveNameFromSchema(schema);
177-
}
178-
return Optional.empty();
179-
}
180-
181-
private static Optional<String> resolveNameFromParameter(Parameter parameter) {
182-
if (parameter.name().isEmpty()) {
183-
return Optional.empty();
184-
}
185-
return Optional.of(parameter.name());
186-
}
187-
188-
private static Optional<String> resolveNameFromSchema(Schema schema) {
189-
if (schema.name().isEmpty()) {
190-
return Optional.empty();
150+
String prefix = fieldNamePrefix + field.getName() + DOT;
151+
return extractFrom(type, prefix);
191152
}
192-
return Optional.of(schema.name());
193-
}
194-
195-
private static boolean resolveVisible(Parameter parameter, Schema schema) {
196-
if (parameter != null) {
197-
return !parameter.hidden();
198-
}
199-
if (schema != null) {
200-
return !schema.hidden();
201-
}
202-
return true;
203-
}
204-
205-
private static boolean resolveRequired(Schema schema, Parameter parameter, boolean nullable) {
206-
if (parameter != null) {
207-
return resolveRequiredFromParameter(parameter, nullable);
208-
}
209-
if (schema != null) {
210-
return resolveRequiredFromSchema(schema, nullable);
211-
}
212-
return !nullable;
213-
}
214-
215-
private static boolean resolveRequiredFromParameter(Parameter parameter, boolean nullable) {
216-
if (parameter.required()) {
217-
return true;
218-
}
219-
return !nullable;
220-
}
221-
222-
private static boolean resolveRequiredFromSchema(Schema schema, boolean nullable) {
223-
if (schema.required()) {
224-
return true;
225-
}
226-
else if (schema.requiredMode() == Schema.RequiredMode.REQUIRED) {
227-
return true;
228-
}
229-
else if (schema.requiredMode() == Schema.RequiredMode.NOT_REQUIRED) {
230-
return false;
231-
}
232-
return !nullable;
233153
}
234154

235155
/**
@@ -261,32 +181,18 @@ private static Class<?> extractType(Class<?> paramClass, Field field) {
261181
* @param fieldNamePrefix the field name prefix
262182
* @return the stream
263183
*/
264-
private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Field field, String fieldNamePrefix, boolean isParentRequired) {
184+
private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Field field, String fieldNamePrefix) {
265185
Annotation[] fieldAnnotations = field.getDeclaredAnnotations();
266186
try {
267187
Parameter parameter = field.getAnnotation(Parameter.class);
268-
Schema schema = field.getAnnotation(Schema.class);
269-
boolean visible = resolveVisible(parameter, schema);
270-
if (!visible) {
271-
return Stream.empty();
272-
}
273-
274-
Set<String> annotationSimpleNames = Arrays.stream(field.getDeclaredAnnotations())
275-
.map(Annotation::annotationType)
276-
.map(Class::getSimpleName)
277-
.collect(Collectors.toSet());
278-
279-
boolean isNotRequired = !resolveRequired(schema, parameter, !AbstractRequestService.hasNotNullAnnotation(annotationSimpleNames));
280-
Annotation[] notNullFieldAnnotations = Arrays.stream(fieldAnnotations)
281-
.filter(annotation -> AbstractRequestService.hasNotNullAnnotation(List.of(annotation.annotationType().getSimpleName())))
282-
.toArray(Annotation[]::new);
188+
boolean isNotRequired = parameter == null || !parameter.required();
283189
if (paramClass.getSuperclass() != null && paramClass.isRecord()) {
284190
return Stream.of(paramClass.getRecordComponents())
285191
.filter(d -> d.getName().equals(field.getName()))
286192
.map(RecordComponent::getAccessor)
287193
.map(method -> new MethodParameter(method, -1))
288194
.map(methodParameter -> DelegatingMethodParameter.changeContainingClass(methodParameter, paramClass))
289-
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), fieldAnnotations, param.getMethodAnnotations(), notNullFieldAnnotations, true, isNotRequired));
195+
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), fieldAnnotations, param.getMethodAnnotations(), true, isNotRequired));
290196

291197
}
292198
else
@@ -296,7 +202,7 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
296202
.filter(Objects::nonNull)
297203
.map(method -> new MethodParameter(method, -1))
298204
.map(methodParameter -> DelegatingMethodParameter.changeContainingClass(methodParameter, paramClass))
299-
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), fieldAnnotations, param.getMethodAnnotations(), notNullFieldAnnotations, true, isNotRequired));
205+
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), fieldAnnotations, param.getMethodAnnotations(), true, isNotRequired));
300206
}
301207
catch (IntrospectionException e) {
302208
return Stream.of();

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -584,9 +584,6 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
584584
if (parameter.getRequired() == null)
585585
parameter.setRequired(parameterInfo.isRequired());
586586

587-
if (Boolean.TRUE.equals(parameter.getRequired()) && parameterInfo.getMethodParameter() instanceof DelegatingMethodParameter delegatingMethodParameter && delegatingMethodParameter.isNotRequired())
588-
parameter.setRequired(false);
589-
590587
if (containsDeprecatedAnnotation(parameterInfo.getMethodParameter().getParameterAnnotations()))
591588
parameter.setDeprecated(true);
592589

@@ -619,7 +616,7 @@ public void applyBeanValidatorAnnotations(final Parameter parameter, final List<
619616
Map<String, Annotation> annos = new HashMap<>();
620617
if (annotations != null)
621618
annotations.forEach(annotation -> annos.put(annotation.annotationType().getSimpleName(), annotation));
622-
boolean annotationExists = hasNotNullAnnotation(annos.keySet());
619+
boolean annotationExists = Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annos::containsKey);
623620
if (annotationExists)
624621
parameter.setRequired(true);
625622
Schema<?> schema = parameter.getSchema();
@@ -646,7 +643,7 @@ public void applyBeanValidatorAnnotations(final RequestBody requestBody, final L
646643
.filter(annotation -> io.swagger.v3.oas.annotations.parameters.RequestBody.class.equals(annotation.annotationType()))
647644
.anyMatch(annotation -> ((io.swagger.v3.oas.annotations.parameters.RequestBody) annotation).required());
648645
}
649-
boolean validationExists = hasNotNullAnnotation(annos.keySet());
646+
boolean validationExists = Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annos::containsKey);
650647

651648
if (validationExists || (!isOptional && (springRequestBodyRequired || swaggerRequestBodyRequired)))
652649
requestBody.setRequired(true);
@@ -843,14 +840,5 @@ else if (requestBody.content().length > 0)
843840
}
844841
return false;
845842
}
846-
847-
/**
848-
* Check if the parameter has any of the annotations that make it non-optional
849-
*
850-
* @param annotationSimpleNames the annotation simple class named, e.g. NotNull
851-
* @return whether any of the known NotNull annotations are present
852-
*/
853-
public static boolean hasNotNullAnnotation(Collection<String> annotationSimpleNames) {
854-
return Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annotationSimpleNames::contains);
855-
}
843+
856844
}

0 commit comments

Comments
 (0)