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

Default null value causes NumberFormatException #61

Closed
joelittlejohn opened this issue Jun 23, 2013 · 3 comments
Closed

Default null value causes NumberFormatException #61

joelittlejohn opened this issue Jun 23, 2013 · 3 comments
Milestone

Comments

@joelittlejohn
Copy link
Owner

Original author: [email protected] (July 09, 2012 19:25:43)

What steps will reproduce the problem?

  1. Generate code from this schema:
    {
    "id": "test",
    "type": "object",
    "additionalProperties": false,
    "properties": {
    "age": {
    "type": "integer",
    "default": null,
    "minimum": 0
    }
    }
    }

What is the expected output?
Code generated successfully.

What do you see instead?
java.lang.NumberFormatException (stack trace pasted below)

What version of the product are you using?
0.3.0

On what Java version?
java version "1.6.0_30"

Adding this code to the beginning of method resolves the issue (DefaultRule.getDefaultValue(DefaultRule.java:92))
private JExpression getDefaultValue(JType fieldType, JsonNode node) {
if( node.isNull() ){
return JExpr._null();
}

Stack Trace:

Exception in thread "main" java.lang.NumberFormatException: For input string: "null"
at java.lang.NumberFormatException.forInputString(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at java.lang.Integer.parseInt(Unknown Source)
at com.googlecode.jsonschema2pojo.rules.DefaultRule.getDefaultValue(DefaultRule.java:101)
at com.googlecode.jsonschema2pojo.rules.DefaultRule.apply(DefaultRule.java:84)
at com.googlecode.jsonschema2pojo.rules.DefaultRule.apply(DefaultRule.java:1)
at com.googlecode.jsonschema2pojo.rules.PropertyRule.apply(PropertyRule.java:108)
at com.googlecode.jsonschema2pojo.rules.PropertyRule.apply(PropertyRule.java:1)
at com.googlecode.jsonschema2pojo.rules.PropertiesRule.apply(PropertiesRule.java:62)
at com.googlecode.jsonschema2pojo.rules.PropertiesRule.apply(PropertiesRule.java:1)
at com.googlecode.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:108)
at com.googlecode.jsonschema2pojo.rules.ObjectRule.apply(ObjectRule.java:1)
at com.googlecode.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:95)
at com.googlecode.jsonschema2pojo.rules.TypeRule.apply(TypeRule.java:1)
at com.googlecode.jsonschema2pojo.rules.JsonSchemaRule.apply(JsonSchemaRule.java:73)
at com.googlecode.jsonschema2pojo.rules.JsonSchemaRule.apply(JsonSchemaRule.java:66)
at com.googlecode.jsonschema2pojo.rules.JsonSchemaRule.apply(JsonSchemaRule.java:1)
at com.googlecode.jsonschema2pojo.SchemaMapperImpl.generate(SchemaMapperImpl.java:65)
at com.googlecode.jsonschema2pojo.cli.Jsonschema2Pojo.generate(Jsonschema2Pojo.java:92)
at com.googlecode.jsonschema2pojo.cli.Jsonschema2Pojo.main(Jsonschema2Pojo.java:60)

Original issue: http://code.google.com/p/jsonschema2pojo/issues/detail?id=61

@joelittlejohn
Copy link
Owner Author

From [email protected] on July 09, 2012 20:20:52
We need to tread a bit carefully here as it's possible that people are using primitive types (e.g. usePrimitives=true) and we don't want to generate code that fails to compile. It will probably be simpler to leave the value uninitialized when "default":null is present (as this will work for all objects including wrapper types, but will leave primitive types with their natural default).

I'm interested to know, why would you write a schema like this? Is this simply a contrived example to reproduce the bug?

@joelittlejohn
Copy link
Owner Author

From [email protected] on July 09, 2012 20:29:39
Hi Joe,

It's a contrived example to reproduce the bug. I'm consuming someone
else's REST service and their schema has number fields that are initialized
to null.

Could this work?
private JExpression getDefaultValue(JType fieldType, JsonNode node) {
if( !fieldType.isPrimitive() && node.isNull() ){
return JExpr._null();
}

@joelittlejohn
Copy link
Owner Author

From [email protected] on July 09, 2012 21:42:20
Ah I see. I expect this is the kind of thing that happens when the schema is auto-generated :)

I think your second suggestion is a good one, I've pushed this change (r4008e43777a2).

Thanks for your contribution!

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

No branches or pull requests

1 participant