-
Notifications
You must be signed in to change notification settings - Fork 10.4k
Implement deep copy for OpenApiSchema instances #57223
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d0f0b3b
Implement deep copy for OpenApiSchema instances
captainsafia b1019d7
Add more tests, use new List(), and fix reference comparer
captainsafia 60d3c8d
Add comment and clean up null check
captainsafia 57b832f
Update benchmark method return values
captainsafia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Interfaces; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
internal static class OpenApiSchemaExtensions | ||
{ | ||
/// <summary> | ||
/// Generates a deep copy of a given <see cref="OpenApiSchema"/> instance. | ||
/// </summary> | ||
/// <remarks> | ||
/// The copy constructors of the <see cref="OpenApiSchema"/> class do not perform a deep | ||
/// copy of the instance which presents a problem whe making modifications in deeply nested | ||
/// subschemas. This extension implements a deep copy on <see cref="OpenApiSchema" /> to guarantee | ||
/// that modifications on cloned subschemas do not affect the original subschema. | ||
/// /// </remarks> | ||
/// <param name="schema">The <see cref="OpenApiSchema"/> to generate a deep copy of.</param> | ||
public static OpenApiSchema Clone(this OpenApiSchema schema) | ||
{ | ||
return new OpenApiSchema | ||
{ | ||
Title = schema.Title, | ||
Type = schema.Type, | ||
Format = schema.Format, | ||
Description = schema.Description, | ||
Maximum = schema.Maximum, | ||
ExclusiveMaximum = schema.ExclusiveMaximum, | ||
Minimum = schema.Minimum, | ||
ExclusiveMinimum = schema.ExclusiveMinimum, | ||
MaxLength = schema.MaxLength, | ||
MinLength = schema.MinLength, | ||
Pattern = schema.Pattern, | ||
MultipleOf = schema.MultipleOf, | ||
Default = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(schema.Default), | ||
ReadOnly = schema.ReadOnly, | ||
WriteOnly = schema.WriteOnly, | ||
AllOf = schema.AllOf != null ? new List<OpenApiSchema>(schema.AllOf.Select(s => s.Clone())) : null, | ||
OneOf = schema.OneOf != null ? new List<OpenApiSchema>(schema.OneOf.Select(s => s.Clone())) : null, | ||
AnyOf = schema.AnyOf != null ? new List<OpenApiSchema>(schema.AnyOf.Select(s => s.Clone())) : null, | ||
Not = schema.Not?.Clone(), | ||
Required = schema.Required != null ? new HashSet<string>(schema.Required) : null, | ||
Items = schema.Items?.Clone(), | ||
MaxItems = schema.MaxItems, | ||
MinItems = schema.MinItems, | ||
UniqueItems = schema.UniqueItems, | ||
Properties = schema.Properties?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Clone()), | ||
MaxProperties = schema.MaxProperties, | ||
MinProperties = schema.MinProperties, | ||
AdditionalPropertiesAllowed = schema.AdditionalPropertiesAllowed, | ||
AdditionalProperties = schema.AdditionalProperties?.Clone(), | ||
Discriminator = schema.Discriminator != null ? new(schema.Discriminator) : null, | ||
Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(schema.Example), | ||
Enum = schema.Enum != null ? new List<IOpenApiAny>(schema.Enum) : null, | ||
amcasey marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Nullable = schema.Nullable, | ||
ExternalDocs = schema.ExternalDocs != null ? new(schema.ExternalDocs) : null, | ||
Deprecated = schema.Deprecated, | ||
Xml = schema.Xml != null ? new(schema.Xml) : null, | ||
Extensions = schema.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null, | ||
UnresolvedReference = schema.UnresolvedReference, | ||
Reference = schema.Reference != null ? new(schema.Reference) : null, | ||
Annotations = schema.Annotations != null ? new Dictionary<string, object>(schema.Annotations) : null, | ||
}; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.