Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 #1258
- Loading branch information
1 parent
dfe0327
commit b838cce
Showing
4 changed files
with
178 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
.../src/main/java/springfox/documentation/schema/property/SimpleMethodSignatureEquality.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* | ||
* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
*/ | ||
package springfox.documentation.schema.property; | ||
|
||
import com.google.common.base.Equivalence; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
class SimpleMethodSignatureEquality extends Equivalence<Method> { | ||
|
||
@Override | ||
protected boolean doEquivalent(Method first, Method other) { | ||
return first.getName().equals(other.getName()) | ||
&& first.getReturnType().equals(other.getReturnType()) | ||
&& equalParamTypes(first.getParameterTypes(), other.getParameterTypes()); | ||
} | ||
|
||
private boolean equalParamTypes(Class<?>[] params1, Class<?>[] params2) { | ||
if (params1.length == params2.length) { | ||
for (int i = 0; i < params1.length; i++) { | ||
if (params1[i] != params2[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
protected int doHash(Method method) { | ||
return method.hashCode(); | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
...t/groovy/springfox/documentation/schema/property/SimpleMethodSignatureEqualitySpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* | ||
* Copyright 2016 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
* | ||
*/ | ||
package springfox.documentation.schema.property | ||
|
||
import spock.lang.Specification | ||
|
||
class SimpleMethodSignatureEqualitySpec extends Specification { | ||
def "detects method equality on different classes"() { | ||
given: | ||
def sut = new SimpleMethodSignatureEquality() | ||
def m1 = SwaggerBugRequest.methods.find { | ||
m -> "setInvisibleField".equals(m.name) && | ||
1 == m.parameterTypes.length && | ||
String.class.equals(m.parameterTypes[0])} | ||
def m2 = SwaggerBugIF.methods.find { m -> "setInvisibleField".equals(m.name) } | ||
expect: | ||
sut.equivalent(m1, m2) | ||
} | ||
|
||
def "detects method inequality on different classes"() { | ||
given: | ||
def sut = new SimpleMethodSignatureEquality() | ||
def m1 = SwaggerBugRequest.methods.find { m -> "setVisibleField".equals(m.name) } | ||
def m2 = SwaggerBugIF.methods.find { m -> "setInvisibleField".equals(m.name) } | ||
expect: | ||
!sut.equivalent(m1, m2) | ||
} | ||
|
||
def "detects method inequality on different classes when parameter count differs"() { | ||
given: | ||
def sut = new SimpleMethodSignatureEquality() | ||
def m1 = SwaggerBugRequest.methods.find { m -> "setInvisibleField".equals(m.name) && 1 == m.parameterTypes.length } | ||
def m2 = SwaggerBugExtended.methods.find { m -> "setInvisibleField".equals(m.name) && 2 == m.parameterTypes.length } | ||
expect: | ||
!sut.equivalent(m1, m2) | ||
} | ||
|
||
def "detects method inequality when parameter type differs"() { | ||
given: | ||
def sut = new SimpleMethodSignatureEquality() | ||
def m1 = SwaggerBugExtended.methods.find { | ||
m -> "setInvisibleField".equals(m.name) && | ||
1 == m.parameterTypes.length && | ||
String.class.equals(m.parameterTypes[0])} | ||
def m2 = SwaggerBugExtended.methods.find { | ||
m -> "setInvisibleField".equals(m.name) && | ||
1 == m.parameterTypes.length && | ||
Boolean.class.equals(m.parameterTypes[0])} | ||
expect: | ||
!sut.equivalent(m1, m2) | ||
} | ||
|
||
def "Computes hashcode"() { | ||
given: | ||
def sut = new SimpleMethodSignatureEquality() | ||
def m1 = SwaggerBugRequest.methods.find { | ||
m -> "setInvisibleField".equals(m.name) && | ||
1 == m.parameterTypes.length && | ||
String.class.equals(m.parameterTypes[0])} | ||
expect: | ||
sut.hash(m1) > 0 | ||
} | ||
|
||
|
||
class SwaggerBugRequest extends SwaggerBugExtended implements SwaggerBugIF { | ||
private String visibleField; | ||
|
||
public String getVisibleField() { | ||
return visibleField; | ||
} | ||
|
||
public void setVisibleField(String visibleField) { | ||
this.visibleField = visibleField; | ||
} | ||
} | ||
|
||
class SwaggerBugExtended { | ||
private String invisibleField; | ||
|
||
public String getInvisibleField() { | ||
return invisibleField; | ||
} | ||
|
||
public void setInvisibleField(String invisibleField) { | ||
this.invisibleField = invisibleField; | ||
} | ||
|
||
public void setInvisibleField(Boolean another) { | ||
} | ||
|
||
public void setInvisibleField(String invisibleField, boolean another) { | ||
this.invisibleField = invisibleField; | ||
} | ||
} | ||
|
||
interface SwaggerBugIF { | ||
|
||
String getVisibleField(); | ||
|
||
void setVisibleField(String visibleField); | ||
|
||
String getInvisibleField(); | ||
|
||
void setInvisibleField(String invisibleField); | ||
|
||
} | ||
} |