| | | 1 | | using Microsoft.OpenApi; |
| | | 2 | | using System.Text.Json.Nodes; |
| | | 3 | | |
| | | 4 | | namespace Kestrun.OpenApi; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Helper methods for cloning OpenAPI components. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class OpenApiComponentClone |
| | | 10 | | { |
| | | 11 | | #region Parameter |
| | | 12 | | /// <summary> |
| | | 13 | | /// Clones an OpenApiParameterReference instance. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <param name="parameter">The OpenApiParameterReference to clone.</param> |
| | | 16 | | /// <returns>A new OpenApiParameterReference instance with the same properties as the input parameter.</returns> |
| | | 17 | | public static OpenApiParameterReference Clone(this OpenApiParameterReference parameter) |
| | | 18 | | { |
| | 0 | 19 | | var clone = new OpenApiParameterReference(parameter.Reference.Id!) |
| | 0 | 20 | | { |
| | 0 | 21 | | Description = parameter.Description, |
| | 0 | 22 | | }; |
| | 0 | 23 | | return clone; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Clones an OpenApiParameter instance. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="parameter">The OpenApiParameter to clone.</param> |
| | | 30 | | /// <returns>A new OpenApiParameter instance with the same properties as the input parameter.</returns> |
| | | 31 | | public static OpenApiParameter Clone(this OpenApiParameter parameter) |
| | | 32 | | { |
| | 1 | 33 | | var clone = new OpenApiParameter |
| | 1 | 34 | | { |
| | 1 | 35 | | Name = parameter.Name, |
| | 1 | 36 | | In = parameter.In, |
| | 1 | 37 | | Description = parameter.Description, |
| | 1 | 38 | | Required = parameter.Required, |
| | 1 | 39 | | Style = parameter.Style, |
| | 1 | 40 | | Explode = parameter.Explode, |
| | 1 | 41 | | AllowReserved = parameter.AllowReserved, |
| | 1 | 42 | | Schema = parameter.Schema?.Clone(), |
| | 1 | 43 | | Examples = parameter.Examples?.Clone(), |
| | 1 | 44 | | Example = JsonNodeClone(parameter.Example), |
| | 1 | 45 | | Content = parameter.Content?.Clone(), |
| | 1 | 46 | | Extensions = parameter.Extensions.Clone(), |
| | 1 | 47 | | AllowEmptyValue = parameter.AllowEmptyValue, |
| | 1 | 48 | | Deprecated = parameter.Deprecated |
| | 1 | 49 | | }; |
| | 1 | 50 | | return clone; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <summary> |
| | | 54 | | /// Clones an IOpenApiParameter instance. |
| | | 55 | | /// </summary> |
| | | 56 | | /// <param name="parameter">The IOpenApiParameter instance to clone.</param> |
| | | 57 | | /// <returns>A cloned instance of IOpenApiParameter.</returns> |
| | | 58 | | /// <exception cref="InvalidOperationException">Thrown when the IOpenApiParameter implementation is unsupported.</ex |
| | | 59 | | public static IOpenApiParameter Clone(this IOpenApiParameter parameter) |
| | | 60 | | { |
| | | 61 | | // Determine the actual type of IOpenApiParameter and clone accordingly |
| | 1 | 62 | | return parameter switch |
| | 1 | 63 | | { |
| | 0 | 64 | | OpenApiParameter param => param.Clone(), |
| | 0 | 65 | | OpenApiParameterReference paramRef => paramRef.Clone(), |
| | 1 | 66 | | _ => throw new InvalidOperationException("Unsupported IOpenApiParameter implementation."), |
| | 1 | 67 | | }; |
| | | 68 | | } |
| | | 69 | | #endregion |
| | | 70 | | #region RequestBody |
| | | 71 | | /// <summary> |
| | | 72 | | /// Clones an OpenApiRequestBodyReference instance. |
| | | 73 | | /// </summary> |
| | | 74 | | /// <param name="requestBody">The OpenApiRequestBodyReference to clone.</param> |
| | | 75 | | /// <returns>A new OpenApiRequestBodyReference instance with the same properties as the input requestBody.</returns> |
| | | 76 | | public static OpenApiRequestBodyReference Clone(this OpenApiRequestBodyReference requestBody) |
| | | 77 | | { |
| | 0 | 78 | | var clone = new OpenApiRequestBodyReference(requestBody.Reference.Id!) |
| | 0 | 79 | | { |
| | 0 | 80 | | Description = requestBody.Description, |
| | 0 | 81 | | }; |
| | 0 | 82 | | return clone; |
| | | 83 | | } |
| | | 84 | | /// <summary> |
| | | 85 | | /// Clones an OpenApiRequestBody instance. |
| | | 86 | | /// </summary> |
| | | 87 | | /// <param name="requestBody">The OpenApiRequestBody to clone.</param> |
| | | 88 | | /// <returns>A new OpenApiRequestBody instance with the same properties as the input requestBody.</returns> |
| | | 89 | | public static OpenApiRequestBody Clone(this OpenApiRequestBody requestBody) |
| | | 90 | | { |
| | 1 | 91 | | var clone = new OpenApiRequestBody |
| | 1 | 92 | | { |
| | 1 | 93 | | Description = requestBody.Description, |
| | 1 | 94 | | Required = requestBody.Required, |
| | 1 | 95 | | Content = requestBody.Content.Clone(), |
| | 1 | 96 | | Extensions = requestBody.Extensions.Clone() |
| | 1 | 97 | | }; |
| | 1 | 98 | | return clone; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | /// <summary> |
| | | 102 | | /// Converts an OpenApiRequestBody to an OpenApiSchema. |
| | | 103 | | /// </summary> |
| | | 104 | | /// <param name="requestBody">The OpenApiRequestBody to convert.</param> |
| | | 105 | | /// <returns>An OpenApiSchema representing the request body.</returns> |
| | | 106 | | public static OpenApiSchema ConvertToSchema(this OpenApiRequestBody requestBody) |
| | | 107 | | { |
| | 1 | 108 | | var clone = new OpenApiSchema |
| | 1 | 109 | | { |
| | 1 | 110 | | Description = requestBody.Description, |
| | 1 | 111 | | Properties = requestBody.Content?.Values.FirstOrDefault()?.Schema?.Properties.Clone(), |
| | 1 | 112 | | Extensions = requestBody.Extensions.Clone() |
| | 1 | 113 | | }; |
| | 1 | 114 | | return clone; |
| | | 115 | | } |
| | | 116 | | /// <summary> |
| | | 117 | | /// Clones an IOpenApiRequestBody instance. |
| | | 118 | | /// </summary> |
| | | 119 | | /// <param name="parameter">The IOpenApiRequestBody instance to clone.</param> |
| | | 120 | | /// <returns>A cloned instance of IOpenApiRequestBody.</returns> |
| | | 121 | | /// <exception cref="InvalidOperationException">Thrown when the IOpenApiRequestBody implementation is unsupported.</ |
| | | 122 | | public static IOpenApiRequestBody Clone(this IOpenApiRequestBody parameter) |
| | | 123 | | { |
| | | 124 | | // Determine the actual type of IOpenApiParameter and clone accordingly |
| | 0 | 125 | | return parameter switch |
| | 0 | 126 | | { |
| | 0 | 127 | | OpenApiRequestBody param => param.Clone(), |
| | 0 | 128 | | OpenApiRequestBodyReference paramRef => paramRef.Clone(), |
| | 0 | 129 | | _ => throw new InvalidOperationException("Unsupported IOpenApiRequestBody implementation."), |
| | 0 | 130 | | }; |
| | | 131 | | } |
| | | 132 | | #endregion |
| | | 133 | | #region Extensions |
| | | 134 | | /// <summary> |
| | | 135 | | /// Clones a dictionary of OpenApiExtension instances. |
| | | 136 | | /// </summary> |
| | | 137 | | /// <param name="extensions">The dictionary to clone.</param> |
| | | 138 | | /// <returns>A new dictionary with cloned OpenApiExtension instances.</returns> |
| | | 139 | | public static IDictionary<string, IOpenApiExtension>? Clone(this IDictionary<string, IOpenApiExtension>? extensions) |
| | | 140 | | { |
| | 21 | 141 | | if (extensions == null) |
| | | 142 | | { |
| | 21 | 143 | | return null; |
| | | 144 | | } |
| | | 145 | | |
| | 0 | 146 | | var clone = new Dictionary<string, IOpenApiExtension>(); |
| | 0 | 147 | | foreach (var kvp in extensions) |
| | | 148 | | { |
| | 0 | 149 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 150 | | } |
| | 0 | 151 | | return clone; |
| | | 152 | | } |
| | | 153 | | |
| | | 154 | | /// <summary> |
| | | 155 | | /// Clones an OpenApiExtension instance. |
| | | 156 | | /// </summary> |
| | | 157 | | /// <param name="extension">The OpenApiExtension to clone.</param> |
| | | 158 | | /// <returns>A new OpenApiExtension instance with the same properties as the input extension.</returns> |
| | | 159 | | /// <exception cref="InvalidOperationException">Thrown when the extension is of an unsupported type.</exception> |
| | 1 | 160 | | public static IOpenApiExtension Clone(this IOpenApiExtension extension) => throw new InvalidOperationException("Unsu |
| | | 161 | | |
| | | 162 | | #endregion |
| | | 163 | | |
| | | 164 | | #region Header |
| | | 165 | | /// <summary> |
| | | 166 | | /// Clones a dictionary of IOpenApiHeader instances. |
| | | 167 | | /// </summary> |
| | | 168 | | /// <param name="headers">The dictionary of headers to clone.</param> |
| | | 169 | | /// <returns>A new dictionary with cloned IOpenApiHeader instances.</returns> |
| | | 170 | | public static IDictionary<string, IOpenApiHeader>? Clone(this IDictionary<string, IOpenApiHeader>? headers) |
| | | 171 | | { |
| | 1 | 172 | | if (headers == null) |
| | | 173 | | { |
| | 0 | 174 | | return null; |
| | | 175 | | } |
| | | 176 | | |
| | 1 | 177 | | var clone = new Dictionary<string, IOpenApiHeader>(); |
| | 4 | 178 | | foreach (var kvp in headers) |
| | | 179 | | { |
| | 1 | 180 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 181 | | } |
| | 1 | 182 | | return clone; |
| | | 183 | | } |
| | | 184 | | |
| | | 185 | | /// <summary> |
| | | 186 | | /// Clones an IOpenApiHeader instance. |
| | | 187 | | /// </summary> |
| | | 188 | | /// <param name="header">The IOpenApiHeader to clone.</param> |
| | | 189 | | /// <returns>A new IOpenApiHeader instance with the same properties as the input header.</returns> |
| | | 190 | | /// <exception cref="InvalidOperationException">Thrown when the header is of an unsupported type.</exception> |
| | | 191 | | public static IOpenApiHeader Clone(this IOpenApiHeader header) => |
| | 1 | 192 | | header switch |
| | 1 | 193 | | { |
| | 1 | 194 | | OpenApiHeader headerObj => headerObj.Clone(), |
| | 0 | 195 | | OpenApiHeaderReference headerRef => headerRef.Clone(), |
| | 0 | 196 | | _ => throw new InvalidOperationException("Unsupported IOpenApiHeader implementation.") |
| | 1 | 197 | | }; |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Clones an OpenApiHeader instance. |
| | | 201 | | /// </summary> |
| | | 202 | | /// <param name="header">The OpenApiHeader to clone.</param> |
| | | 203 | | /// <returns>A new OpenApiHeader instance with the same properties as the input header.</returns> |
| | | 204 | | public static OpenApiHeader Clone(this OpenApiHeader header) |
| | | 205 | | { |
| | 2 | 206 | | var clone = new OpenApiHeader |
| | 2 | 207 | | { |
| | 2 | 208 | | Description = header.Description, |
| | 2 | 209 | | Required = header.Required, |
| | 2 | 210 | | Deprecated = header.Deprecated, |
| | 2 | 211 | | Style = header.Style, |
| | 2 | 212 | | Explode = header.Explode, |
| | 2 | 213 | | AllowEmptyValue = header.AllowEmptyValue, |
| | 2 | 214 | | Schema = header.Schema?.Clone(), |
| | 2 | 215 | | Examples = header.Examples?.Clone(), |
| | 2 | 216 | | Example = JsonNodeClone(header.Example), |
| | 2 | 217 | | Content = header.Content?.Clone(), |
| | 2 | 218 | | Extensions = header.Extensions.Clone(), |
| | 2 | 219 | | AllowReserved = header.AllowReserved |
| | 2 | 220 | | }; |
| | 2 | 221 | | return clone; |
| | | 222 | | } |
| | | 223 | | /// <summary> |
| | | 224 | | /// Clones an OpenApiHeaderReference instance. |
| | | 225 | | /// </summary> |
| | | 226 | | /// <param name="header">The OpenApiHeaderReference to clone.</param> |
| | | 227 | | /// <returns>A new OpenApiHeaderReference instance with the same properties as the input header.</returns> |
| | | 228 | | public static OpenApiHeaderReference Clone(this OpenApiHeaderReference header) |
| | | 229 | | { |
| | 0 | 230 | | var clone = new OpenApiHeaderReference(header.Reference.Id!) |
| | 0 | 231 | | { |
| | 0 | 232 | | Description = header.Description, |
| | 0 | 233 | | }; |
| | 0 | 234 | | return clone; |
| | | 235 | | } |
| | | 236 | | #endregion |
| | | 237 | | #region Response |
| | | 238 | | |
| | | 239 | | /// <summary> |
| | | 240 | | /// Clones an OpenApiResponse instance. |
| | | 241 | | /// </summary> |
| | | 242 | | /// <param name="response">The OpenApiResponse to clone.</param> |
| | | 243 | | /// <returns>A new OpenApiResponse instance with the same properties as the input response.</returns> |
| | | 244 | | public static OpenApiResponse Clone(this OpenApiResponse response) |
| | | 245 | | { |
| | 1 | 246 | | var clone = new OpenApiResponse |
| | 1 | 247 | | { |
| | 1 | 248 | | Description = response.Description, |
| | 1 | 249 | | Headers = response.Headers?.Clone(), |
| | 1 | 250 | | Content = Clone(response.Content), |
| | 1 | 251 | | Links = response.Links.Clone(), |
| | 1 | 252 | | Extensions = response.Extensions.Clone() |
| | 1 | 253 | | }; |
| | 1 | 254 | | return clone; |
| | | 255 | | } |
| | | 256 | | /// <summary> |
| | | 257 | | /// Clones an OpenApiResponseReference instance. |
| | | 258 | | /// </summary> |
| | | 259 | | /// <param name="response">The OpenApiResponseReference to clone.</param> |
| | | 260 | | /// <returns>A new OpenApiResponseReference instance with the same properties as the input response.</returns> |
| | | 261 | | public static OpenApiResponseReference Clone(this OpenApiResponseReference response) |
| | | 262 | | { |
| | 0 | 263 | | var clone = new OpenApiResponseReference(response.Reference.Id!) |
| | 0 | 264 | | { |
| | 0 | 265 | | Description = response.Description, |
| | 0 | 266 | | }; |
| | 0 | 267 | | return clone; |
| | | 268 | | } |
| | | 269 | | |
| | | 270 | | /// <summary> |
| | | 271 | | /// Clones an IOpenApiResponse instance. |
| | | 272 | | /// </summary> |
| | | 273 | | /// <param name="response"> The IOpenApiResponse instance to clone.</param> |
| | | 274 | | /// <returns>A cloned IOpenApiResponse instance.</returns> |
| | | 275 | | /// <exception cref="InvalidOperationException">Thrown when the IOpenApiResponse implementation is unsupported.</exc |
| | | 276 | | public static IOpenApiResponse Clone(this IOpenApiResponse response) |
| | | 277 | | { |
| | | 278 | | // Determine the actual type of IOpenApiResponse and clone accordingly |
| | 0 | 279 | | return response switch |
| | 0 | 280 | | { |
| | 0 | 281 | | OpenApiResponse resp => resp.Clone(), |
| | 0 | 282 | | OpenApiResponseReference respRef => respRef.Clone(), |
| | 0 | 283 | | _ => throw new InvalidOperationException("Unsupported IOpenApiResponse implementation."), |
| | 0 | 284 | | }; |
| | | 285 | | } |
| | | 286 | | |
| | | 287 | | #endregion |
| | | 288 | | #region MediaType |
| | | 289 | | /// <summary> |
| | | 290 | | /// Clones a dictionary of OpenApiMediaType instances. |
| | | 291 | | /// </summary> |
| | | 292 | | /// <param name="content">The dictionary to clone.</param> |
| | | 293 | | /// <returns>A new dictionary with cloned OpenApiMediaType instances.</returns> |
| | | 294 | | public static IDictionary<string, IOpenApiMediaType>? Clone(this IDictionary<string, IOpenApiMediaType>? content) |
| | | 295 | | { |
| | 3 | 296 | | if (content == null) |
| | | 297 | | { |
| | 0 | 298 | | return null; |
| | | 299 | | } |
| | | 300 | | |
| | 3 | 301 | | var clone = new Dictionary<string, IOpenApiMediaType>(); |
| | 12 | 302 | | foreach (var kvp in content) |
| | | 303 | | { |
| | 3 | 304 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 305 | | } |
| | 3 | 306 | | return clone; |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Clones an IOpenApiMediaType instance. |
| | | 311 | | /// </summary> |
| | | 312 | | /// <param name="mediaType"> The IOpenApiMediaType instance to clone.</param> |
| | | 313 | | /// <returns>A cloned IOpenApiMediaType instance.</returns> |
| | | 314 | | /// <exception cref="InvalidOperationException">Thrown when the IOpenApiMediaType implementation is unsupported.</ex |
| | | 315 | | public static IOpenApiMediaType Clone(this IOpenApiMediaType mediaType) |
| | | 316 | | { |
| | | 317 | | // Determine the actual type of IOpenApiParameter and clone accordingly |
| | 4 | 318 | | return mediaType switch |
| | 4 | 319 | | { |
| | 4 | 320 | | OpenApiMediaType media => media.Clone(), |
| | 0 | 321 | | OpenApiMediaTypeReference mediaRef => mediaRef.Clone(), |
| | 0 | 322 | | _ => throw new InvalidOperationException("Unsupported IOpenApiMediaType implementation."), |
| | 4 | 323 | | }; |
| | | 324 | | } |
| | | 325 | | /// <summary> |
| | | 326 | | /// Clones an OpenApiMediaTypeReference instance. |
| | | 327 | | /// </summary> |
| | | 328 | | /// <param name="mediaType"> The OpenApiMediaTypeReference instance to clone.</param> |
| | | 329 | | /// <returns>A cloned OpenApiMediaTypeReference instance.</returns> |
| | | 330 | | public static OpenApiMediaTypeReference Clone(this OpenApiMediaTypeReference mediaType) |
| | | 331 | | { |
| | 0 | 332 | | var clone = new OpenApiMediaTypeReference(mediaType.Reference.Id!); |
| | 0 | 333 | | return clone; |
| | | 334 | | } |
| | | 335 | | |
| | | 336 | | /// <summary> |
| | | 337 | | /// Clones an OpenApiMediaType instance. |
| | | 338 | | /// </summary> |
| | | 339 | | /// <param name="mediaType">The OpenApiMediaType instance to clone.</param> |
| | | 340 | | /// <returns>A cloned OpenApiMediaType instance.</returns> |
| | | 341 | | public static OpenApiMediaType Clone(this OpenApiMediaType mediaType) |
| | | 342 | | { |
| | 5 | 343 | | var clone = new OpenApiMediaType(mediaType); |
| | 5 | 344 | | return clone; |
| | | 345 | | } |
| | | 346 | | |
| | | 347 | | #endregion |
| | | 348 | | #region Example |
| | | 349 | | /// <summary> |
| | | 350 | | /// Clones a dictionary of OpenApiExample instances. |
| | | 351 | | /// </summary> |
| | | 352 | | /// <param name="examples">The dictionary to clone.</param> |
| | | 353 | | /// <returns>A new dictionary with cloned OpenApiExample instances.</returns> |
| | | 354 | | public static IDictionary<string, IOpenApiExample>? Clone(this IDictionary<string, IOpenApiExample>? examples) |
| | | 355 | | { |
| | 1 | 356 | | if (examples == null) |
| | | 357 | | { |
| | 0 | 358 | | return null; |
| | | 359 | | } |
| | | 360 | | |
| | 1 | 361 | | var clone = new Dictionary<string, IOpenApiExample>(); |
| | 4 | 362 | | foreach (var kvp in examples) |
| | | 363 | | { |
| | 1 | 364 | | clone[kvp.Key] = kvp.Value switch |
| | 1 | 365 | | { |
| | 1 | 366 | | OpenApiExample exampleObj => exampleObj.Clone(), |
| | 0 | 367 | | OpenApiExampleReference exampleRef => exampleRef.Clone(), |
| | 0 | 368 | | _ => throw new InvalidOperationException("Unsupported IOpenApiExample implementation."), |
| | 1 | 369 | | }; |
| | | 370 | | } |
| | 1 | 371 | | return clone; |
| | | 372 | | } |
| | | 373 | | |
| | | 374 | | /// <summary> |
| | | 375 | | /// Clones an OpenApiExampleReference instance. |
| | | 376 | | /// </summary> |
| | | 377 | | /// <param name="example">The OpenApiExampleReference to clone.</param> |
| | | 378 | | /// <returns>A new OpenApiExampleReference instance with the same properties as the input instance.</returns> |
| | | 379 | | public static OpenApiExampleReference Clone(this OpenApiExampleReference example) |
| | | 380 | | { |
| | 0 | 381 | | var clone = new OpenApiExampleReference(example.Reference.Id!) |
| | 0 | 382 | | { |
| | 0 | 383 | | Description = example.Description, |
| | 0 | 384 | | Summary = example.Summary |
| | 0 | 385 | | }; |
| | 0 | 386 | | return clone; |
| | | 387 | | } |
| | | 388 | | |
| | | 389 | | /// <summary> |
| | | 390 | | /// Clones an OpenApiExample instance. |
| | | 391 | | /// </summary> |
| | | 392 | | /// <param name="example">The OpenApiExample to clone.</param> |
| | | 393 | | /// <returns>A new OpenApiExample instance with the same properties as the input example.</returns> |
| | | 394 | | public static OpenApiExample Clone(this OpenApiExample example) |
| | | 395 | | { |
| | 4 | 396 | | var clone = new OpenApiExample |
| | 4 | 397 | | { |
| | 4 | 398 | | Summary = example.Summary, |
| | 4 | 399 | | Description = example.Description, |
| | 4 | 400 | | Value = example.Value != null ? JsonNodeClone(example.Value) : null, |
| | 4 | 401 | | ExternalValue = example.ExternalValue, |
| | 4 | 402 | | Extensions = example.Extensions.Clone() |
| | 4 | 403 | | }; |
| | 4 | 404 | | return clone; |
| | | 405 | | } |
| | | 406 | | #endregion |
| | | 407 | | #region Schema |
| | | 408 | | /// <summary> |
| | | 409 | | /// Clones an IOpenApiSchema instance. |
| | | 410 | | /// </summary> |
| | | 411 | | /// <param name="schema">The IOpenApiSchema to clone.</param> |
| | | 412 | | /// <returns>A new IOpenApiSchema instance with the same properties as the input schema.</returns> |
| | | 413 | | public static IOpenApiSchema Clone(this IOpenApiSchema schema) => |
| | 6 | 414 | | schema switch |
| | 6 | 415 | | { |
| | 0 | 416 | | OpenApiSchemaReference schemaRef => schemaRef.Clone(), |
| | 6 | 417 | | OpenApiSchema schemaObj => schemaObj.Clone(), |
| | 0 | 418 | | _ => throw new InvalidOperationException("Unsupported IOpenApiSchema implementation.") |
| | 6 | 419 | | }; |
| | | 420 | | |
| | | 421 | | /// <summary> |
| | | 422 | | /// Clones an OpenApiSchema instance. |
| | | 423 | | /// </summary> |
| | | 424 | | /// <param name="schema">The OpenApiSchema to clone.</param> |
| | | 425 | | /// <returns>A new OpenApiSchema instance with the same properties as the input schema.</returns> |
| | | 426 | | public static OpenApiSchema Clone(this OpenApiSchema schema) |
| | | 427 | | { |
| | 7 | 428 | | var clone = new OpenApiSchema() |
| | 7 | 429 | | { |
| | 7 | 430 | | Title = schema.Title, |
| | 7 | 431 | | Id = schema.Id, |
| | 7 | 432 | | Const = schema.Const, |
| | 7 | 433 | | Schema = schema.Schema, |
| | 7 | 434 | | Comment = schema.Comment, |
| | 7 | 435 | | Vocabulary = schema.Vocabulary != null ? new Dictionary<string, bool>(schema.Vocabulary) : null, |
| | 7 | 436 | | DynamicAnchor = schema.DynamicAnchor, |
| | 7 | 437 | | DynamicRef = schema.DynamicRef, |
| | 7 | 438 | | Definitions = schema.Definitions.Clone(), |
| | 7 | 439 | | UnevaluatedProperties = schema.UnevaluatedProperties, |
| | 7 | 440 | | ExclusiveMaximum = schema.ExclusiveMaximum, |
| | 7 | 441 | | ExclusiveMinimum = schema.ExclusiveMinimum, |
| | 7 | 442 | | |
| | 7 | 443 | | Type = schema.Type, |
| | 7 | 444 | | Format = schema.Format, |
| | 7 | 445 | | Description = schema.Description, |
| | 7 | 446 | | Maximum = schema.Maximum, |
| | 7 | 447 | | Minimum = schema.Minimum, |
| | 7 | 448 | | MaxLength = schema.MaxLength, |
| | 7 | 449 | | MinLength = schema.MinLength, |
| | 7 | 450 | | Pattern = schema.Pattern, |
| | 7 | 451 | | MultipleOf = schema.MultipleOf, |
| | 7 | 452 | | Default = JsonNodeClone(schema.Default), |
| | 7 | 453 | | ReadOnly = schema.ReadOnly, |
| | 7 | 454 | | WriteOnly = schema.WriteOnly, |
| | 7 | 455 | | AllOf = schema.AllOf?.Clone(), |
| | 7 | 456 | | OneOf = schema.OneOf?.Clone(), |
| | 7 | 457 | | AnyOf = schema.AnyOf?.Clone(), |
| | 7 | 458 | | Not = schema.Not?.Clone(), |
| | 7 | 459 | | Required = schema.Required != null ? new HashSet<string>(schema.Required) : null, |
| | 7 | 460 | | Items = schema.Items?.Clone(), |
| | 7 | 461 | | MaxItems = schema.MaxItems, |
| | 7 | 462 | | MinItems = schema.MinItems, |
| | 7 | 463 | | UniqueItems = schema.UniqueItems, |
| | 7 | 464 | | Properties = schema.Properties.Clone(), |
| | 7 | 465 | | PatternProperties = schema.PatternProperties?.Clone(), |
| | 7 | 466 | | MaxProperties = schema.MaxProperties, |
| | 7 | 467 | | MinProperties = schema.MinProperties, |
| | 7 | 468 | | AdditionalPropertiesAllowed = schema.AdditionalPropertiesAllowed, |
| | 7 | 469 | | AdditionalProperties = schema.AdditionalProperties?.Clone(), |
| | 7 | 470 | | Discriminator = schema.Discriminator != null ? new(schema.Discriminator) : null, |
| | 7 | 471 | | Example = schema.Example != null ? JsonNodeClone(schema.Example) : null, |
| | 7 | 472 | | Examples = schema.Examples != null ? [.. schema.Examples] : null, |
| | 7 | 473 | | Enum = schema.Enum != null ? [.. schema.Enum] : null, |
| | 7 | 474 | | ExternalDocs = schema.ExternalDocs != null ? new(schema.ExternalDocs) : null, |
| | 7 | 475 | | Deprecated = schema.Deprecated, |
| | 7 | 476 | | Xml = schema.Xml != null ? new(schema.Xml) : null, |
| | 7 | 477 | | Extensions = schema.Extensions.Clone(), |
| | 7 | 478 | | Metadata = schema is IMetadataContainer { Metadata: not null } mContainer ? new Dictionary<string, object>(m |
| | 7 | 479 | | UnrecognizedKeywords = schema.UnrecognizedKeywords != null ? new Dictionary<string, JsonNode>(schema.Unrecog |
| | 7 | 480 | | DependentRequired = schema.DependentRequired != null ? new Dictionary<string, HashSet<string>>(schema.Depend |
| | 7 | 481 | | }; |
| | 7 | 482 | | return clone; |
| | | 483 | | } |
| | | 484 | | /// <summary> |
| | | 485 | | /// Clones an OpenApiSchemaReference instance. |
| | | 486 | | /// </summary> |
| | | 487 | | /// <param name="schemaRef">The OpenApiSchemaReference to clone</param> |
| | | 488 | | /// <returns>A new OpenApiSchemaReference instance with the same properties as the input instance.</returns> |
| | | 489 | | public static OpenApiSchemaReference Clone(this OpenApiSchemaReference schemaRef) |
| | | 490 | | { |
| | 1 | 491 | | var cloneRef = new OpenApiSchemaReference(referenceId: schemaRef.Reference.Id!) |
| | 1 | 492 | | { |
| | 1 | 493 | | Reference = schemaRef.Reference, |
| | 1 | 494 | | Title = schemaRef.Title, |
| | 1 | 495 | | Description = schemaRef.Description |
| | 1 | 496 | | }; |
| | 1 | 497 | | return cloneRef; |
| | | 498 | | } |
| | | 499 | | /// <summary> |
| | | 500 | | /// Clones a list of OpenApiSchema instances. |
| | | 501 | | /// </summary> |
| | | 502 | | /// <param name="schemas">The list to clone.</param> |
| | | 503 | | /// <returns>A new list containing cloned OpenApiSchema instances.</returns> |
| | | 504 | | public static IList<IOpenApiSchema>? Clone(this IList<IOpenApiSchema>? schemas) |
| | | 505 | | { |
| | 0 | 506 | | if (schemas == null) |
| | | 507 | | { |
| | 0 | 508 | | return null; |
| | | 509 | | } |
| | 0 | 510 | | var cloneList = new List<IOpenApiSchema>(); |
| | 0 | 511 | | foreach (var schema in schemas) |
| | | 512 | | { |
| | 0 | 513 | | cloneList.Add(schema.Clone()); |
| | | 514 | | } |
| | 0 | 515 | | return cloneList; |
| | | 516 | | } |
| | | 517 | | /// <summary> |
| | | 518 | | /// Clones a dictionary of OpenApiSchema instances. |
| | | 519 | | /// </summary> |
| | | 520 | | /// <param name="schemas">The dictionary to clone.</param> |
| | | 521 | | /// <returns>A new dictionary containing cloned OpenApiSchema instances.</returns> |
| | | 522 | | public static Dictionary<string, IOpenApiSchema>? Clone(this IDictionary<string, IOpenApiSchema>? schemas) |
| | | 523 | | { |
| | 15 | 524 | | if (schemas == null) |
| | | 525 | | { |
| | 12 | 526 | | return null; |
| | | 527 | | } |
| | 3 | 528 | | var clone = new Dictionary<string, IOpenApiSchema>(); |
| | 12 | 529 | | foreach (var kvp in schemas) |
| | | 530 | | { |
| | 3 | 531 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 532 | | } |
| | 3 | 533 | | return clone; |
| | | 534 | | } |
| | | 535 | | #endregion |
| | | 536 | | |
| | | 537 | | #region Callback |
| | | 538 | | /// <summary> |
| | | 539 | | /// Clones an IOpenApiCallback instance. |
| | | 540 | | /// </summary> |
| | | 541 | | /// <param name="callback">The IOpenApiCallback to clone.</param> |
| | | 542 | | /// <returns>A new IOpenApiCallback instance with the same properties as the input callback.</returns> |
| | | 543 | | /// <exception cref="InvalidOperationException">Thrown when the callback is of an unsupported type.</exception> |
| | | 544 | | public static IOpenApiCallback Clone(this IOpenApiCallback callback) => |
| | 0 | 545 | | callback switch |
| | 0 | 546 | | { |
| | 0 | 547 | | OpenApiCallback callbackObj => callbackObj.Clone(), |
| | 0 | 548 | | OpenApiCallbackReference callbackRef => callbackRef.Clone(), |
| | 0 | 549 | | _ => throw new InvalidOperationException("Unsupported IOpenApiCallback implementation.") |
| | 0 | 550 | | }; |
| | | 551 | | |
| | | 552 | | /// <summary> |
| | | 553 | | /// Clones an OpenApiCallback instance. |
| | | 554 | | /// </summary> |
| | | 555 | | /// <param name="callback">The OpenApiCallback to clone.</param> |
| | | 556 | | /// <returns>A new OpenApiCallback instance with the same properties as the input callback.</returns> |
| | | 557 | | public static OpenApiCallback Clone(this OpenApiCallback callback) |
| | | 558 | | { |
| | 1 | 559 | | var clone = new OpenApiCallback |
| | 1 | 560 | | { |
| | 1 | 561 | | PathItems = callback?.PathItems != null ? new(callback.PathItems) : null, |
| | 1 | 562 | | Extensions = callback?.Extensions.Clone() |
| | 1 | 563 | | }; |
| | 1 | 564 | | return clone; |
| | | 565 | | } |
| | | 566 | | |
| | | 567 | | /// <summary> |
| | | 568 | | /// Clones an OpenApiCallbackReference instance. |
| | | 569 | | /// </summary> |
| | | 570 | | /// <param name="callback">The OpenApiCallbackReference to clone.</param> |
| | | 571 | | /// <returns>A new OpenApiCallbackReference instance with the same properties as the input callback.</returns> |
| | | 572 | | public static OpenApiCallbackReference Clone(this OpenApiCallbackReference callback) |
| | | 573 | | { |
| | 0 | 574 | | var clone = new OpenApiCallbackReference(callback.Reference.Id!); |
| | 0 | 575 | | return clone; |
| | | 576 | | } |
| | | 577 | | |
| | | 578 | | #endregion |
| | | 579 | | #region Link |
| | | 580 | | /// <summary> |
| | | 581 | | /// Clones a dictionary of IOpenApiLink instances. |
| | | 582 | | /// </summary> |
| | | 583 | | /// <param name="links">The dictionary of IOpenApiLink instances to clone.</param> |
| | | 584 | | /// <returns>A new dictionary containing cloned IOpenApiLink instances.</returns> |
| | | 585 | | public static IDictionary<string, IOpenApiLink>? Clone(this IDictionary<string, IOpenApiLink>? links) |
| | | 586 | | { |
| | 1 | 587 | | if (links == null) |
| | | 588 | | { |
| | 0 | 589 | | return null; |
| | | 590 | | } |
| | | 591 | | |
| | 1 | 592 | | var clone = new Dictionary<string, IOpenApiLink>(); |
| | 4 | 593 | | foreach (var kvp in links) |
| | | 594 | | { |
| | 1 | 595 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 596 | | } |
| | 1 | 597 | | return clone; |
| | | 598 | | } |
| | | 599 | | |
| | | 600 | | /// <summary> |
| | | 601 | | /// Clones an IOpenApiLink instance. |
| | | 602 | | /// </summary> |
| | | 603 | | /// <param name="link">The IOpenApiLink to clone.</param> |
| | | 604 | | /// <returns>A new IOpenApiLink instance with the same properties as the input link.</returns> |
| | | 605 | | public static IOpenApiLink Clone(this IOpenApiLink link) => |
| | 1 | 606 | | link switch |
| | 1 | 607 | | { |
| | 1 | 608 | | OpenApiLink linkObj => linkObj.Clone(), |
| | 0 | 609 | | OpenApiLinkReference linkRef => linkRef.Clone(), |
| | 0 | 610 | | _ => throw new InvalidOperationException("Unsupported IOpenApiLink implementation.") |
| | 1 | 611 | | }; |
| | | 612 | | |
| | | 613 | | /// <summary> |
| | | 614 | | /// Clones an OpenApiLinkReference instance. |
| | | 615 | | /// </summary> |
| | | 616 | | /// <param name="link">The OpenApiLinkReference instance to clone.</param> |
| | | 617 | | /// <returns>A new OpenApiLinkReference instance with the same properties as the input instance.</returns> |
| | | 618 | | public static OpenApiLinkReference Clone(this OpenApiLinkReference link) |
| | | 619 | | { |
| | 0 | 620 | | var clone = new OpenApiLinkReference(link.Reference.Id!) |
| | 0 | 621 | | { |
| | 0 | 622 | | Reference = link.Reference |
| | 0 | 623 | | }; |
| | 0 | 624 | | return clone; |
| | | 625 | | } |
| | | 626 | | |
| | | 627 | | /// <summary> |
| | | 628 | | /// Clones an OpenApiLink instance. |
| | | 629 | | /// </summary> |
| | | 630 | | /// <param name="link">The OpenApiLink instance to clone.</param> |
| | | 631 | | /// <returns>A new OpenApiLink instance with the same properties as the input instance.</returns> |
| | | 632 | | public static OpenApiLink Clone(this OpenApiLink link) |
| | | 633 | | { |
| | 3 | 634 | | var clone = new OpenApiLink |
| | 3 | 635 | | { |
| | 3 | 636 | | OperationRef = link.OperationRef, |
| | 3 | 637 | | OperationId = link.OperationId, |
| | 3 | 638 | | Parameters = link.Parameters.Clone(), |
| | 3 | 639 | | RequestBody = link.RequestBody!.Clone(), |
| | 3 | 640 | | Description = link.Description, |
| | 3 | 641 | | Server = link.Server?.Clone(), |
| | 3 | 642 | | Extensions = link.Extensions.Clone() |
| | 3 | 643 | | }; |
| | 3 | 644 | | return clone; |
| | | 645 | | } |
| | | 646 | | #endregion |
| | | 647 | | |
| | | 648 | | /// <summary> |
| | | 649 | | /// Clones an OpenApiServer instance. |
| | | 650 | | /// </summary> |
| | | 651 | | /// <param name="server">The OpenApiServer instance to clone.</param> |
| | | 652 | | /// <returns>A new OpenApiServer instance with the same properties as the input instance.</returns> |
| | | 653 | | public static OpenApiServer Clone(this OpenApiServer server) |
| | | 654 | | { |
| | 0 | 655 | | var clone = new OpenApiServer |
| | 0 | 656 | | { |
| | 0 | 657 | | Url = server.Url, |
| | 0 | 658 | | Description = server.Description, |
| | 0 | 659 | | Variables = server.Variables != null ? new Dictionary<string, OpenApiServerVariable>(server.Variables) : nul |
| | 0 | 660 | | Extensions = server.Extensions.Clone() |
| | 0 | 661 | | }; |
| | 0 | 662 | | return clone; |
| | | 663 | | } |
| | | 664 | | |
| | | 665 | | #region RuntimeExpression |
| | | 666 | | /// <summary> |
| | | 667 | | /// Clones a RuntimeExpressionAnyWrapper instance. |
| | | 668 | | /// </summary> |
| | | 669 | | /// <param name="expressionWrapper">The RuntimeExpressionAnyWrapper instance to clone.</param> |
| | | 670 | | /// <returns>A new RuntimeExpressionAnyWrapper instance with the same properties as the input instance.</returns> |
| | | 671 | | public static RuntimeExpressionAnyWrapper Clone(this RuntimeExpressionAnyWrapper expressionWrapper) |
| | | 672 | | { |
| | 6 | 673 | | return new RuntimeExpressionAnyWrapper |
| | 6 | 674 | | { |
| | 6 | 675 | | Expression = expressionWrapper.Expression, |
| | 6 | 676 | | Any = expressionWrapper.Any != null ? JsonNodeClone(expressionWrapper.Any) : null |
| | 6 | 677 | | }; |
| | | 678 | | } |
| | | 679 | | |
| | | 680 | | /// <summary> |
| | | 681 | | /// Clones a dictionary of RuntimeExpressionAnyWrapper instances. |
| | | 682 | | /// </summary> |
| | | 683 | | /// <param name="parameters">The dictionary of RuntimeExpressionAnyWrapper instances to clone.</param> |
| | | 684 | | /// <returns>A new dictionary that is a deep clone of the input dictionary.</returns> |
| | | 685 | | public static IDictionary<string, RuntimeExpressionAnyWrapper>? Clone(this IDictionary<string, RuntimeExpressionAnyW |
| | | 686 | | { |
| | 3 | 687 | | if (parameters == null) |
| | | 688 | | { |
| | 0 | 689 | | return null; |
| | | 690 | | } |
| | | 691 | | |
| | 3 | 692 | | var clone = new Dictionary<string, RuntimeExpressionAnyWrapper>(); |
| | 12 | 693 | | foreach (var kvp in parameters) |
| | | 694 | | { |
| | 3 | 695 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 696 | | } |
| | 3 | 697 | | return clone; |
| | | 698 | | } |
| | | 699 | | |
| | | 700 | | #endregion |
| | | 701 | | #region JsonNode |
| | | 702 | | /// <summary> |
| | | 703 | | /// Clones a JsonNode instance. |
| | | 704 | | /// </summary> |
| | | 705 | | /// <param name="value">The JsonNode to clone.</param> |
| | | 706 | | /// <returns>A new JsonNode instance that is a deep clone of the input value.</returns> |
| | 18 | 707 | | public static JsonNode? JsonNodeClone(JsonNode? value) => value?.DeepClone(); |
| | | 708 | | #endregion |
| | | 709 | | } |