-
Notifications
You must be signed in to change notification settings - Fork 325
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
Improve type validation of numeric values #334
Comments
@oguzhanunlu Thanks a lot for raising the issue and provide a solution. Let me add some background information here so that others can see where this issue is from and why we should adopt your solution as a Java-specific implementation.
Given the above quote from the official site, I think we should leverage the Java language feature to support the Long type; however, the change might make the validator different than the Javascript implementation. As this is a community-driven implementation, I want to know the opinion of @everybody. The best solution is to support both and use the configuration to differentiate it. We just need to make the decision of which implementation is the default. |
Thanks for the quick response @stevehu ! Looking at the implementation of Instead of the initial proposal, perhaps something as following: if (node.isIntegralNumber() || (node.canConvertToLong() && !node.asText().contains('.')))
return JsonType.INTEGER; |
Hi @stevehu , is there any update on this one? Or is there anything I can do to proceed here? |
@oguzhanunlu I think we should go ahead with the implementation. Just make sure to keep the existing behaviour as default and the new feature can be enabled from the configuration. Could you submit a PR for this? Thanks. |
sure @stevehu , I'll open a PR, thanks! |
@oguzhanunlu Thanks a lot for your help. I am working with customers these days and very busy. I am reviewing in my spare time and it will take a bit longer. |
I understand it @stevehu , no worries at all! I hope jackson-databind publishes 2.12.0 in the meantime. |
* Improve type validation of integrals Instead of doing string comparison, use new jackson method to determine if a number is an integral. The `javaSemantics` config option was added in PR #343 which partially addressed issue #334. In the notes for this PR: > Once jackson-databind 2.12.0 is out, I'll replace my solution with a call to canConvertToExactIntegral jackson-databind has been updated to 2.12.1 so this is available but the change has not yet been made. PR #450 which addressed #446 missed this location which is used when calling `JsonSchemaFactory.getSchema`. Issue #344 requested coercion of various types but the only type implemented in PR #379 was lossless narrowing, set with configuration option `losslessNarrowing`. I believe that setting is unnecessary now as this implementation of `javaSemantics` addresses the original issue, but left it for backwards compatibility. It also allows you to set `javaSemantics=false` and `losslessNarrowing=true` to achieve only this specific case rather than anything that `javaSemantics` is used for in the future. At this time, these properties do exactly the same thing. - Change from string comparison to `canConvertToExactIntegral` for `javaSemantics` and `losslessNarrowing` - Add missing documentation around `losslessNarrowing` - Add more test cases around integrals * Update changelog
Issue
TypeFactory uses
jackson-databind
'sJsonNode.isIntegralNumber
andJsonNode.isNumber
to see if the node's type is integer or number, respectively. This result is used when comparing node type and schema type which is not precise enough in some cases.json-schema-validator/src/main/java/com/networknt/schema/TypeFactory.java
Lines 72 to 75 in 4ba09d5
Scenario
Assume that we have a schema where a property's type is
integer
.Given a
DecimalNode(v)
wherev
is a value that can fit intoLong
, json-schema-validator thinks that node type isnumber
and this results in type failure asnumber found, integer expected
Proposal
Update line 72-73 in the snippet above as following (
canConvertToLong()
is defined forDecimalNode
atjackson-databind
)so that we can have a validator with better precision
The text was updated successfully, but these errors were encountered: