Skip to content
This repository has been archived by the owner on Jan 22, 2019. It is now read-only.

Not able to serialize avro generated object having schema$ object #35

Closed
anandshah123 opened this issue Feb 18, 2016 · 7 comments
Closed
Milestone

Comments

@anandshah123
Copy link

Below exception is thrown

com.fasterxml.jackson.databind.JsonMappingException: No field named 'schema' (through reference chain: com.demonstration.Event["schema"])
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:339)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:299)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:342)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:686)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
    at com.fasterxml.jackson.databind.ObjectWriter$Prefetch.serialize(ObjectWriter.java:1428)
    at com.fasterxml.jackson.databind.ObjectWriter._configAndWriteValue(ObjectWriter.java:1129)
    at com.fasterxml.jackson.databind.ObjectWriter.writeValueAsBytes(ObjectWriter.java:1025)
    at com.fasterxml.jackson.dataformat.avro.WriteGeneratedObject.testWriteBidResponse(WriteGeneratedObject.java:16)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:483)
    at junit.framework.TestCase.runTest(TestCase.java:176)
    at junit.framework.TestCase.runBare(TestCase.java:141)
    at junit.framework.TestResult$1.protect(TestResult.java:122)
    at junit.framework.TestResult.runProtected(TestResult.java:142)
    at junit.framework.TestResult.run(TestResult.java:125)
    at junit.framework.TestCase.run(TestCase.java:129)
    at junit.framework.TestSuite.runTest(TestSuite.java:252)
    at junit.framework.TestSuite.run(TestSuite.java:247)
    at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:86)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: java.lang.IllegalStateException: No field named 'schema'
    at com.fasterxml.jackson.dataformat.avro.ser.ObjectWriteContext._reportUnknownField(ObjectWriteContext.java:124)
    at com.fasterxml.jackson.dataformat.avro.ser.ObjectWriteContext.writeFieldName(ObjectWriteContext.java:67)
    at com.fasterxml.jackson.dataformat.avro.AvroGenerator.writeFieldName(AvroGenerator.java:283)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:670)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:678)
    ... 25 more

anandshah123 added a commit to anandshah123/jackson-dataformat-avro that referenced this issue Feb 18, 2016
@cowtowncoder
Copy link
Member

Which Jackson version is this with?

@anandshah123
Copy link
Author

I am getting this for any version above 2.4.3 but theoretically it should happen for all versions.
Probable reason is that BeanSerializer identifies SCHEMA$ as field (even if it is static)

@cowtowncoder
Copy link
Member

@anandshah123 static members should not be included, although I vaguely recall fixing a bug in that handling a while ago. But that would probably have been before 2.4.3.

I think I better have a look at your test case at this point.

@cowtowncoder
Copy link
Member

Ok; the problem is not the static field, but rather definition in SpecificRecordBase:

public abstract class SpecificRecordBase
  implements SpecificRecord, Comparable<SpecificRecord>, GenericRecord {

  public abstract Schema getSchema();
}

which does suggest there is property schema, accessible via getSchema().
I'll have to think about how to handle this.

In the meantime, you can work around this by adding mix-in for something like

abstract class Mixin {
   @JsonIgnore
   public abstract Schema getSchema();
}

and attach that to SpecificRecordBase. That should get rid of the property for all generated types.

@cowtowncoder
Copy link
Member

@anandshah123 I think this could be fixed via AvroModule registering BeanSerializerModifier, which would strip schema property. I'll add a failing test first.

cowtowncoder added a commit that referenced this issue Mar 9, 2016
@cowtowncoder cowtowncoder added this to the 2.7.3 milestone Mar 9, 2016
@cowtowncoder
Copy link
Member

Fixed this. Note: you need to use AvroMapper to get functionality (or if you really want to, register AvroSerializerModifier via custom module).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants