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

Unable to deserialize (extended) Schema #67

Closed
sebfz1 opened this issue Apr 6, 2015 · 4 comments
Closed

Unable to deserialize (extended) Schema #67

sebfz1 opened this issue Apr 6, 2015 · 4 comments
Milestone

Comments

@sebfz1
Copy link

sebfz1 commented Apr 6, 2015

Hi,

It appears that it's not possible to deserialize a schema into an extended ObjectSchema class (ie: class MySchema extends ObjectSchema)

java.lang.IllegalArgumentException: Class com.fasterxml.jackson.module.jsonSchema.types.ObjectSchema is not assignable to com.github.issue.MySchema
    at com.fasterxml.jackson.databind.JavaType._assertSubclass(JavaType.java:408)
    at com.fasterxml.jackson.databind.JavaType.narrowBy(JavaType.java:148)
    at com.fasterxml.jackson.databind.jsontype.impl.TypeDeserializerBase._findDeserializer(TypeDeserializerBase.java:179)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer._deserializeTypedForId(AsPropertyTypeDeserializer.java:99)
    at com.fasterxml.jackson.databind.jsontype.impl.AsPropertyTypeDeserializer.deserializeTypedFromObject(AsPropertyTypeDeserializer.java:84)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeWithType(BeanDeserializerBase.java:948)
    at com.fasterxml.jackson.databind.deser.impl.TypeWrappedDeserializer.deserialize(TypeWrappedDeserializer.java:41)
    at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:3051)
    at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2146)
    at com.github.issue.TestSchema.testSchema(TestSchema.java:28)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)

Thanks!
Sebastien

@sebfz1
Copy link
Author

sebfz1 commented Apr 6, 2015

@cowtowncoder
Copy link
Member

Ok, first things first: the immediate reason for failure is use of type 'object', which will result in basic ObjectSchema being deserialized.

But adding handling of this new type is more involved. I'll see if I can figure that out, and if so, will add instructions for doing that.

@cowtowncoder
Copy link
Member

Sample implementation will be found from 'src/test/java/com/fasterxml/jackson/module/jsonSchema/CustomSchemaReadTest.java'. Here are the things needed:

    @JsonTypeIdResolver(MyResolver.class)
    public static class MySchema extends ObjectSchema { }

    static class MyResolver extends JsonSchemaIdResolver
    {
        @Override
        public String idFromValue(Object value) {
            if (value instanceof MySchema) {
                return "CUSTOM";
            }
            return super.idFromValue(value);
        }

        @Override
        public String idFromValueAndType(Object value, Class<?> suggestedType) {
            if (value instanceof MySchema) {
                return "CUSTOM";
            }
            return super.idFromValueAndType(value, suggestedType);
        }

        @Override
        public JavaType typeFromId(DatabindContext ctxt, String id) {
            if ("CUSTOM".equals(id)) {
                return ctxt.constructType(MySchema.class);
            }
            return super.typeFromId(ctxt, id);
        }
    }

so it just boils down to adding handling for custom type; you can choose whatever id you want, and it would be possible to make this bit more general.

What I do not know is why handling was done this way: standard Jackson mechanism would have been to instead of basic @JsonTypeName without custom id handler. But the original author (this is a contribution) for some reason chose a bit more complex (and less extensible) route.
Perhaps this should be revisited in future.

@cowtowncoder cowtowncoder added this to the 2.6.0 milestone Apr 27, 2015
@sebfz1
Copy link
Author

sebfz1 commented Apr 28, 2015

Hi Tatu,

Thank you very much for this!

Best regards,
Sebastien

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