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

maximum/minimum use decimal which do not parse certain many double "number" strings #747

Closed
1 task done
marcschier opened this issue Jun 14, 2024 · 3 comments
Closed
1 task done
Labels
bug Something isn't working pkg:schema

Comments

@marcschier
Copy link

Nuget Package

JsonSchema.Net

Package Version

7.0.4

Operating System

None

.Net Target (if relevant, please specify the version in the description)

None

A clear and concise description of the bug.

if maximum of a number is double.MaxValue, parsing fails since it is too large for decimal (used in the max/min keyword implementation).

What did you expect?

Looks like any exponential floating point should be allowed as per json-schema.org, hence parsing should not fail.

Please add test code or steps to reproduce the behavior.

JsonSchema fails to parse

"Double": {
"$id": "http://somethingsomething/#Double",
"title": "Type Double",
"type": "number",
"minimum": -1.7976931348623157E+308,
"maximum": 1.7976931348623157E+308,
"default": 0
}

Is there any other information you'd like to share regarding this bug?

No response

Code of Conduct & Contributor Guidelines

  • I agree to follow this project's Code of Conduct and Contribution Guidelines.
@marcschier marcschier added the bug Something isn't working label Jun 14, 2024
@koepalex
Copy link

It is also not possible to generate the Json schemas:

var schema = new JsonSchemaBuilder()
                    .Type(SchemaValueType.Number)
                    .Format("float")
                    .Minimum(Convert.ToDecimal(float.MinValue))
                    .Maximum(Convert.ToDecimal(float.MaxValue))
                    .Const(0f)
                    .Build(),

Results into runtime error message: System.OverflowException : Value was either too large or too small for a Decimal.

@gregsdennis
Copy link
Collaborator

This was an intentional design decision.

While IEEE floating point numbers support a greater range, decimal supports greater precision, which often is needed more, for instance in financial applications.

JSON itself supports arbitrary-precision numbers, but it also does have a note that computers have limitations in numeric representation.

JSON Schema has no such requirements, instead deferring to JSON.

I've even marked the appropriate test as skipped in the test suite as listed on Bowtie (find my implementation and click "Details").

@gregsdennis
Copy link
Collaborator

@marcschier I have a workaround for you: copy the existing keywords that use decimal (i.e. MinimumKeyword, etc.) and modify them to use double. Then you just need to register them, and they should work just fine.

Here's a quick test I ran to make sure it works:

SchemaKeywordRegistry.Register<MinimumDoubleKeyword>(TestSerializerContext.Default);

var text = $$"""
    {
        "type": "number",
        "minimum": {{float.MaxValue}}
    }       
    """;
var schema = JsonSerializer.Deserialize(text, TestSerializerContext.Default.JsonSchema);
JsonNode instance = (double)float.MaxValue * 5;
var result = schema.Evaluate(instance, new EvaluationOptions { OutputFormat = OutputFormat.Hierarchical });

result.AssertValid();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working pkg:schema
Projects
None yet
Development

No branches or pull requests

3 participants