-
Notifications
You must be signed in to change notification settings - Fork 1.3k
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
Records (positional) and DataAnnotations #2212
Comments
Def a useful feature that will become more relevant with the adoption of records for API types. It's doable but non-trivial and I don't have the bandwidth to look into it at the moment. However, I would accept a PR if you're interested? But, if it's looking like a big refactor, I would recommend discussing and aligning on the approach before knocking out the PR. Off the top of my head, you would need to update the When retrieving custom attributes, you could do an additional check to determine if the member has a matching constructor parameter and if so include attributes on that parameter. Actually, we already do something similar but specifically for honoring Newtonsoft support for deserializing property values via the constructor. |
That Newtonsoft logic seems very promising... ctor support has been added to STJ in 5.x
As I'm currently very busy as well, I will have a go starting mid-October. I hope you don't mind having an issue open that long? |
Here's how I solved another issue with getting the StringLength attribute values from records for a specific property. Maybe something like this could be of use in Swashbuckle if it was a bit more generic?
|
In relation to this issue, created fix for positional record example/summary handling #2546 |
Should this issue have been closed? #2546 does not seem to solve the data annotation issue with C# records. |
My understanding was that it did - if it doesn't, please open a new issue and we can look into it. |
Not possible to just reopen? |
Either way, we'd like a repro so we can see exactly what doesn't seem to be working. |
The repro is already stated in this issue. |
I needed this even thought about contributing a fix. However after digging deeper it seems to me that this issue should be fixed in ASP.NET Core's OpenAPI support and not in Swashbuckle. In the meantime I use the following filter.
Note that it works for my API, it might need fixing for some cases and it lacks support for some things for example BigInteger properties, AllowedValues and DeniedValues and probably more |
I have a similar issue with the addition of AspNetCore that I did in #2658. |
Looking at this it seems that the problem is rooted in ASP.NET Core. |
I think that the problem could be caused by where are the CustomAttributes obtained. For records right now if they are part of the Body the CustomAttributes are obtained from the property, whereas if they are part of AsParameters they are obtained from the ctor. With the changes I did for showing the XML comments for AsParameters in both cases they are obtained from the Property |
Maybe we should open an issue on the ASP.NET Core repo |
Done: dotnet/aspnetcore#57486 |
I'd like to combine C# 9 positional records,
System.ComponentModel.DataAnnotations
, model validation andSwashbuckle.AspNetCore
(current latest 6.2.1) in a concise and convenient way:Ideally, this record
would produce
but currently, I get
Targeting the attribute to the underlying property (by adding
property:
to the attribute) fixes the OpenAPI definition but breaks model validation (i.e. we get a 200 for models that should return a 400).Known "workaround"s:
duplicate the attributes (usingdoes not work in .NET 9 (Preview 7) anymoreproperty
andparam
)init
) property-bag classes (read-only properties in classes with constructors do not show up in the generated OpenAPI definition).Is this something you would consider adding / having added (any blockers / difficulties you see) to Swashbuckle?
The text was updated successfully, but these errors were encountered: