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 hyphen in Json Key Strings #298

Open
tarunjoshi-lily opened this issue Sep 16, 2021 · 1 comment
Open

Allow hyphen in Json Key Strings #298

tarunjoshi-lily opened this issue Sep 16, 2021 · 1 comment
Labels

Comments

@tarunjoshi-lily
Copy link

tarunjoshi-lily commented Sep 16, 2021

I am using my Jackson-dataformat-avro jar to parse POJO (which are going to accept deserialized json & I don't control schema of those JSON) and subsequently use those POJO to map to a GenericRecord for Avro. The JSON schema has all bad naming conventions like hyphen and underscore. When I try to set up the AvroSchemaWrapper in this way :

 ObjectMapper mapper = new ObjectMapper(new AvroFactory());
        AvroSchemaGenerator gen = new AvroSchemaGenerator();
        mapper.acceptJsonFormatVisitor(Response.class, gen);
        AvroSchema schemaWrapper = gen.getGeneratedSchema();

        return schemaWrapper.getAvroSchema(); 

I get exception while initialization of my Java Job saying :

Caused by: org.apache.avro.SchemaParseException: Illegal character in: User-Agent
at org.apache.avro.Schema.validateName(Schema.java:1561)
at org.apache.avro.Schema.access$400(Schema.java:87)
at org.apache.avro.Schema$Field.(Schema.java:541)
at org.apache.avro.Schema$Field.(Schema.java:580)
at com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor.schemaFieldForWriter(RecordVisitor.java:189)
at com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor.optionalProperty(RecordVisitor.java:117)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.depositSchemaProperty(BeanPropertyWriter.java:839)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.acceptJsonFormatVisitor(BeanSerializerBase.java:912)
at com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor.schemaFieldForWriter(RecordVisitor.java:174)
at com.fasterxml.jackson.dataformat.avro.schema.RecordVisitor.optionalProperty(RecordVisitor.java:117)
at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.depositSchemaProperty(BeanPropertyWriter.java:839)
at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.acceptJsonFormatVisitor(BeanSerializerBase.java:912)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.acceptJsonFormatVisitor(DefaultSerializerProvider.java:566)
at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:4438)
at com.fasterxml.jackson.databind.ObjectMapper.acceptJsonFormatVisitor(ObjectMapper.java:4417)
at ai.lily.sample.pipeline.SimpleMapper.generateAvroSchemaFromJacksonPOJO(SimpleMapper.java:99)
at ai.lily.sample.pipeline.SimpleMapper.(SimpleMapper.java:29)

Am I doing something wrong , can i (While extracting Avro Schema ) allow parser to not use @JsonProperty to define Avro schema field names ??

@cowtowncoder
Copy link
Member

Jackson requires logical property names to be used to match to Avro schema. But it is possible to both define safe property names with @JsonProperty (ones with no hyphens) and maybe also add @JsonAlias for "bad" names to be understood on input. This would allow sort of conversion.

PropertyNamingStrategy may also be used for some automated transformations: it can be applied on POJOs with @JsonNaming, or configured on ObjectMapper. Not sure if that could help.

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

No branches or pull requests

2 participants