You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[Bug]: C#9 positional Record with xmldoc containing param-tag without example property, generates unexpected 'example=""' for types with "string" json type
#2900
Closed
stb-co opened this issue
May 16, 2024
· 4 comments
· Fixed by #2901
We recently tried updating from 6.5.1 to 6.6.1 but had to put it off because we started seeing a bunch of these warnings in the OpenApiDiagnostics object being returned when generating the swagger doc
The problematic part of the schema was reported to be the generated 'examples' taken from the xmldoc of the start|end property of the record:
We could see that it happened for Records using positional syntax with DateTime fields and an xmldoc with a param tag without the example property, like so:
/// <summary>/// Some Request DTO/// </summary>/// <param name="IncludeSomething">Include a thing</param>/// <param name="UserIds">Ids of users</param>/// <param name="Start">Start date of something</param>/// <param name="End">End date of something</param>publicsealedrecordSomeRequestDto(boolIncludeSomething,List<string>UserIds,DateTimeStart,DateTimeEnd);
The same behaviour is present for Bool, Guid and string.
Expected behavior
The swagger doc should not contain empty schema 'examples' when the source records contains param tags without an example property.
Actual behavior
The swagger doc contains empty schema 'examples' even though the source records do not contain the example property in their param tags.
Steps to reproduce
Add an endpoint accepting a Record like this:
/// <summary>/// Some Request DTO/// </summary>/// <param name="IncludeSomething">Include a thing</param>/// <param name="UserIds">Ids of users</param>/// <param name="Start">Start date of something</param>/// <param name="End">End date of something</param>publicsealedrecordSomeRequestDto(boolIncludeSomething,List<string>UserIds,DateTimeStart,DateTimeEnd);
Call the Swagger endpoint and check the Warnings in the OpenApiDiagnostics of the result.
Check the generated Schema and observe there being empty examples.
Exception(s) (if any)
No response
Swashbuckle.AspNetCore version
6.6.1
.NET Version
net8.0
Anything else?
I've tracked the issue down to this line which is using this method. It returns string.Empty when the example propery isn't found, which causes the TrySetExample method to set the empty string as the example.
I can't think of a situation where it is ever desired for this method to allow an empty string anyway, so it should be easily fixed by replacing the example == null check with string.IsNullOrEmpty(example)
I can push a fix along with a test covering the problem, if desired :)
The text was updated successfully, but these errors were encountered:
@stb-co A PR to fix would be welcome if you can do it today, otherwise I'll look at it so it can be included in our next release which we're looking to do ASAP.
I think it would be safer (as in, not changing existing behaviour for non-records) to put the check before calling TrySetExample() on line 53, rather than changing the behaviour for everything.
Describe the bug
We recently tried updating from 6.5.1 to 6.6.1 but had to put it off because we started seeing a bunch of these warnings in the OpenApiDiagnostics object being returned when generating the swagger doc
The problematic part of the schema was reported to be the generated 'examples' taken from the xmldoc of the start|end property of the record:
We could see that it happened for Records using positional syntax with DateTime fields and an xmldoc with a param tag without the example property, like so:
The same behaviour is present for
Bool
,Guid
andstring
.Expected behavior
The swagger doc should not contain empty schema 'examples' when the source records contains param tags without an example property.
Actual behavior
The swagger doc contains empty schema 'examples' even though the source records do not contain the example property in their param tags.
Steps to reproduce
Add an endpoint accepting a Record like this:
Call the Swagger endpoint and check the Warnings in the OpenApiDiagnostics of the result.
Check the generated Schema and observe there being empty examples.
Exception(s) (if any)
No response
Swashbuckle.AspNetCore version
6.6.1
.NET Version
net8.0
Anything else?
I've tracked the issue down to this line which is using this method.
It returns
string.Empty
when the example propery isn't found, which causes theTrySetExample
method to set the empty string as the example.I can't think of a situation where it is ever desired for this method to allow an empty string anyway, so it should be easily fixed by replacing the
example == null
check withstring.IsNullOrEmpty(example)
I can push a fix along with a test covering the problem, if desired :)
The text was updated successfully, but these errors were encountered: