-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
Problem using optional ObjectIds and JacksonInject in the constructor when deserializing #1643
Comments
I managed to resolve this issue by doing a very ugly hack, that gets an auto generated objectId (getName) if the id is not found as an external property. And using reflection to set the field used to store it in the ValueInstantiator. public class NamedTestObjectInstantiator extends StdValueInstantiator {
public NamedTestObjectInstantiator(StdValueInstantiator src) {
super(src);
}
@Override
public Object createFromObjectWith(DeserializationContext ctxt, SettableBeanProperty[] props, PropertyValueBuffer buffer) throws IOException {
NamedTestObject object = (NamedTestObject)super.createFromObjectWith(ctxt, props, buffer);
forcefullySetObjectId(buffer, object.getName());
return object;
}
private void forcefullySetObjectId(PropertyValueBuffer buffer, String name) {
try {
Field idField = buffer.getClass().getDeclaredField("_idValue");
idField.setAccessible(true);
Object idValue = idField.get(buffer);
if(idValue == null) {
idField.set(buffer, name);
}
idField.setAccessible(false);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new IllegalStateException(e);
}
}
} |
On original question/problem: Object Ids are not optional -- if object type is defined to use Object Id / Reference constructs, Id must be provided. It is not optional. But there is an RFE, I think, to allow relaxing this limit, so perhaps in future this could be made to work. I do not remember issue number off-hand. |
FYI the RFE you are referring to is #1698. In my case I am trying to unify the |
I'm having some issues with using
@JacksonInject
inside the constructor in combination with optional ObjectIds.I'm trying to deserialize objects that not always have an ObjectId defined.
This works fine, but as soon as I use
@JacksonInject
to inject a constructor argument, I receive acom.fasterxml.jackson.databind.exc.MismatchedInputException: No Object Id found for an instance of test.jackson.SimpleTest$Simple, to assign to property 'id
Is this an issue or is this by design? I must confess I'm still new to the jackson library.
I've tried several version, mostly 2.8.8 but 2.9.0.pr3 also shows the same behavior.
I've includes a small test to illustrate my problem.
The text was updated successfully, but these errors were encountered: