Skip to content

Commit 785f0be

Browse files
author
Marcin Zarebski
committed
Refactor: move parameter object field extraction to separate class
1 parent f085e71 commit 785f0be

File tree

2 files changed

+103
-50
lines changed

2 files changed

+103
-50
lines changed

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

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,30 @@
11
package org.springdoc.core;
22

3-
import java.beans.IntrospectionException;
4-
import java.beans.Introspector;
5-
import java.beans.PropertyDescriptor;
63
import java.lang.annotation.Annotation;
74
import java.lang.reflect.AnnotatedElement;
85
import java.lang.reflect.Constructor;
96
import java.lang.reflect.Executable;
10-
import java.lang.reflect.Field;
117
import java.lang.reflect.Member;
128
import java.lang.reflect.Method;
139
import java.lang.reflect.Type;
1410
import java.util.ArrayList;
1511
import java.util.Arrays;
1612
import java.util.List;
1713
import java.util.Objects;
18-
import java.util.stream.Stream;
1914

20-
import io.swagger.v3.oas.annotations.Parameter;
2115
import org.apache.commons.lang3.ArrayUtils;
2216
import org.springdoc.api.annotations.ParameterObject;
2317
import org.springdoc.core.converters.AdditionalModelsConverter;
2418

2519
import org.springframework.core.MethodParameter;
2620
import org.springframework.core.ParameterNameDiscoverer;
2721
import org.springframework.lang.NonNull;
28-
import org.springframework.lang.Nullable;
2922

