-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Description
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:
/// <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>
public sealed record SomeRequestDto(
bool IncludeSomething,
List<string> UserIds,
DateTime Start,
DateTime End);
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>
public sealed record SomeRequestDto(
bool IncludeSomething,
List<string> UserIds,
DateTime Start,
DateTime End);
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 :)