Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getter is not detected correctly when method name is identical to variable name #202

Closed
HoaBo opened this issue Apr 15, 2015 · 3 comments
Closed

Comments

@HoaBo
Copy link

HoaBo commented Apr 15, 2015

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.ObjectWriter
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import org.junit.Test

class JsonSchemaTest {
  @Test
  def test(): Unit = {
    val mapper = new ObjectMapper
    mapper.registerModule(DefaultScalaModule)
    val generator = new JsonSchemaGenerator(mapper)
    val schema = generator.generateSchema(classOf[TestClass])

    val writer: ObjectWriter = mapper.writerWithDefaultPrettyPrinter().asInstanceOf[ObjectWriter]
    val schemaString = writer.writeValueAsString(schema)
    println(schemaString)
  }

  class TestClass {
    var firstName: String = ""
    var lastName: String = ""

    def firstName(firstName: String) { this.firstName = firstName }
    def lastName(lastName: String) { this.lastName = lastName }
  }
}

The result sometimes doesn't contain any properties, sometimes only contain firstName. I tried to debug and it is because the getter is not detected correctly because we have 2 methods with the same name

void lastName(String lastName) // generated from def lastName
String lastName() // generated from var lastName

The code to detect the getter

def findMethod(cls: Class[_], name: String): Option[Method] =
      listMethods(cls).filter(isNotSyntheticOrBridge).find(m => NameTransformer.decode(m.getName) == name).headOption
def findGetter(cls: Class[_], propertyName: String): Option[Method] = {
      findMethod(cls, propertyName).filter(isAcceptableGetter)    }

Unfortunately, findMethod returns the first one, which is refused to be a getter later by filter(isAcceptableGetter)

I fixed this by

def findMethodWithFilter(cls: Class[_], name: String, additionalFilter: Method => Boolean): Option[Method] =
      listMethods(cls).filter(isNotSyntheticOrBridge)
        .filter(additionalFilter)
        .find(m => NameTransformer.decode(m.getName) == name).headOption
def findGetter(cls: Class[_], propertyName: String): Option[Method] = {
      findMethodWithFilter(cls, propertyName, isAcceptableGetter)
    }

Please consider rolling this into master :)

@christophercurrie
Copy link
Member

I presume you're running under Java 7+? Non-deterministic behavior of reflection is one of the "features" of Java 7, and I can't repro the use case using Java 6.

Thanks for the report. I should have a fix similar to your suggesting integrated soon.

christophercurrie added a commit that referenced this issue Apr 16, 2015
@HoaBo
Copy link
Author

HoaBo commented Apr 16, 2015

Yes, I'm running under Java 8.

christophercurrie added a commit that referenced this issue Apr 16, 2015
@christophercurrie
Copy link
Member

Fix has been merged into master and 2.5, and will be in the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants