nokia / Avro-Schema-Generator

Tool which generates Avro schemas and Java bindings from XML schemas.
Apache License 2.0
37 stars 26 forks source link

Unable to handle defined type 'java.math.BigDecimal' #7

Open edi-bice opened 8 years ago

edi-bice commented 8 years ago

I have an attribute of type xs:decimal in my XSD.

Get the following error

Unable to handle defined type 'java.math.BigDecimal'.

... and suspect it's due to Avro not supporting Decimal until recently

https://issues.apache.org/jira/browse/AVRO-1402

UnquietCode commented 8 years ago

Some 'special' xml types like dates and big numbers are handled specially. There is room to add a test and support for java.math.BigDecimal. The real question is what the mapping should be. You can see that for BigInteger, an Avro string is used: https://github.com/nokia/Avro-Schema-Generator/blob/master/SchemaCompiler/schemagen-core/src/main/java/com/nokia/util/avro/schemagen/SchemagenHelper.java#L271

edi-bice commented 8 years ago

I don't know enough to decide. There's a lengthy discussion in the Avro JIRA above about the merits of string etc.

UnquietCode commented 8 years ago

Right. I think for now you would have to use a string and then in your application parse it into a BigDecimal object after you deserialize from Avro. For this tool at least, it would be pretty easy to add the mapping to make it a string.

See this bit about parsing strings back into BigDecimals in Java: http://stackoverflow.com/a/18231864/4966696

edi-bice commented 8 years ago

Okay, it seemed it was easy enough to get this tool to handle BigDecimal - added following where you pointed, compiled and installed, and then generate parsed my schema okay and generated classes

if (clazz.isAssignableFrom(clazz.owner().ref(BigDecimal.class))) { return AvroPrimitive.PrimitiveType.STRING.newInstance(); }