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

@JsonSerialize(contentAs=superType) behavior disallowed in 2.7 #1178

Closed
markwoon opened this issue Mar 31, 2016 · 8 comments
Closed

@JsonSerialize(contentAs=superType) behavior disallowed in 2.7 #1178

markwoon opened this issue Mar 31, 2016 · 8 comments
Milestone

Comments

@markwoon
Copy link
Contributor

After updating to 2.7, I'm getting:

Failed to widen value type of [collection type; class java.util.SortedSet, contains [simple type, class org.pharmgkb.model.AccessionIdentifier]] with concrete-type annotation (value org.pharmgkb.model.BaseAccessionIdentifier), from 'getGenes': Class org.pharmgkb.model.BaseAccessionIdentifier not a super-type of [simple type, class org.pharmgkb.model.AccessionIdentifier]

The signature of the method in question is:

@JsonSerialize(contentAs = BaseAccessionIdentifier.class)
@JsonDeserialize(contentAs = BaseAccessionIdentifier.class)
public Set<AccessionIdentifier> getGenes() {
}

How am I suppose to annotate this to get it to work again?

@cowtowncoder
Copy link
Member

Sounds like earlier versions did not verify that types are compatible. Assuming BaseAccessionIdentifier is a subtype of AccessionIdentifier, @JsonSerialize here would be problematic as it can really be guaranteed to match type; but I also don't think that should be necessary should it? Serialization annotations are only typically used to limit type to a supertype, and not to try to use specialized type. In that way @JsonSerialize and @JsonDeserialize behave in opposing ways; former to force more general type, latter for more specific.

@markwoon
Copy link
Contributor Author

markwoon commented Apr 1, 2016

Our inheritance tree looks something like this:

  • AccessionIdentifier (interface)
    • BaseAccessionIdentifier
      • ClassA
      • ClassB

I'm trying to limit what gets serialized here to only the properties defined in BaseAccessionIdentifier.

Do I need to define a custom serializer in this case?

@cowtowncoder
Copy link
Member

@markwoon Hmmh. I can see that although such definition is not necessarily safe for all usage (since you could have alternative AccessionIdentifier implementations), it may be for some usage; and could just dynamically fail if so, instead of statically.

So perhaps this is a flaw after all.

@markwoon
Copy link
Contributor Author

markwoon commented Apr 1, 2016

@cowtowncoder Does that mean that this is something that might change a future release?

And what would you recommend in the mean time?

@cowtowncoder
Copy link
Member

@markwoon Right, I think I will try to fix this for 2.7.4.

In the meantime... good question. I am not sure if there is an easier way than custom serializer.
While forcing use of AccessionIdentifier would be easy (either by specifying that as content type, or marking typing as Static), getting this to use intermediate type is more difficult.

@cowtowncoder cowtowncoder changed the title @JsonSerialize behavior change from 2.6 to 2.7 @JsonSerialize(contentAs=superType) behavior disallowed in 2.7 Apr 4, 2016
@cowtowncoder cowtowncoder added this to the 2.7.0 milestone Apr 4, 2016
@cowtowncoder
Copy link
Member

I was able to fix this, will be included in 2.7.4.

@markwoon
Copy link
Contributor Author

markwoon commented May 13, 2016

@cowtowncoder Confirming this works for collections, but not for single objects.

This is failing:

@JsonSerialize(as = BaseAccessionIdentifier.class)
@JsonDeserialize(as = BaseAccessionIdentifier.class)
public AccessionIdentifier getReference() {
}

Error message:

com.fasterxml.jackson.databind.JsonMappingException: Failed to widen type [simple type, class org.pharmgkb.model.AccessionIdentifier] with annotation (value org.pharmgkb.model.BaseAccessionIdentifier), from 'getReference': Class org.pharmgkb.model.BaseAccessionIdentifier not a super-type of [simple type, class org.pharmgkb.model.AccessionIdentifier]

@cowtowncoder
Copy link
Member

@markwoon could you file a new issue with that? Along with either reproduction, or at least type definitions needed. Stack trace would be helpful as well.

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