Skip to content

Skip open generic types when finding subtypes - #4245

Merged
martincostello merged 3 commits into
domaindrivendev:masterfrom
mohsinaliwork686-rgb:fix/skip-open-generic-subtypes
Nov 7, 2026
Merged

martincostello merged 3 commits into
domaindrivendev:masterfrom
mohsinaliwork686-rgb:fix/skip-open-generic-subtypes

Conversation

@mohsinaliwork686-rgb

@mohsinaliwork686-rgb mohsinaliwork686-rgb commented Nov 7, 2026 •

Copy link
Copy Markdown
Contributor

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 as SuccessDetailsResponse<T>. Those can't have a schema of their own. With Newtonsoft that threw ArgumentException: Invalid type owner for DynamicMethod, and with System.Text.Json it added schemas for the open type and for T.

UseOneOfForPolymorphism uses the same selector and failed the same way. DefaultSubTypesSelector now skips types with unassigned generic parameters. Closed subtypes, such as class Foo : Bar<int>, are still found, and a custom SelectSubTypesUsing isn't affected.

Tests are in NewtonsoftSchemaGeneratorTests and JsonSerializerSchemaGeneratorTests, for both options, and all four fail without the change. GenerateSchema_UseAllOfForInheritance_SkipsOpenGenericSubTypes in VerifyTests adds a snapshot of the resulting document, which also fails without the change.

Swashbuckle.AspNetCore.Newtonsoft.Test (139) and Swashbuckle.AspNetCore.SwaggerGen.Test (810) pass on net8.0, net9.0 and net10.0, including the snapshot on each. I didn't run the integration tests.


PR Checklist

  • I have read and followed the Contributing guide.
  • I have added tests for my changes where appropriate.
  • I have the experience and knowledge necessary to understand, review, and validate all content in this PR.
  • This PR has content that I did not fully write myself.

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.

@RaphaelFakhri RaphaelFakhri left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 DefaultSubTypesSelector change reverted, both new tests fail. Newtonsoft throws ArgumentException: Invalid type owner for DynamicMethod, and System.Text.Json adds OpenGenericSubType1andT` 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.

@mohsinaliwork686-rgb

Copy link
Copy Markdown
Contributor Author

Thanks for running it against the exact types from the issue, and good catch on UseOneOfForPolymorphism. I've added a test for it with both serializers, and both fail without the change.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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 martincostello left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a snapshot test for this change so it's easy to visualise the resulting serialized OpenAPI document.

@mohsinaliwork686-rgb

Copy link
Copy Markdown
Contributor Author

Done. GenerateSchema_UseAllOfForInheritance_SkipsOpenGenericSubTypes adds a snapshot. The document has GenericSubTypesBaseType, ClosedGenericSubType and Int32OpenGenericSubType, with no OpenGenericSubType or T.

I only have .NET 10 here, so I generated the 10_0 snapshot and copied it to 8_0 and 9_0. The existing snapshots are identical across all three, but those two copies haven't been run yet.

@mohsinaliwork686-rgb

Copy link
Copy Markdown
Contributor Author

A quick update: I've installed .NET 8 and 9 and run both test projects on them, and the 8_0 and 9_0 snapshots pass, so they're no longer untested.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues are recorded for the change excluding open generic subtypes from default discovery.

No open issues 🎉

Review effort: Balanced

@martincostello martincostello added this to the v11.0.0 milestone Nov 7, 2026
@codecov

codecov Bot commented Nov 7, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 91.77%. Comparing base (9ab1b7c) to head (102e988).

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           
Flag Coverage Δ
Linux 91.77% <100.00%> (ø)
Windows 91.77% <100.00%> (ø)
macOS 91.77% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@martincostello
martincostello merged commit d27b767 into domaindrivendev:master Nov 7, 2026
14 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

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 🚀

This was referenced Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

UseAllOfForInheritance crashes on generic subtypes

4 participants