Fix null examples being wrongly converted#3802
Fix null examples being wrongly converted#3802dldl-cmd wants to merge 3 commits intodomaindrivendev:masterfrom
Conversation
Fixes domaindrivendev#3801 Changes the JsonModelFactory, which is used in more cases than the XML comment examples to convert a json string to JsonNode with special handling for the null json string. Moreover, adding a special rule for the null example on string which results in either the string null (`"example": "null"`) or the json value null (`"example": null`)
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3802 +/- ##
=======================================
Coverage 94.93% 94.93%
=======================================
Files 111 111
Lines 3886 3892 +6
Branches 784 785 +1
=======================================
+ Hits 3689 3695 +6
Misses 197 197
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
The country parameter is not nullable and of string type. Therefore, the example should just contain the given example string
| "schema": { | ||
| "type": "string", | ||
| "example": null | ||
| "example": "null" |
There was a problem hiding this comment.
For applications that do not have nullable enable this sounds weird
There was a problem hiding this comment.
Why is the weird to applications which do not have nullable enabled? When the generated schema does indicate, that null isn't allowed having null in the example is wrong. For strings to use than just the value the developer has put there in the example would be a single a good understandable solution.
There was a problem hiding this comment.
But an application which has nullable enabled the desired behaviour I think it'll be to present that value as null(There is no possibility to use string?).. Indeed in the SwashBuckle versions prior to 10 that was the behaviour.. What I tried to do in #3803 is to put the null when the type is null or when is a string that is not DateTime/DateOnly/TimeOnly/Timespan (Because those had the possibility always to have the DateTime?/DateOnly?...)
Also think about the possibility that the client is using OpenApi 3.0.. To put the example as null seems fine.. Also I think that if the client put null we must never put the nullstring.. Because the client mean null not "null"(Which was the original issue #3784)
There was a problem hiding this comment.
If you want you can merge my changes (There are a couple of test more with DateTime/DateOnly).. And get to a consensus
There was a problem hiding this comment.
The library already handles the nullable type indication correctly. So when nullable is enabled
public string Prop { get; set; };{
"type": "string"
}If nullable annotations are not enabled:
public string Prop { get; set; };{
"type": "string",
"nullable": true
}Also the difference between OpenApi 3.0 and 3.1 doesn't make any difference here as the OpenApi.NET library abstracts the difference away:
OpenApi v3.0
{
"type": "string",
"nullable": true
}OpenApi v3.1
{
"type": ["null","string"],
}
|
Could you talk me about the first point?, I don't get to see it I do not like to put "null" as an example because if the example was set as null we shouldn´t create the example as "null", if they want to set the example as "null", they could put "null" |
|
For the first point the Why I think putting
Furthermore considering these two cases: /// <example>null</example>
public required string NullExampleOnNotNullable { get; set; }
/// <example>null2</example>
public required string Null2ExampleOnNotNullable { get; set; }My expectation would be that I see: {
"NullExampleOnNotNullable": {
"type": "string",
"example": "null"
},
"Null2ExampleOnNotNullable": {
"type": "string",
"example": "null2"
}
}And for the nullable case: /// <example>null</example>
public string? NullExampleOnNullable { get; set; }
/// <example>null2</example>
public string? Null2ExampleOnNullable { get; set; }{
"NullExampleOnNullable": {
"type": "string",
"nullable": true,
"example": null
},
"Null2ExampleOnNullable": {
"type": "string",
"nullable": true,
"example": "null2"
}
} |
|
With your branch for simple int values with example set to null it will get serialized to null which is incorrect right?. From the table sent on the issue my branch does not fail at any case |
|
@dldl-cmd should I remove the draft from my PR? if so you can do the comments there ;) |
Fixes #3801
Changes the JsonModelFactory, which is used in more cases than the XML comment examples to convert a json string to JsonNode with special handling for the null json string.
Moreover, adding a special rule for the null example on string which results in either the string null (
"example": "null") or the json value null ("example": null)