Repository navigation
Skip open generic types when finding subtypes - #4245
martincostello merged 3 commits into
Conversation
RaphaelFakhri
left a comment
There was a problem hiding this comment.
I checked out 5e8237d and ran it on net10.0.
Swashbuckle.AspNetCore.Newtonsoft.Test: 138 of 138 pass.Swashbuckle.AspNetCore.SwaggerGen.Test: 808 of 808 pass.- With the
DefaultSubTypesSelectorchange reverted, both new tests fail. Newtonsoft throwsArgumentException: Invalid type owner for DynamicMethod, and System.Text.Json addsOpenGenericSubType1andT` to the schemas.
I also ran the exact types from #2375 (base and generic subtype both with constructors) through GenerateSchema:
| Options | Serializer | master 9ab1b7c | this PR |
|---|---|---|---|
UseAllOfForInheritance |
Newtonsoft | ArgumentException |
SuccessResponse only |
UseOneOfForPolymorphism |
Newtonsoft | ArgumentException |
SuccessResponse only |
| both | System.Text.Json | adds SuccessDetailsResponse1andT` |
SuccessResponse only |
The change also fixes the UseOneOfForPolymorphism path, since both options use the same selector. Consider a test for UseOneOfForPolymorphism too, so the fix stays covered there. I have no other changes to suggest.
|
Thanks for running it against the exact types from the issue, and good catch on |
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the change excluding open generic types from default subtype discovery.
No open issues 🎉
What changed in this PR
Addresses #2375 by excluding types with unassigned generic parameters from default subtype discovery, preventing schema-generation failures or invalid schemas while retaining closed subtypes.
Changes:
- Filters open generic types from the default subtype selector without changing custom selectors.
- Adds regression tests for inheritance and polymorphism with both System.Text.Json and Newtonsoft.Json.
- Adds shared fixtures covering open generic and closed subtype hierarchies.
| File | Description |
|---|---|
test/Swashbuckle.AspNetCore.TestSupport/Fixtures/GenericSubTypes.cs |
Adds shared generic subtype fixtures. |
test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs |
Tests open-generic exclusion for both options with System.Text.Json. |
test/Swashbuckle.AspNetCore.Newtonsoft.Test/SchemaGenerator/NewtonsoftSchemaGeneratorTests.cs |
Tests the same scenarios with Newtonsoft.Json. |
src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/SchemaGeneratorOptions.cs |
Excludes types containing unassigned generic parameters from default subtype discovery. |
Review effort: Balanced
martincostello
left a comment
There was a problem hiding this comment.
Please add a snapshot test for this change so it's easy to visualise the resulting serialized OpenAPI document.
|
Done. I only have .NET 10 here, so I generated the |
|
A quick update: I've installed .NET 8 and 9 and run both test projects on them, and the |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #4245 +/- ##
=======================================
Coverage 91.77% 91.77%
=======================================
Files 108 108
Lines 4072 4072
Branches 914 915 +1
=======================================
Hits 3737 3737
Misses 189 189
Partials 146 146
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
|
Thanks for your contribution @mohsinaliwork686-rgb - the changes from this pull request have been published as part of version 11.0.0 📦, which is now available from NuGet.org 🚀 |
Pull Request
The issue or feature being addressed
Fixes #2375
Details on the issue fix or feature implementation
With
UseAllOfForInheritance, the default subtype selector returned open generic types such asSuccessDetailsResponse<T>. Those can't have a schema of their own. With Newtonsoft that threwArgumentException: Invalid type owner for DynamicMethod, and with System.Text.Json it added schemas for the open type and forT.UseOneOfForPolymorphismuses the same selector and failed the same way.DefaultSubTypesSelectornow skips types with unassigned generic parameters. Closed subtypes, such asclass Foo : Bar<int>, are still found, and a customSelectSubTypesUsingisn't affected.Tests are in
NewtonsoftSchemaGeneratorTestsandJsonSerializerSchemaGeneratorTests, for both options, and all four fail without the change.GenerateSchema_UseAllOfForInheritance_SkipsOpenGenericSubTypesinVerifyTestsadds a snapshot of the resulting document, which also fails without the change.Swashbuckle.AspNetCore.Newtonsoft.Test(139) andSwashbuckle.AspNetCore.SwaggerGen.Test(810) pass onnet8.0,net9.0andnet10.0, including the snapshot on each. I didn't run the integration tests.PR Checklist
Note
This PR was written with Claude Code, which wrote the fix, the tests, this description and my replies to review. I reviewed the change before opening it, and each update before pushing it.