Skip to content

Commit b838cce

Browse files
committed
Updated the method comparison to not be naive
Currently it checks for method equality, but this can sometimes not be correct as the resolved method and the target of comparison might be on different classes in the class inheritance chain. fixes springfox#1258
1 parent dfe0327 commit b838cce

File tree

4 files changed

+178
-3
lines changed

4 files changed

+178
-3
lines changed

config/apache-copyright.header

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
^\Q/*\E$
22
^\Q *\E$
3-
^\Q * Copyright \E\d\d\d\d\Q the original author or authors.\E$
3+
^\Q * Copyright \E(20\d\d\-)?20\d\d\Q the original author or authors.\E$
44
^\Q *\E$
55
^\Q * Licensed under the Apache License, Version 2.0 (the "License");\E$
66
^\Q * you may not use this file except in compliance with the License.\E$

springfox-schema/src/main/java/springfox/documentation/schema/property/OptimizedModelPropertiesProvider.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/*
22
*
3-
* Copyright 2015 the original author or authors.
3+
* Copyright 2015-2016 the original author or authors.
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License");
66
* you may not use this file except in compliance with the License.
@@ -58,6 +58,7 @@
5858
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
5959

6060
import java.lang.annotation.Annotation;
61+
import java.lang.reflect.Method;
6162
import java.util.ArrayList;
6263
import java.util.Comparator;
6364
import java.util.List;
@@ -321,7 +322,8 @@ private ModelProperty paramModelProperty(
321322
private Optional<ResolvedMethod> findAccessorMethod(ResolvedType resolvedType, final AnnotatedMember member) {
322323
return tryFind(accessors.in(resolvedType), new Predicate<ResolvedMethod>() {
323324
public boolean apply(ResolvedMethod accessorMethod) {
324-
return accessorMethod.getRawMember().equals(member.getMember());
325+
SimpleMethodSignatureEquality methodComparer = new SimpleMethodSignatureEquality();
326+
return methodComparer.equivalent(accessorMethod.getRawMember(), (Method) member.getMember());
325327
}
326328
});
327329
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
*
3+
* Copyright 2016 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+
* http://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 springfox.documentation.schema.property;
20+
21+
import com.google.common.base.Equivalence;
22+
23+
import java.lang.reflect.Method;
24+
25+
class SimpleMethodSignatureEquality extends Equivalence<Method> {
26+
27+
@Override
28+
protected boolean doEquivalent(Method first, Method other) {
29+
return first.getName().equals(other.getName())
30+
&& first.getReturnType().equals(other.getReturnType())
31+
&& equalParamTypes(first.getParameterTypes(), other.getParameterTypes());
32+
}
33+
34+
private boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) {
35+
if (params1.length == params2.length) {
36+
for (int i = 0; i < params1.length; i++) {
37+
if (params1[i] != params2[i]) {
38+
return false;
39+
}
40+
}
41+
return true;
42+
}
43+
return false;
44+
}
45+
46+
@Override
47+
protected int doHash(Method method) {
48+
return method.hashCode();
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/*
2+
*
3+
* Copyright 2016 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+
* http://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 springfox.documentation.schema.property
20+
21+
import spock.lang.Specification
22+
23+
class SimpleMethodSignatureEqualitySpec extends Specification {
24+
def "detects method equality on different classes"() {
25+
given:
26+
def sut = new SimpleMethodSignatureEquality()
27+
def m1 = SwaggerBugRequest.methods.find {
28+
m -> "setInvisibleField".equals(m.name) &&
29+
1 == m.parameterTypes.length &&
30+
String.class.equals(m.parameterTypes[0])}
31+
def m2 = SwaggerBugIF.methods.find { m -> "setInvisibleField".equals(m.name) }
32+
expect:
33+
sut.equivalent(m1, m2)
34+
}
35+
36+
def "detects method inequality on different classes"() {
37+
given:
38+
def sut = new SimpleMethodSignatureEquality()
39+
def m1 = SwaggerBugRequest.methods.find { m -> "setVisibleField".equals(m.name) }
40+
def m2 = SwaggerBugIF.methods.find { m -> "setInvisibleField".equals(m.name) }
41+
expect:
42+
!sut.equivalent(m1, m2)
43+
}
44+
45+
def "detects method inequality on different classes when parameter count differs"() {
46+
given:
47+
def sut = new SimpleMethodSignatureEquality()
48+
def m1 = SwaggerBugRequest.methods.find { m -> "setInvisibleField".equals(m.name) && 1 == m.parameterTypes.length }
49+
def m2 = SwaggerBugExtended.methods.find { m -> "setInvisibleField".equals(m.name) && 2 == m.parameterTypes.length }
50+
expect:
51+
!sut.equivalent(m1, m2)
52+
}
53+
54+
def "detects method inequality when parameter type differs"() {
55+
given:
56+
def sut = new SimpleMethodSignatureEquality()
57+
def m1 = SwaggerBugExtended.methods.find {
58+
m -> "setInvisibleField".equals(m.name) &&
59+
1 == m.parameterTypes.length &&
60+
String.class.equals(m.parameterTypes[0])}
61+
def m2 = SwaggerBugExtended.methods.find {
62+
m -> "setInvisibleField".equals(m.name) &&
63+
1 == m.parameterTypes.length &&
64+
Boolean.class.equals(m.parameterTypes[0])}
65+
expect:
66+
!sut.equivalent(m1, m2)
67+
}
68+
69+
def "Computes hashcode"() {
70+
given:
71+
def sut = new SimpleMethodSignatureEquality()
72+
def m1 = SwaggerBugRequest.methods.find {
73+
m -> "setInvisibleField".equals(m.name) &&
74+
1 == m.parameterTypes.length &&
75+
String.class.equals(m.parameterTypes[0])}
76+
expect:
77+
sut.hash(m1) > 0
78+
}
79+
80+
81+
class SwaggerBugRequest extends SwaggerBugExtended implements SwaggerBugIF {
82+
private String visibleField;
83+
84+
public String getVisibleField() {
85+
return visibleField;
86+
}
87+
88+
public void setVisibleField(String visibleField) {
89+
this.visibleField = visibleField;
90+
}
91+
}
92+
93+
class SwaggerBugExtended {
94+
private String invisibleField;
95+
96+
public String getInvisibleField() {
97+
return invisibleField;
98+
}
99+
100+
public void setInvisibleField(String invisibleField) {
101+
this.invisibleField = invisibleField;
102+
}
103+
104+
public void setInvisibleField(Boolean another) {
105+
}
106+
107+
public void setInvisibleField(String invisibleField, boolean another) {
108+
this.invisibleField = invisibleField;
109+
}
110+
}
111+
112+
interface SwaggerBugIF {
113+
114+
String getVisibleField();
115+
116+
void setVisibleField(String visibleField);
117+
118+
String getInvisibleField();
119+
120+
void setInvisibleField(String invisibleField);
121+
122+
}
123+
}

0 commit comments

Comments
 (0)