| | | 1 | | using Microsoft.OpenApi; |
| | | 2 | | using System.Text.Json.Nodes; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.OpenApi; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Helper methods for creating shallow copies of OpenAPI components. |
| | | 8 | | /// </summary> |
| | | 9 | | internal static class OpenApiComponentShallowCopy |
| | | 10 | | { |
| | | 11 | | /// <summary> |
| | | 12 | | /// Converts an OpenApiRequestBody to an OpenApiSchema. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <param name="requestBody">The OpenApiRequestBody to convert.</param> |
| | | 15 | | /// <returns>An OpenApiSchema representing the request body.</returns> |
| | | 16 | | internal static OpenApiSchema ConvertToSchema(this OpenApiRequestBody requestBody) |
| | | 17 | | { |
| | 1 | 18 | | var clone = new OpenApiSchema |
| | 1 | 19 | | { |
| | 1 | 20 | | Description = requestBody.Description, |
| | 1 | 21 | | Properties = requestBody.Content?.Values.FirstOrDefault()?.Schema?.Properties.CreateShallowCopy(), |
| | 1 | 22 | | Extensions = requestBody.Extensions.CreateShallowCopy() |
| | 1 | 23 | | }; |
| | 1 | 24 | | return clone; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Creates a shallow copy of a dictionary of OpenApiExtension instances. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <param name="extensions">The dictionary to copy.</param> |
| | | 31 | | /// <returns>A new dictionary with shallow copies of the OpenApiExtension instances.</returns> |
| | | 32 | | internal static IDictionary<string, IOpenApiExtension>? CreateShallowCopy(this IDictionary<string, IOpenApiExtension |
| | | 33 | | { |
| | 2 | 34 | | if (extensions == null) |
| | | 35 | | { |
| | 1 | 36 | | return null; |
| | | 37 | | } |
| | | 38 | | |
| | 1 | 39 | | var clone = new Dictionary<string, IOpenApiExtension>(); |
| | 4 | 40 | | foreach (var kvp in extensions) |
| | | 41 | | { |
| | 1 | 42 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 43 | | } |
| | 1 | 44 | | return clone; |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Creates a shallow copy of an OpenApiExtension instance. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="extension">The OpenApiExtension to copy.</param> |
| | | 51 | | /// <returns>A new OpenApiExtension instance with the same properties as the input extension.</returns> |
| | | 52 | | /// <exception cref="InvalidOperationException">Thrown when the extension is of an unsupported type.</exception> |
| | | 53 | | internal static IOpenApiExtension CreateShallowCopy(this IOpenApiExtension extension) |
| | | 54 | | { |
| | 3 | 55 | | ArgumentNullException.ThrowIfNull(extension); |
| | | 56 | | |
| | 3 | 57 | | if (extension is JsonNodeExtension jsonNodeExtension) |
| | | 58 | | { |
| | 2 | 59 | | var nodeClone = DeepClone(jsonNodeExtension.Node) |
| | 2 | 60 | | ?? throw new InvalidOperationException("Unsupported IOpenApiExtension implementation."); |
| | 2 | 61 | | return new JsonNodeExtension(nodeClone); |
| | | 62 | | } |
| | | 63 | | |
| | 1 | 64 | | throw new InvalidOperationException("Unsupported IOpenApiExtension implementation."); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <summary> |
| | | 68 | | /// Creates a shallow copy of a dictionary of IOpenApiHeader instances. |
| | | 69 | | /// </summary> |
| | | 70 | | /// <param name="headers">The dictionary of headers to copy.</param> |
| | | 71 | | /// <returns>A new dictionary with shallow copies of the IOpenApiHeader instances.</returns> |
| | | 72 | | internal static IDictionary<string, IOpenApiHeader>? CreateShallowCopy(this IDictionary<string, IOpenApiHeader>? hea |
| | | 73 | | { |
| | 0 | 74 | | if (headers == null) |
| | | 75 | | { |
| | 0 | 76 | | return null; |
| | | 77 | | } |
| | | 78 | | |
| | 0 | 79 | | var clone = new Dictionary<string, IOpenApiHeader>(); |
| | 0 | 80 | | foreach (var kvp in headers) |
| | | 81 | | { |
| | 0 | 82 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 83 | | } |
| | 0 | 84 | | return clone; |
| | | 85 | | } |
| | | 86 | | |
| | | 87 | | /// <summary> |
| | | 88 | | /// Creates a shallow copy of a dictionary of OpenApiMediaType instances. |
| | | 89 | | /// </summary> |
| | | 90 | | /// <param name="content">The dictionary to copy.</param> |
| | | 91 | | /// <returns>A new dictionary with shallow copies of the OpenApiMediaType instances.</returns> |
| | | 92 | | internal static IDictionary<string, IOpenApiMediaType>? CreateShallowCopy(this IDictionary<string, IOpenApiMediaType |
| | | 93 | | { |
| | 0 | 94 | | if (content == null) |
| | | 95 | | { |
| | 0 | 96 | | return null; |
| | | 97 | | } |
| | | 98 | | |
| | 0 | 99 | | var clone = new Dictionary<string, IOpenApiMediaType>(); |
| | 0 | 100 | | foreach (var kvp in content) |
| | | 101 | | { |
| | 0 | 102 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 103 | | } |
| | 0 | 104 | | return clone; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Creates a shallow copy of a dictionary of OpenApiExample instances. |
| | | 109 | | /// </summary> |
| | | 110 | | /// <param name="examples">The dictionary to copy.</param> |
| | | 111 | | /// <returns>A new dictionary with shallow copies of the OpenApiExample instances.</returns> |
| | | 112 | | internal static IDictionary<string, IOpenApiExample>? CreateShallowCopy(this IDictionary<string, IOpenApiExample>? e |
| | | 113 | | { |
| | 0 | 114 | | if (examples == null) |
| | | 115 | | { |
| | 0 | 116 | | return null; |
| | | 117 | | } |
| | | 118 | | |
| | 0 | 119 | | var clone = new Dictionary<string, IOpenApiExample>(); |
| | 0 | 120 | | foreach (var kvp in examples) |
| | | 121 | | { |
| | 0 | 122 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 123 | | } |
| | 0 | 124 | | return clone; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | /// <summary> |
| | | 128 | | /// Creates a shallow copy of a list of OpenApiSchema instances. |
| | | 129 | | /// </summary> |
| | | 130 | | /// <param name="schemas">The list to copy.</param> |
| | | 131 | | /// <returns>A new list containing shallow copies of the OpenApiSchema instances.</returns> |
| | | 132 | | internal static IList<IOpenApiSchema>? CreateShallowCopy(this IList<IOpenApiSchema>? schemas) |
| | | 133 | | { |
| | 0 | 134 | | if (schemas == null) |
| | | 135 | | { |
| | 0 | 136 | | return null; |
| | | 137 | | } |
| | 0 | 138 | | var cloneList = new List<IOpenApiSchema>(); |
| | 0 | 139 | | foreach (var schema in schemas) |
| | | 140 | | { |
| | 0 | 141 | | cloneList.Add(schema.CreateShallowCopy()); |
| | | 142 | | } |
| | 0 | 143 | | return cloneList; |
| | | 144 | | } |
| | | 145 | | /// <summary> |
| | | 146 | | /// Creates a shallow copy of a dictionary of OpenApiSchema instances. |
| | | 147 | | /// </summary> |
| | | 148 | | /// <param name="schemas">The dictionary to copy.</param> |
| | | 149 | | /// <returns>A new dictionary containing shallow copies of the OpenApiSchema instances.</returns> |
| | | 150 | | internal static Dictionary<string, IOpenApiSchema>? CreateShallowCopy(this IDictionary<string, IOpenApiSchema>? sche |
| | | 151 | | { |
| | 1 | 152 | | if (schemas == null) |
| | | 153 | | { |
| | 0 | 154 | | return null; |
| | | 155 | | } |
| | 1 | 156 | | var clone = new Dictionary<string, IOpenApiSchema>(); |
| | 4 | 157 | | foreach (var kvp in schemas) |
| | | 158 | | { |
| | 1 | 159 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 160 | | } |
| | 1 | 161 | | return clone; |
| | | 162 | | } |
| | | 163 | | |
| | | 164 | | /// <summary> |
| | | 165 | | /// Creates a shallow copy of a dictionary of IOpenApiLink instances. |
| | | 166 | | /// </summary> |
| | | 167 | | /// <param name="links">The dictionary of IOpenApiLink instances to copy.</param> |
| | | 168 | | /// <returns>A new dictionary containing shallow copies of the IOpenApiLink instances.</returns> |
| | | 169 | | internal static IDictionary<string, IOpenApiLink>? CreateShallowCopy(this IDictionary<string, IOpenApiLink>? links) |
| | | 170 | | { |
| | 0 | 171 | | if (links == null) |
| | | 172 | | { |
| | 0 | 173 | | return null; |
| | | 174 | | } |
| | | 175 | | |
| | 0 | 176 | | var clone = new Dictionary<string, IOpenApiLink>(); |
| | 0 | 177 | | foreach (var kvp in links) |
| | | 178 | | { |
| | 0 | 179 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 180 | | } |
| | 0 | 181 | | return clone; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | /// <summary> |
| | | 185 | | /// Creates a shallow copy of an OpenApiServer instance. |
| | | 186 | | /// </summary> |
| | | 187 | | /// <param name="server">The OpenApiServer instance to copy.</param> |
| | | 188 | | /// <returns>A new OpenApiServer instance with the same properties as the input instance.</returns> |
| | | 189 | | internal static OpenApiServer CreateShallowCopy(this OpenApiServer server) |
| | | 190 | | { |
| | 1 | 191 | | var clone = new OpenApiServer |
| | 1 | 192 | | { |
| | 1 | 193 | | Url = server.Url, |
| | 1 | 194 | | Description = server.Description, |
| | 1 | 195 | | Extensions = server.Extensions.CreateShallowCopy() |
| | 1 | 196 | | }; |
| | | 197 | | // Clone Variables dictionary if it exists |
| | 1 | 198 | | if (server.Variables != null) |
| | | 199 | | { |
| | 1 | 200 | | clone.Variables = new Dictionary<string, OpenApiServerVariable>(); |
| | 4 | 201 | | foreach (var variable in server.Variables) |
| | | 202 | | { |
| | 1 | 203 | | clone.Variables[variable.Key] = new OpenApiServerVariable(variable.Value); |
| | | 204 | | } |
| | | 205 | | } |
| | 1 | 206 | | return clone; |
| | | 207 | | } |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Creates a shallow copy of a RuntimeExpressionAnyWrapper instance. |
| | | 211 | | /// </summary> |
| | | 212 | | /// <param name="expressionWrapper">The RuntimeExpressionAnyWrapper instance to copy.</param> |
| | | 213 | | /// <returns>A new RuntimeExpressionAnyWrapper instance with the same properties as the input instance.</returns> |
| | | 214 | | internal static RuntimeExpressionAnyWrapper CreateShallowCopy(this RuntimeExpressionAnyWrapper expressionWrapper) |
| | | 215 | | { |
| | 1 | 216 | | return new RuntimeExpressionAnyWrapper |
| | 1 | 217 | | { |
| | 1 | 218 | | Expression = expressionWrapper.Expression, |
| | 1 | 219 | | Any = expressionWrapper.Any != null ? DeepClone(expressionWrapper.Any) : null |
| | 1 | 220 | | }; |
| | | 221 | | } |
| | | 222 | | |
| | | 223 | | /// <summary> |
| | | 224 | | /// Creates a shallow copy of a dictionary of RuntimeExpressionAnyWrapper instances. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <param name="parameters">The dictionary of RuntimeExpressionAnyWrapper instances to copy.</param> |
| | | 227 | | /// <returns>A new dictionary containing shallow copies of the RuntimeExpressionAnyWrapper instances.</returns> |
| | | 228 | | internal static IDictionary<string, RuntimeExpressionAnyWrapper>? CreateShallowCopy(this IDictionary<string, Runtime |
| | | 229 | | { |
| | 1 | 230 | | if (parameters == null) |
| | | 231 | | { |
| | 1 | 232 | | return null; |
| | | 233 | | } |
| | | 234 | | |
| | 0 | 235 | | var clone = new Dictionary<string, RuntimeExpressionAnyWrapper>(); |
| | 0 | 236 | | foreach (var kvp in parameters) |
| | | 237 | | { |
| | 0 | 238 | | clone[kvp.Key] = kvp.Value.CreateShallowCopy(); |
| | | 239 | | } |
| | 0 | 240 | | return clone; |
| | | 241 | | } |
| | | 242 | | |
| | | 243 | | /// <summary> |
| | | 244 | | /// Clones a JsonNode instance. |
| | | 245 | | /// </summary> |
| | | 246 | | /// <param name="value">The JsonNode to clone.</param> |
| | | 247 | | /// <returns>A new JsonNode instance that is a deep clone of the input value.</returns> |
| | 3 | 248 | | internal static JsonNode? DeepClone(JsonNode? value) => value?.DeepClone(); |
| | | 249 | | } |