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

Clarifying about floating point types extra values #2617

Open
orihomie opened this issue Jan 10, 2020 · 6 comments
Open

Clarifying about floating point types extra values #2617

orihomie opened this issue Jan 10, 2020 · 6 comments

Comments

@orihomie
Copy link

orihomie commented Jan 10, 2020

There's another problem with float and double and its about this types scopes - they can be Infinity or NaN, but generated json schema points them as "number" only.
I think its could be a problem when casting json types onto C# Dto objects.

Originally posted by @orihomie in #1122 (comment)

I need to know if there's any solution for that problem without specifying type of such value like this:

"some_value": {
          "type": "number, string"
        }

This way there's no clarity if I got specific type strings like "NaN" / "Infinity" or any trash that shouldnt be there.
I've read old issues on OAS repo which were pointing this problem, but they, like this one, gave me no clarity on how I should I behave myself with such types.

@orihomie orihomie changed the title There's another problem with float and double and its about this types scopes - they can be Infinity or NaN, but generated json schema points them as "number" only. Clarifying about floating point types extra values Jan 10, 2020
@RicoSuter
Copy link
Owner

Cant a "number" be NaN or Infinity?

@orihomie
Copy link
Author

It seems not cause I'm having a problem with that kind of json object:

{
	"id": 1,
	"date": "2019-12-16T20:31:48.68",
	"policyStartDate": "2020-01-21T00:00:00",
	"externalContractNumber": "",
	"userName": "skipped user name",
	"price": 0.0,
	"companyId": 2,
	"policyLink": "",
	"partnerCampaignMedium": "campaign",
	"policyBlankLink": null,
	"source": "skipped",
	"subId": "skipped",
	"registrationRoute": "route",
	"subAgentRoute": "skipped",
	"isAccepted": true,
	"withProlongation": true,
	"profit": 0.0,
	"profitPercent": "NaN",
	"hash": "skippedipped",
}

Look at this property:

"profitPercent": "NaN",

This is object's (property) C# code:

public double ProfitPercent { get; set; }

And this is error I'm getting when trying to validate it:

Shouldly.ShouldAssertException : validateResult.Count
    should be
0
    but was
1

Additional Info:
    ArrayItemNotValid: #/result[3]
{
  NumberExpected: #/result[3].profitPercent
}

This is the json schema of this object:

 "profitPercent": {
          "type": "number"
        },

This is how I generate schema's:

var schemaString = NJsonSchema.JsonSchema.FromSampleJson(result).ToJson();

And this is how I validate them:

var validateResult = new NJsonSchema.Validation.JsonSchemaValidator().Validate(result, schema);
validateResult.Count.ShouldBe(0, string.Join('\n', validateResult));

By the way - it seems neither NSwag or OAS spec supports nullable C# values. I mean I expect that value's schema to be like this:

 "someValue": {
          "type": "number, null"
        },

@RicoSuter
Copy link
Owner

With JSON Schema the "type" can also be an array, e.g. "type": ["number", "null"] but this is not allowed in Swagger 2.0 or OpenAPI 3.0.

In Swagger 2.0 there is no way to specify nullability - there is only required (which is often misused to also specify nullability).

In OpenAPI 3.0 a new property "nullable" on a property has been introduced:

"myproperty": {
   "type": "string",
   "nullable": true
}

@orihomie
Copy link
Author

orihomie commented Jan 14, 2020

Ok, and what about "NaN" or "Inifinity" values? I'm not even mentioning C# methods checking if its positive or negative infinity

@RicoSuter
Copy link
Owner

Any idea what the correct way is to serialize NaN and Infinity with JSON? Maybe using "NaN" or "Inifinity" is correct but Newtonsoft.Json cannot handle it (with default settings)?

@orihomie
Copy link
Author

orihomie commented Jan 14, 2020

How we could make possible to declare custom type with some mixed allowable values, like "number" type range of values along with "NaN" and "pInfinity" / "nInfinity" ?
This is idea I'm thinking of (sry, lacking a bit of exp in json stuff).

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

No branches or pull requests

2 participants