-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Fix attributes on primary constructor parameters in OpenAPI documents #62027
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
Conversation
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
@copilot Good start! Can you update the implementation so that attributes work even when they don't target the property. For example, the example below should pass.
|
…ameters Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
... I've updated the implementation to support validation attributes without the
This should now handle the example case you provided with attributes that don't use the Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
@copilot Can you remove the PrimaryConstructorAttributesTest test and make sure that the code that is used to discover the attributes is trim-friendly? |
Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
I've removed the PrimaryConstructorAttributesTest and made the attribute discovery code trim-friendly in commit 85468ea. The main changes:
The unit tests in OpenApiSchemaService.PrimaryConstructorSchemas.cs are still in place to verify the functionality. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
@copilot Instead of using the private static void ApplyCtorAttributes(
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
Type dtoType,
JsonObject properties)
{
foreach (var ctor in dtoType.GetConstructors( /* public instance only */ ))
{
...
}
} |
…dly code Co-authored-by: captainsafia <1857993+captainsafia@users.noreply.github.com>
I've updated the implementation to use
This approach preserves the same functionality while ensuring the code is properly annotated for trimming. Changes are in commit 4cc431c. Warning Firewall rules blocked me from connecting to one or more addressesI tried to connect to the following addresses, but was blocked by firewall rules:
If you need me to access, download, or install something from one of these locations, you can either:
|
Problem
Currently, validation attributes (like
[Range]
) on primary constructor parameters in class types aren't being reflected in generated OpenAPI documents. While record types correctly show these attributes, class types don't:This inconsistency is occurring because record primary constructors auto-generate properties that carry the attributes, but class primary constructors don't.
Solution
The implementation enhances the schema generation process to:
This ensures consistency between validation behavior and OpenAPI documentation, particularly for class types using C# 12's primary constructor feature.
Testing
Added tests in two forms:
Fixes #61538.
Warning
Firewall rules blocked me from connecting to one or more addresses
I tried to connect to the following addresses, but was blocked by firewall rules:
dc.services.visualstudio.com
/home/REDACTED/work/aspnetcore/aspnetcore/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/aspnetcore/aspnetcore/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/aspnetcore/aspnetcore/artifacts/toolset/10.0.0-beta.25265.101.txt
(dns block)pkgs.dev.azure.com
/home/REDACTED/work/aspnetcore/aspnetcore/.dotnet/dotnet msbuild /m /nologo /clp:Summary /v:minimal /nr:true /warnaserror /p:TreatWarningsAsErrors=true /p:ContinuousIntegrationBuild=false /home/REDACTED/work/aspnetcore/aspnetcore/artifacts/toolset/restore.proj /t:__WriteToolsetLocation /clp:ErrorsOnly;NoSummary /p:__ToolsetLocationOutputFile=/home/REDACTED/work/aspnetcore/aspnetcore/artifacts/toolset/10.0.0-beta.25265.101.txt
(dns block)If you need me to access, download, or install something from one of these locations, you can either:
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.