3023
/**
3124
* @author zarebski.m
3225
*/
3326
class DelegatingMethodParameter extends MethodParameter {
27+
3428
private MethodParameter delegate;
3529

3630
private Annotation[] additionalParameterAnnotations;
@@ -50,10 +44,7 @@ public static MethodParameter[] customize(String[] pNames, MethodParameter[] par
5044
MethodParameter p = parameters[i];
5145
if (p.hasParameterAnnotation(ParameterObject.class)) {
5246
Class<?> paramClass = AdditionalModelsConverter.getReplacement(p.getParameterType());
53-
allFieldsOf(paramClass).stream()
54-
.map(f -> fromGetterOfField(paramClass, f))
55-
.filter(Objects::nonNull)
56-
.forEach(explodedParameters::add);
47+
MethodParameterPojoExtractor.extractFrom(paramClass).forEach(explodedParameters::add);
5748
}
5849
else {
5950
String name = pNames != null ? pNames[i] : p.getParameterName();
@@ -139,31 +130,6 @@ public void initParameterNameDiscovery(ParameterNameDiscoverer parameterNameDisc
139130
delegate.initParameterNameDiscovery(parameterNameDiscoverer);
140131
}
141132

142-
@Nullable
143-
static MethodParameter fromGetterOfField(Class<?> paramClass, Field field) {
144-
try {
145-
Annotation[] filedAnnotations = field.getDeclaredAnnotations();
146-
Parameter parameter = field.getAnnotation(Parameter.class);
147-
if (parameter != null && !parameter.required()) {
148-
Field fieldNullable = NullableFieldClass.class.getDeclaredField("nullableField");
149-
Annotation annotation = fieldNullable.getAnnotation(Nullable.class);
150-
filedAnnotations = ArrayUtils.add(filedAnnotations, annotation);
151-
}
152-
Annotation[] filedAnnotationsNew = filedAnnotations;
153-
return Stream.of(Introspector.getBeanInfo(paramClass).getPropertyDescriptors())
154-
.filter(d -> d.getName().equals(field.getName()))
155-
.map(PropertyDescriptor::getReadMethod)
156-
.filter(Objects::nonNull)
157-
.findFirst()
158-
.map(method -> new MethodParameter(method, -1))
159-
.map(param -> new DelegatingMethodParameter(param, field.getName(), filedAnnotationsNew))
160-
.orElse(null);
161-
}
162-
catch (IntrospectionException | NoSuchFieldException e) {
163-
return null;
164-
}
165-
}
166-
167133
@Override
168134
public boolean equals(Object o) {
169135
if (this == o) return true;
@@ -181,18 +147,4 @@ public int hashCode() {
181147
result = 31 * result + Arrays.hashCode(additionalParameterAnnotations);
182148
return result;
183149
}
184-
185-
private class NullableFieldClass {
186-
@Nullable
187-
private String nullableField;
188-
}
189-
190-
private static List<Field> allFieldsOf(Class<?> clazz) {
191-
List<Field> fields = new ArrayList<>();
192-
do {
193-
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
194-
clazz = clazz.getSuperclass();
195-
} while (clazz != null);
196-
return fields;
197-
}
198150
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package org.springdoc.core;
2+
3+
import java.beans.IntrospectionException;
4+
import java.beans.Introspector;
5+
import java.beans.PropertyDescriptor;
6+
import java.lang.annotation.Annotation;
7+
import java.lang.reflect.Field;
8+
import java.util.ArrayList;
9+
import java.util.Arrays;
10+
import java.util.Collections;
11+
import java.util.HashSet;
12+
import java.util.List;
13+
import java.util.Objects;
14+
import java.util.Set;
15+
import java.util.stream.Stream;
16+
17+
import io.swagger.v3.oas.annotations.Parameter;
18+
import org.apache.commons.lang3.ArrayUtils;
19+
20+
import org.springframework.core.MethodParameter;
21+
import org.springframework.lang.NonNull;
22+
import org.springframework.lang.Nullable;
23+
24+
class MethodParameterPojoExtractor {
25+
private static final Set<Class<?>> SIMPLE_TYPES;
26+
private static final Set<Class<?>> COLLECTION_TYPES;
27+
28+
static {
29+
Set<Class<?>> simpleTypes = new HashSet<>();
30+
simpleTypes.add(boolean.class);
31+
simpleTypes.add(char.class);
32+
simpleTypes.add(byte.class);
33+
simpleTypes.add(short.class);
34+
simpleTypes.add(int.class);
35+
simpleTypes.add(long.class);
36+
simpleTypes.add(float.class);
37+
simpleTypes.add(double.class);
38+
39+
simpleTypes.add(Boolean.class);
40+
simpleTypes.add(Character.class);
41+
simpleTypes.add(Byte.class);
42+
simpleTypes.add(Short.class);
43+
simpleTypes.add(Integer.class);
44+
simpleTypes.add(Long.class);
45+
simpleTypes.add(Float.class);
46+
simpleTypes.add(Double.class);
47+
48+
simpleTypes.add(CharSequence.class);
49+
simpleTypes.add(Number.class);
50+
51+
SIMPLE_TYPES = Collections.unmodifiableSet(simpleTypes);
52+
53+
Set<Class<?>> collectionTypes = new HashSet<>();
54+
collectionTypes.add(Iterable.class);
55+
56+
COLLECTION_TYPES = Collections.unmodifiableSet(collectionTypes);
57+
}
58+
59+
private static final Nullable NULLABLE_ANNOTATION = new Nullable() {
60+
@Override
61+
public Class<? extends Annotation> annotationType() {
62+
return Nullable.class;
63+
}
64+
};
65+
66+
@NonNull
67+
static Stream<MethodParameter> extractFrom(Class<?> clazz) {
68+
return allFieldsOf(clazz).stream()
69+
.flatMap(f -> fromGetterOfField(clazz, f))
70+
.filter(Objects::nonNull);
71+
}
72+
73+
private static Stream<MethodParameter> fromGetterOfField(Class<?> paramClass, Field field) {
74+
try {
75+
Annotation[] filedAnnotations = field.getDeclaredAnnotations();
76+
Parameter parameter = field.getAnnotation(Parameter.class);
77+
if (parameter != null && !parameter.required()) {
78+
filedAnnotations = ArrayUtils.add(filedAnnotations, NULLABLE_ANNOTATION);
79+
}
80+
Annotation[] filedAnnotationsNew = filedAnnotations;
81+
return Stream.of(Introspector.getBeanInfo(paramClass).getPropertyDescriptors())
82+
.filter(d -> d.getName().equals(field.getName()))
83+
.map(PropertyDescriptor::getReadMethod)
84+
.filter(Objects::nonNull)
85+
.map(method -> new MethodParameter(method, -1))
86+
.map(param -> new DelegatingMethodParameter(param, field.getName(), filedAnnotationsNew));
87+
}
88+
catch (IntrospectionException e) {
89+
return Stream.of();
90+
}
91+
}
92+
93+
private static List<Field> allFieldsOf(Class<?> clazz) {
94+
List<Field> fields = new ArrayList<>();
95+
do {
96+
fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
97+
clazz = clazz.getSuperclass();
98+
} while (clazz != null);
99+
return fields;
100+
}
101+
}

0 commit comments

Comments
 (0)