|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using System.Linq; |
| 5 | +using Microsoft.OpenApi.Any; |
| 6 | +using Microsoft.OpenApi.Interfaces; |
| 7 | +using Microsoft.OpenApi.Models; |
| 8 | + |
| 9 | +namespace Microsoft.AspNetCore.OpenApi; |
| 10 | + |
| 11 | +internal static class OpenApiSchemaExtensions |
| 12 | +{ |
| 13 | + /// <summary> |
| 14 | + /// Generates a deep copy of a given <see cref="OpenApiSchema"/> instance. |
| 15 | + /// </summary> |
| 16 | + /// <remarks> |
| 17 | + /// The copy constructors of the <see cref="OpenApiSchema"/> class do not perform a deep |
| 18 | + /// copy of the instance which presents a problem whe making modifications in deeply nested |
| 19 | + /// subschemas. This extension implements a deep copy on <see cref="OpenApiSchema" /> to guarantee |
| 20 | + /// that modifications on cloned subschemas do not affect the original subschema. |
| 21 | + /// /// </remarks> |
| 22 | + /// <param name="schema">The <see cref="OpenApiSchema"/> to generate a deep copy of.</param> |
| 23 | + public static OpenApiSchema Clone(this OpenApiSchema schema) |
| 24 | + { |
| 25 | + return new OpenApiSchema |
| 26 | + { |
| 27 | + Title = schema.Title, |
| 28 | + Type = schema.Type, |
| 29 | + Format = schema.Format, |
| 30 | + Description = schema.Description, |
| 31 | + Maximum = schema.Maximum, |
| 32 | + ExclusiveMaximum = schema.ExclusiveMaximum, |
| 33 | + Minimum = schema.Minimum, |
| 34 | + ExclusiveMinimum = schema.ExclusiveMinimum, |
| 35 | + MaxLength = schema.MaxLength, |
| 36 | + MinLength = schema.MinLength, |
| 37 | + Pattern = schema.Pattern, |
| 38 | + MultipleOf = schema.MultipleOf, |
| 39 | + Default = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(schema.Default), |
| 40 | + ReadOnly = schema.ReadOnly, |
| 41 | + WriteOnly = schema.WriteOnly, |
| 42 | + AllOf = schema.AllOf != null ? new List<OpenApiSchema>(schema.AllOf.Select(s => s.Clone())) : null, |
| 43 | + OneOf = schema.OneOf != null ? new List<OpenApiSchema>(schema.OneOf.Select(s => s.Clone())) : null, |
| 44 | + AnyOf = schema.AnyOf != null ? new List<OpenApiSchema>(schema.AnyOf.Select(s => s.Clone())) : null, |
| 45 | + Not = schema.Not?.Clone(), |
| 46 | + Required = schema.Required != null ? new HashSet<string>(schema.Required) : null, |
| 47 | + Items = schema.Items?.Clone(), |
| 48 | + MaxItems = schema.MaxItems, |
| 49 | + MinItems = schema.MinItems, |
| 50 | + UniqueItems = schema.UniqueItems, |
| 51 | + Properties = schema.Properties?.ToDictionary(kvp => kvp.Key, kvp => kvp.Value.Clone()), |
| 52 | + MaxProperties = schema.MaxProperties, |
| 53 | + MinProperties = schema.MinProperties, |
| 54 | + AdditionalPropertiesAllowed = schema.AdditionalPropertiesAllowed, |
| 55 | + AdditionalProperties = schema.AdditionalProperties?.Clone(), |
| 56 | + Discriminator = schema.Discriminator != null ? new(schema.Discriminator) : null, |
| 57 | + Example = OpenApiAnyCloneHelper.CloneFromCopyConstructor<IOpenApiAny>(schema.Example), |
| 58 | + Enum = schema.Enum != null ? new List<IOpenApiAny>(schema.Enum) : null, |
| 59 | + Nullable = schema.Nullable, |
| 60 | + ExternalDocs = schema.ExternalDocs != null ? new(schema.ExternalDocs) : null, |
| 61 | + Deprecated = schema.Deprecated, |
| 62 | + Xml = schema.Xml != null ? new(schema.Xml) : null, |
| 63 | + Extensions = schema.Extensions != null ? new Dictionary<string, IOpenApiExtension>(schema.Extensions) : null, |
| 64 | + UnresolvedReference = schema.UnresolvedReference, |
| 65 | + Reference = schema.Reference != null ? new(schema.Reference) : null, |
| 66 | + Annotations = schema.Annotations != null ? new Dictionary<string, object>(schema.Annotations) : null, |
| 67 | + }; |
| 68 | + } |
| 69 | +} |
0 commit comments