Skip to content

Fix OpenApiSchemaService to handle implementation different from Dictionary<,> for schema.Properties#67384

Open
marcominerva wants to merge 3 commits into
dotnet:mainfrom
marcominerva:ResolveReferenceForSchema-fix
Open

Fix OpenApiSchemaService to handle implementation different from Dictionary<,> for schema.Properties#67384
marcominerva wants to merge 3 commits into
dotnet:mainfrom
marcominerva:ResolveReferenceForSchema-fix

Conversation

@marcominerva

Copy link
Copy Markdown
Contributor

This pull request addresses a potential runtime exception when handling OpenAPI schema properties that are stored in collections sensitive to modification during enumeration (such as SortedDictionary). The core fix involves modifying the way properties are iterated and updated, and a test is added to ensure robustness.

Bug fix for property enumeration and update:

  • Changed the iteration over schema.Properties in OpenApiSchemaService.cs to first create a list of keys (schema.Properties.Keys.ToList()) and then update properties by key, preventing InvalidOperationException when schema.Properties is a collection like SortedDictionary that detects modification during enumeration.

Test coverage improvements:

  • Added a new test SchemaTransformer_WithSortedDictionaryProperties_DoesNotThrow in SchemaTransformerTests.cs to verify that ResolveReferenceForSchema does not throw when schema.Properties is a SortedDictionary, ensuring the fix is validated and preventing regressions.

Fixes #67086

Copilot AI review requested due to automatic review settings June 23, 2026 13:03
@marcominerva marcominerva requested a review from a team as a code owner June 23, 2026 13:03
@dotnet-policy-service dotnet-policy-service Bot added the community-contribution Indicates that the PR has been added by a community member label Jun 23, 2026
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

Thanks for your PR, @marcominerva. Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

Copilot AI 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.

Pull request overview

This PR fixes a runtime InvalidOperationException in OpenAPI schema reference resolution that can occur when schema.Properties is backed by an IDictionary implementation that detects modification during enumeration (e.g., SortedDictionary). It also adds a regression test to ensure schema transformers can swap in such a dictionary without breaking document generation.

Changes:

  • Update ResolveReferenceForSchema to iterate a snapshot of schema.Properties keys before mutating entries.
  • Add a test that replaces schema.Properties with a SortedDictionary during schema transformation and verifies OpenAPI document generation does not throw.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs Avoids mutating schema.Properties while enumerating it by iterating a key snapshot first.
src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Transformers/SchemaTransformerTests.cs Adds regression coverage for schema.Properties backed by a SortedDictionary.

Comment thread src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs Outdated
Comment thread src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using an implementation different from Dictionary<,> for schema.Properties breaks ResolveReferenceForSchema

3 participants