Skip to content

Commit ddf6738

Browse files
authored
Merge pull request #1855 from uc4w6c/fix_record_field_get
Fixed a bug that duplicate field were get for record classes
2 parents eeeae9c + a3c5c96 commit ddf6738

File tree

2 files changed

+94
-1
lines changed

2 files changed

+94
-1
lines changed

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,9 @@ private static Stream<MethodParameter> fromSimpleClass(Class<?> paramClass, Fiel
181181
boolean isNotRequired = parameter == null || !parameter.required();
182182
Annotation[] finalFieldAnnotations = fieldAnnotations;
183183
if (paramClass.isRecord()) {
184-
return Stream.of( paramClass.getRecordComponents()).map(recordComponent -> recordComponent.getAccessor())
184+
return Stream.of(paramClass.getRecordComponents())
185+
.filter(d -> d.getName().equals(field.getName()))
186+
.map(recordComponent -> recordComponent.getAccessor())
185187
.map(method -> new MethodParameter(method, -1))
186188
.map(methodParameter -> DelegatingMethodParameter.changeContainingClass(methodParameter, paramClass))
187189
.map(param -> new DelegatingMethodParameter(param, fieldNamePrefix + field.getName(), finalFieldAnnotations, true, isNotRequired));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
/*
2+
*
3+
* *
4+
* * *
5+
* * * *
6+
* * * * * Copyright 2019-2022 the original author or authors.
7+
* * * * *
8+
* * * * * Licensed under the Apache License, Version 2.0 (the "License");
9+
* * * * * you may not use this file except in compliance with the License.
10+
* * * * * You may obtain a copy of the License at
11+
* * * * *
12+
* * * * * https://www.apache.org/licenses/LICENSE-2.0
13+
* * * * *
14+
* * * * * Unless required by applicable law or agreed to in writing, software
15+
* * * * * distributed under the License is distributed on an "AS IS" BASIS,
16+
* * * * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17+
* * * * * See the License for the specific language governing permissions and
18+
* * * * * limitations under the License.
19+
* * * *
20+
* * *
21+
* *
22+
*
23+
*/
24+
package org.springdoc.core.extractor;
25+
26+
import java.lang.reflect.Method;
27+
import java.util.stream.Stream;
28+
29+
import org.junit.jupiter.api.Nested;
30+
import org.junit.jupiter.api.Test;
31+
32+
import org.springframework.core.MethodParameter;
33+
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
36+
/**
37+
* Tests for {@link MethodParameterPojoExtractor}.
38+
*/
39+
class MethodParameterPojoExtractorTest {
40+
/**
41+
* Tests for {@link MethodParameterPojoExtractor#extractFrom(Class<?>)}.
42+
*/
43+
@Nested
44+
class extractFrom {
45+
@Test
46+
void ifRecordObjectShouldGetField() {
47+
Stream<MethodParameter> actual = MethodParameterPojoExtractor.extractFrom(RecordObject.class);
48+
assertThat(actual)
49+
.extracting(MethodParameter::getMethod)
50+
.extracting(Method::getName)
51+
.containsOnlyOnce("email", "firstName", "lastName");
52+
}
53+
54+
@Test
55+
void ifClassObjectShouldGetMethod() {
56+
Stream<MethodParameter> actual = MethodParameterPojoExtractor.extractFrom(ClassObject.class);
57+
assertThat(actual)
58+
.extracting(MethodParameter::getMethod)
59+
.extracting(Method::getName)
60+
.containsOnlyOnce("getEmail", "getFirstName", "getLastName");
61+
}
62+
63+
public record RecordObject(String email, String firstName, String lastName) {}
64+
65+
public class ClassObject {
66+
private String email;
67+
68+
private String firstName;
69+
70+
private String lastName;
71+
72+
public ClassObject(String email, String firstName, String lastName) {
73+
this.email = email;
74+
this.firstName = firstName;
75+
this.lastName = lastName;
76+
}
77+
78+
public String getEmail() {
79+
return email;
80+
}
81+
82+
public String getFirstName() {
83+
return firstName;
84+
}
85+
86+
public String getLastName() {
87+
return lastName;
88+
}
89+
}
90+
}
91+
}

0 commit comments

Comments
 (0)