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

Jackson module scala potentially breaks serialization for swagger Model #454

Closed
nick-benoit14 opened this issue Jul 8, 2020 · 6 comments
Assignees
Labels

Comments

@nick-benoit14
Copy link
Contributor

Hello, running into a bit of a weird case here. Our project is in scala, but I have reproduced minimally with the code below. When I run the following

`
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new DefaultScalaModule());

    Model model = new ModelImpl();

    Property property = new IntegerProperty();

    Map<String, Property> map = new HashMap<String, Property>();
    map.put("foo", property);
    model.setProperties(map);
    System.out.println(mapper.writeValueAsString(model));

`

I end up with the following exception com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Direct self-reference leading to cycle

When I remove

mapper.registerModule(new DefaultScalaModule());

Things deserialize as I would expect. It could be that I have something misconfigured somewhere, but I am surprised that the inclusion of the DefaultScalaModule makes a difference as ModelImpl is not a scala collection or class. More details on ModelImpl: https://www.javadoc.io/doc/io.swagger/swagger-models/1.5.13/io/swagger/models/ModelImpl.html

Any thoughts?

From my minimal example:
compile "io.dropwizard:dropwizard-jackson:2.0.10" compile "io.swagger:swagger-jersey2-jaxrs:1.5.13" compile "com.fasterxml.jackson.module:jackson-module-scala_2.12:+"

@pjfanning
Copy link
Member

pjfanning commented Jul 9, 2020

swagger 1.5 is compiled with an older version of jackson and is very dependent on using the version that appears in its dependency list - arbitrarily updating jackson to latest version does not work - if you update both jackson and swagger, you'll find latest version of swagger is built with latest version of jackson

https://mvnrepository.com/artifact/io.swagger/swagger-jaxrs/1.5.23 -- depends on jackson 2.9.9
https://mvnrepository.com/artifact/io.swagger/swagger-jaxrs/1.6.1 -- depends on jackson 2.10.1
https://mvnrepository.com/artifact/io.swagger/swagger-jaxrs/1.6.2 -- depends on jackson 2.11.1

@nick-benoit14
Copy link
Contributor Author

That did it! I would have had a very hard time finding that out on my own. Thanks!

@nick-benoit14
Copy link
Contributor Author

@pjfanning I did a bit more poking around. It seems I am able to use other versions of jackson, but I still run into the error depending on my version of jackson-module-scala. Here is my build.gradle

plugins {
    id 'java'
}

group 'jackson-test-3'
version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}

dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile "com.fasterxml.jackson.core:jackson-annotations:2.10.4"
    compile "com.fasterxml.jackson.core:jackson-core:2.10.4"
    compile "com.fasterxml.jackson.core:jackson-databind:2.10.4"
    
    compile "io.swagger:swagger-jaxrs:1.6.1"
    compile "com.fasterxml.jackson.module:jackson-module-scala_2.12:2.10.1" // This works
    /*This works*/ // compile "com.fasterxml.jackson.module:jackson-module-scala_2.12:2.10.2"
    /* This does not work*/ // compile "com.fasterxml.jackson.module:jackson-module-scala_2.12:2.10.3"
    /* This does not work*/ // compile "com.fasterxml.jackson.module:jackson-module-scala_2.12:2.10.4"
}

Does it even make sense to you that jackson-module-scala would come into play serializing a non scala collection?

@nick-benoit14 nick-benoit14 reopened this Jul 9, 2020
@pjfanning
Copy link
Member

similar issue - swagger-akka-http/swagger-akka-http#171 - but I was able to code around that problem

@pjfanning
Copy link
Member

I added a test case to the 2.11 branch that reproduces the problem (SwaggerTest) - still need to find time to debug why adding the DefaultScalaModule makes the code fail

@pjfanning
Copy link
Member

I have added a hacky fix in 2.11 branch, targeted for 2.11.2 release. I modified the code to get the jackson scala module to ignore the swagger model classes. I hope I can find a better solution but this will do as a transitional fix.

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

No branches or pull requests

2 participants