Implement deep copy for OpenApiSchema instances #57223
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #56990.
As the last transformation we apply to the OpenAPI document, we utilize the
OpenApiSchemaReferenceTransformer
to replace inlined schemas in the documents with reference schemas that reference the schema definition in the top-leveldocument.schemas
registery. As part of this process, we map schemas store in theOpenApiSchemaStore
cache to elements in the OpenAPI document'scomponents.schemas
property as seen below.aspnetcore/src/OpenApi/src/Transformers/Implementations/OpenApiSchemaReferenceTransformer.cs
Lines 18 to 37 in 257d690
The implementation currently uses the copy constructor implementation on
OpenApiSchema
to avoid making mutations to instances in the cache that we copy from that are used later in the application.aspnetcore/src/OpenApi/src/Transformers/Implementations/OpenApiSchemaReferenceTransformer.cs
Lines 30 to 36 in 257d690
However, the copy constructor implementation for
OpenApiSchema
executes a shallow copy on sub-schemas within a top-levelOpenApiSchema
which means that modiciations to the copy made inResolveReferenceForSchema
end up impacting the instances in the cache.To resolve this issue, we implement a deep copy implementation for
OpenApiSchema
so that subschemas in the copied instance can be modified without impacting the original instance.