Skip to content

Fix handling of duplicate enum values for dictionaries#3839

Merged
martincostello merged 2 commits intomasterfrom
gh-3742
Mar 12, 2026
Merged

Fix handling of duplicate enum values for dictionaries#3839
martincostello merged 2 commits intomasterfrom
gh-3742

Conversation

@martincostello
Copy link
Copy Markdown
Collaborator

Pull Request

The issue or feature being addressed

Resolves #2956.

Details on the issue fix or feature implementation

Add Distinct() to remove repeated declarations of the same enum values to avoid ArgumentException.

Supersedes #3742.

olegtarasov and others added 2 commits January 10, 2026 15:06
This fixes #2956 by literally adding a single `Distinct()` :)  Right now we have to maintain our own version of the resolver just to not trip on this bug.
Add test for #2956.
@martincostello martincostello added this to the v10.1.6 milestone Mar 12, 2026
Copilot AI review requested due to automatic review settings March 12, 2026 10:46
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a Swagger schema generation regression when dictionary keys are enums that contain multiple named members with the same underlying value (enum aliases), preventing ArgumentException during ToDictionary key materialization.

Changes:

  • Deduplicate serialized enum key representations via Distinct() when building known dictionary keys in JsonSerializerDataContractResolver.
  • Add a regression test covering dictionary schema generation for enums with duplicate underlying values.
  • Add a new test fixture enum that includes duplicate underlying values (alias member).

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
test/Swashbuckle.AspNetCore.TestSupport/Fixtures/Enums.cs Adds an enum fixture with aliased/duplicate underlying values for regression coverage.
test/Swashbuckle.AspNetCore.SwaggerGen.Test/SchemaGenerator/JsonSerializerSchemaGeneratorTests.cs Adds a regression test asserting duplicate enum values don’t break dictionary schema generation.
src/Swashbuckle.AspNetCore.SwaggerGen/SchemaGenerator/JsonSerializerDataContractResolver.cs Applies Distinct() to enum key JSON values to avoid duplicate dictionary keys.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 12, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 94.95%. Comparing base (c6a3c78) to head (889e256).
⚠️ Report is 80 commits behind head on master.
✅ All tests successful. No failed tests found.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #3839      +/-   ##
==========================================
+ Coverage   94.67%   94.95%   +0.28%     
==========================================
  Files         111      111              
  Lines        3868     3886      +18     
  Branches      780      783       +3     
==========================================
+ Hits         3662     3690      +28     
+ Misses        206      196      -10     
Flag Coverage Δ
Linux 94.95% <100.00%> (+0.28%) ⬆️
Windows 94.95% <100.00%> (+0.28%) ⬆️
macOS 94.95% <100.00%> (+0.28%) ⬆️

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

☔ View full report in Codecov by Sentry.
📢 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 4c2a849 into master Mar 12, 2026
19 checks passed
@martincostello martincostello deleted the gh-3742 branch March 12, 2026 10:58
This was referenced Mar 24, 2026
This was referenced Apr 23, 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.

[Bug]: Breaking change with Dictionary of Enum, when Enum has multiple values with same ordinal

3 participants