Skip to content

Commit

Permalink
Updated the method comparison to not be naive
Browse files Browse the repository at this point in the history
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
dilipkrish committed May 29, 2016
1 parent dfe0327 commit b838cce
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/apache-copyright.header
@@ -1,6 +1,6 @@
^\Q/*\E$
^\Q *\E$
^\Q * Copyright \E\d\d\d\d\Q the original author or authors.\E$
^\Q * Copyright \E(20\d\d\-)?20\d\d\Q the original author or authors.\E$
^\Q *\E$
^\Q * Licensed under the Apache License, Version 2.0 (the "License");\E$
^\Q * you may not use this file except in compliance with the License.\E$
Expand Down
@@ -1,6 +1,6 @@
/*
*
* Copyright 2015 the original author or authors.
* Copyright 2015-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.
Expand Down Expand Up @@ -58,6 +58,7 @@
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
Expand Down Expand Up @@ -321,7 +322,8 @@ private ModelProperty paramModelProperty(
private Optional<ResolvedMethod> findAccessorMethod(ResolvedType resolvedType, final AnnotatedMember member) {
return tryFind(accessors.in(resolvedType), new Predicate<ResolvedMethod>() {
public boolean apply(ResolvedMethod accessorMethod) {
return accessorMethod.getRawMember().equals(member.getMember());
SimpleMethodSignatureEquality methodComparer = new SimpleMethodSignatureEquality();
return methodComparer.equivalent(accessorMethod.getRawMember(), (Method) member.getMember());
}
});
}
Expand Down
@@ -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();
}
}
@@ -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);

}
}

0 comments on commit b838cce

Please sign in to comment.