Skip to content

[Bug]: C#9 positional Record with xmldoc containing param-tag without example property, generates unexpected 'example=""' for types with "string" json type #2900

@stb-co

Description

@stb-co

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions