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

@JsonTypeInfo property added twice to generated JSON: add support for JsonType.As.EXISTING_PROPERTY #528

Closed
heapifyman opened this issue Aug 29, 2014 · 6 comments
Milestone

Comments

@heapifyman
Copy link

Hi,

I have an abstract super class annotated with

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")

where type is an existing property of the class.

The full class hierarchy looks like this:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
@JsonSubTypes({
                      @JsonSubTypes.Type(value = SubClassA.class, name = "a"),
                      @JsonSubTypes.Type(value = SubClassB.class, name = "b")
              })
public abstract class AbstractSuperClass {
    @Transient
    private String type;

    protected AbstractSuperClass() {
        setType(getTypeValue());
    }
    protected abstract String getTypeValue();

    // getters and setters
}

public class SubClassA extends AbstractSuperClass {
    private double a;

    public SubClassA(double a) {
        this.a = a;
    }

    // getters and setters

    @Override
    protected String getTypeValue() {
        return "a";
    }
}

public class SubClassB extends AbstractSuperClass {

    private int b;

    public SubClassB(int b) {
        this.b = b;
    }

    // getters and setters

    @Override
    protected String getTypeValue() {
        return "b";
    }
}

Now when a SubClass of this class gets serialised, the type is added twice to the generated JSON. E.g. the JSON for a SubClassB would look like this:

{"type":"b","type":"b","b":42}

For the most part this does not seem to be an issue, but some clients (e.g. Postman) refuse this as Malformed JSON.

I know I could add e.g. @JsonTypeId or @JsonIgnore but in that case, type will not be included in generated JSON when using a List<AbstractSuperClass>, which is what I definitely need.

I assume that include = JsonTypeInfo.As.EXISTING_PROPERTY would solve my problem but this results in java.lang.IllegalStateException: Do not know how to construct standard type serializer for inclusion type: EXISTING_PROPERTY

@cowtowncoder
Copy link
Member

I think there's another issue opened for this. And yes, it is a bug; and also yes, EXISTING_PROPERTY, when implemented should solve it. Unfortunately I haven't had time to dig deep into doing just that.

@cowtowncoder cowtowncoder changed the title @JsonTypeInfo property added twice to generated JSON @JsonTypeInfo property added twice to generated JSON: add support for JsonType.As.EXISTING_PROPERTY Sep 23, 2014
@fleebytes
Copy link

I also have a need for JsonType.As.EXISTING_PROPERTY to be implemented. I have done this locally, but would like to know how extensive the test cases need to be before I commit to my fork and issue a pull request. Thanks!

@cowtowncoder
Copy link
Member

@fleebytes I think a fork & Pull Request is a good way -- we can help with tweaks, and testing. Usually the first thing is to ensure that none of existing tests start failing, but here it may not help a lot, given that EXISTING_PROPERTY is not being used. So new tests are needed, perhaps based on existing tests for creator-functionality.
But I can help with tests too, if there's at a test or two to verify happy case, including round-trip (serialize, then deserialize back) test.

For what it is worth, this is a REALLY important feature, and should go in 2.5. I have been hoping to find time to tackle it myself, after resolving other somewhat related problems (wrt visibility of type info).
So all help is much appreciated.

fleebytes pushed a commit to fleebytes/jackson-databind that referenced this issue Oct 15, 2014
…OPERTY

serialization and deserialization
@fleebytes
Copy link

@cowtowncoder I've taken a stab at writing the tests. There is round-trip serialization for three scenarios: (1) sub-classes each have the type property (no reference in base class), (2) sub-classes each have getType() method implemented but no explicit property, and (3) base class has abstract getType() method that enforces sub-classes to have that method. All three of them have working round trip test cases. Let me know if there is anything else I can do to help complete adding this feature to jackson-databind. Thanks!

fleebytes pushed a commit to fleebytes/jackson-databind that referenced this issue Oct 15, 2014
…tween

inclusion mechanism EXISTING_PROPERTY and PROPERTY via inheritance.
cowtowncoder added a commit that referenced this issue Oct 16, 2014
Fixes #528 - Add support for As.EXISTING_PROPERTY inclusion mechanism
cowtowncoder added a commit that referenced this issue Oct 16, 2014
@ouaibsky
Copy link

Hi

Can you tell me in which version this fix has been released ?
Thx
Christophe

@cowtowncoder cowtowncoder added this to the 2.5.0 milestone Jun 11, 2015
@cowtowncoder
Copy link
Member

Original fix was in 2.5.0. There is one related fix (#222) that will be included in 2.6.0 (and 2.6.0-rc3 if such is released before 2.6.0 final).

alexGofore added a commit to Opetushallitus/tarjonta that referenced this issue Feb 16, 2016
Toteutustyyppi was not serialized in JSON response when creating/updating
a koulutus.

Need to update Jackson to a newer version, because there was a bug with previous version
related to @JsonTypeInfo serialization.

See more: FasterXML/jackson-databind#528
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

4 participants