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

Allow optional Object Id for @JsonIdentityInfo #1698

Open
f0def opened this issue Jul 11, 2017 · 1 comment
Open

Allow optional Object Id for @JsonIdentityInfo #1698

f0def opened this issue Jul 11, 2017 · 1 comment

Comments

@f0def
Copy link

f0def commented Jul 11, 2017

Related to the #1388 and #1261

I need to use @JsonCreator to test the input arguments in the constructor. And I want to support alternate API options with or without "@id" (in some places there is recursion, and somewhere there is no).

If the way for this test to be performed without errors?

com.fasterxml.jackson.databind.exc.MismatchedInputException: No Object Id found for an instance of com.example.JsonIdentityInfoWithJsonCreatorTest$Foo, to assign to property '@id'

Tested on 2.8.3 and 2.9.0.pr4

package com.example;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;

import java.io.IOException;

public class JsonIdentityInfoWithJsonCreatorTest {
  @Test
  public void testJsonIdentityInfoWithJsonCreator() throws IOException {
    ObjectMapper objectMapper = new ObjectMapper();

    TypeReference typeReference = new TypeReference<Foo>() {
    };

    //no exception - ok
    objectMapper.readValue("{\"@id\": \"1\", \"name\": \"foo\"}", typeReference);

    //JsonMappingException throwed - why?
    objectMapper.readValue("{\"name\": \"foo\"}", typeReference);

    //JsonMappingException throwed - why?
    objectMapper.readValue("{\"name\": \"foo\", \"@id\": \"1\"}", typeReference);
  }

  @JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
  public static class Foo {
    @JsonCreator
    public Foo(@JsonProperty("name") String name) {
    }
  }
}
@cowtowncoder
Copy link
Member

At this point Object Id must be present, if one is expected.

Perhaps it would be possible to have a DeserializationFeature that allows missing type id during deserialization: if so, such values would not be referencable, but would not fail.

Alternatively... maybe a better way would be to add one more property for @JsonIdentityInfo, something like requireId or just required, which would apply to that specific type.
I think I like this approach more.

I will change the title to reflect this as RFE, assuming that ability to optionally exclude Object Ids is what you would like to see, regardless of mechanism.

@cowtowncoder cowtowncoder changed the title @JsonIdentityInfo and @JsonCreator Allow optional Object Id for @JsonIdentityInfo Jul 21, 2017
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