Fix missing OpenAPI 3.1 $ref properties#3859
Conversation
Fix missing properties on OpenAPI 3.1 schema references (`default`, `deprecated`). Resolves #3858.
There was a problem hiding this comment.
Pull request overview
This PR addresses issue #3858 by ensuring OpenAPI 3.1 schemas can retain certain “sibling” properties (default, deprecated) even when the schema is emitted as a $ref, improving fidelity for referenced enums and other referenced schemas.
Changes:
- Update
SchemaGeneratorto emitdefault/deprecatedon OpenAPI 3.1$refschemas (when not usingallOfwrapping). - Extend integration test coverage to snapshot OpenAPI 2.0 and 3.1 outputs for the Basic test site.
- Add/adjust fixtures and unit tests to cover enum default values and deprecated properties.
Reviewed changes
Copilot reviewed 20 out of 20 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs | Adds default/deprecated support on referenced member schemas without forcing allOf. |
| test/WebSites/Basic/Controllers/SwaggerAnnotationsController.cs | Adds enum default and an obsolete property to exercise $ref + default/deprecated behavior. |
| test/WebSites/Basic/Controllers/DefaultValuesAttribute.cs | Removes unused custom attribute in favor of System.ComponentModel.DefaultValueAttribute. |
| test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithDefaultAttributes.cs | Adds an enum property with DefaultValue for unit test coverage. |
| test/Swashbuckle.AspNetCore.TestSupport/Fixtures/TypeWithDefaultAttributeOnEnum.cs | Minor formatting update in fixture (no functional change). |
| test/Swashbuckle.AspNetCore.TestSupport/Fixtures/ComplexType.cs | Adds an enum property with DefaultValue and updates usings for schema-generation tests. |
| test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs | Updates schema assertions and adds enum default test coverage. |
| test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs | Updates schema assertions to account for the new ComplexType property. |
| test/Swashbuckle.AspNetCore.IntegrationTests/VerifyTests.cs | Adds snapshot coverage for Basic’s 2.0 and 3.1 endpoints and disambiguates snapshot parameters. |
| test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerIntegrationTests.cs | Includes the additional Basic endpoints in integration coverage. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/code/GeneratesValidClient_6548ff89f26311aa/Models/Cart.verified.cs | Snapshot updated for the newly added model property. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/8_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt | Snapshot updated for the added model property. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/8_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=2.0.verified.txt | New snapshot covering OpenAPI 2.0 output. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/8_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=3.1.verified.txt | New snapshot covering OpenAPI 3.1 output (incl. $ref sibling props). |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/9_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt | Snapshot updated for the added model property. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/9_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=2.0.verified.txt | New snapshot covering OpenAPI 2.0 output. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/9_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=3.1.verified.txt | New snapshot covering OpenAPI 3.1 output (incl. $ref sibling props). |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/10_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=v1.verified.txt | Snapshot updated for the added model property. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/10_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=2.0.verified.txt | New snapshot covering OpenAPI 2.0 output. |
| test/Swashbuckle.AspNetCore.IntegrationTests/snapshots/10_0/VerifyTests.SwaggerEndpoint_ReturnsValidSwaggerJson_startupType=Basic.Startup_swaggerRequestUri=3.1.verified.txt | New snapshot covering OpenAPI 3.1 output (incl. $ref sibling props). |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGenerator.cs
Show resolved
Hide resolved
Address review comment and add missing code to handle `[DefaultValue]` for schema references on parameters.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3859 +/- ##
==========================================
+ Coverage 94.95% 94.97% +0.02%
==========================================
Files 111 111
Lines 3886 3903 +17
Branches 783 789 +6
==========================================
+ Hits 3690 3707 +17
Misses 196 196
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Refactor to halve the number of calls to `Type.GetGenericTypeDefinition()` in the worst case.
Fix missing properties on OpenAPI 3.1 schema references (
default,deprecated).Resolves #3858.