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

Schema Metadata for C# 9 Records #1920

Closed
RehanSaeed opened this issue Dec 4, 2020 · 6 comments
Closed

Schema Metadata for C# 9 Records #1920

RehanSaeed opened this issue Dec 4, 2020 · 6 comments
Labels
p2 Medium priority
Milestone

Comments

@RehanSaeed
Copy link
Contributor

RehanSaeed commented Dec 4, 2020

C# 9 Positional Records & XML Comments

We can't use positional records with XML comments because there is no way to provide comments for the properties. There is a Roslyn bug open for this at dotnet/roslyn#44571. This would be the ideal solution.

C# 9 Records & XML Comments

We can create init properties instead and comment them as usual. This works.

/// <summary>A product.</summary>
public record Product
{
    /// <summary>Gets or sets the unique identifier.</summary>
    public int Id { get; init; }
}

C# 9 Positional Records & Swagger Annotations

Using the [SwaggerSchema] attribute in a positional record doesn't seem to work. Please can support for this be added?

public record Product([SwaggerSchema("The product identifier")] int Id);
@GoodeUser
Copy link

GoodeUser commented Jan 15, 2021

@RehanSaeed You should be able to target the backing property by doing something like this:

public record Product([property: SwaggerSchema("The product identifier")] int Id);

The corresponding documentation for Positional Record Members can be found here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/records#positional-record-members

Update (2/24/2022):
This is in the current release preview but until the fix to allow XML param comments on records make it to a stable release (link here), our team has been manually specifying the properties on records (which ain't pretty). An example of a working solution is this:

public record ActiveSessionsPostInput(Guid Pid, Guid Cid, string SessionId)
{
    /// <summary>
    /// The ParentId of the account
    /// </summary>
    public Guid Pid { get; init; } = Pid;

    /// <summary>
    /// The ChildId sending the request
    /// </summary>
    public Guid Cid { get; init; } = Cid;

    /// <summary>
    /// The ID of the current session.
    /// </summary>
    public string SessionId { get; init; } = SessionId;

    public ActiveSession ToActiveSession() => new(Pid, Cid, DateTime.UtcNow, SessionId);
}

@domaindrivendev domaindrivendev added the p2 Medium priority label Jan 17, 2021
@domaindrivendev domaindrivendev removed this from the Backlog milestone Jan 17, 2021
@reinux
Copy link

reinux commented Nov 16, 2021

@RehanSaeed You should be able to target the backing property by doing something like this:

public record Product([property: SwaggerSchema("The product identifier")] int Id);

The corresponding documentation for Positional Record Members can be found here: https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/proposals/csharp-9.0/records#positional-record-members

This compiles, but it doesn't appear to be picked up by Swashbuckle.

The problem has also been stuck on the C# side since April. dotnet/roslyn#52663

@andy-g
Copy link

andy-g commented Jun 12, 2022

From my understanding, the issue with XML comments for record properties has been resolved with dotnet 6.

I'm able to compile either of these options:

/// <summary>A product.</summary>
public record Product
{
    /// <summary>Gets or sets the unique identifier.</summary>
    public int Id { get; init; }
}

/// <summary>A product.</summary>
/// <param name = "Id">Gets or sets the unique identifier.</param>
public record Product2
(
    int Id
);

Now that this is supported in a released version of the language, is it possible to support this in Swashbuckle too, to handle both xml comments like this, as well as additional data annotations as outlined in dotnet/AspNetCore.Docs#24858?

There quite a lot of excitement around using records as immutable DTO's etc, which is generally very useful for API requests & responses, but it's quite confusing when the results on the swaggger / redoc output is inconsistent depending on whether a class or record type is used.

Thanks so much.

@voroninp
Copy link

voroninp commented Jun 24, 2022

@GoodeUser , targeting parameter:

public record Product([property: Required] string Id);

does not work.
Framework throws, because for positional records validation attributes must be applied to constructor parameters. However, Swashbuckle does not recognize nullability of reference types or attribute applied to constructor parameter.

@MJLHThomassenHadrian
Copy link

Any updates on this?

@barruka
Copy link

barruka commented Jan 9, 2024

I believe it's crucial for the developer community to have this issue resolved, especially since the necessary code to fix it is already available. Documentation is incredibly important when building an API, and it's been three years since the release of .NET 5 with record types. In my opinion, prioritizing the resolution and merging of this issue might be more important than adding new features.

@martincostello martincostello added this to the v6.5.1 milestone Apr 13, 2024
jcracknell pushed a commit to jcracknell/Swashbuckle.AspNetCore that referenced this issue Apr 15, 2024
Add support for reading summary and example from default type constructor for C# 9 records.
Resolves domaindrivendev#1920.
---------

Co-authored-by: pixellos <codereinvented@gmail.com>
@martincostello martincostello modified the milestones: v6.5.1, v6.6.0 Apr 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
p2 Medium priority
Projects
None yet
Development

No branches or pull requests

9 participants