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

[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
Labels
Milestone

Comments

@stb-co
Copy link
Contributor

stb-co commented May 16, 2024

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
image

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:
image

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.
image 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 :)

@stb-co stb-co added the bug label May 16, 2024
@martincostello
Copy link
Collaborator

@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.

@martincostello
Copy link
Collaborator

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.

@stb-co
Copy link
Contributor Author

stb-co commented May 16, 2024

@martincostello Alright, that makes sense. I'll make a PR with the fix immediately :)

@martincostello
Copy link
Collaborator

Thanks for reporting this issue - the fix is available in Swashbuckle.AspNetCore 6.6.2.

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