| | | 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 | | { |
| | 0 | 33 | | var clone = new OpenApiParameter |
| | 0 | 34 | | { |
| | 0 | 35 | | Name = parameter.Name, |
| | 0 | 36 | | In = parameter.In, |
| | 0 | 37 | | Description = parameter.Description, |
| | 0 | 38 | | Required = parameter.Required, |
| | 0 | 39 | | Style = parameter.Style, |
| | 0 | 40 | | Explode = parameter.Explode, |
| | 0 | 41 | | AllowReserved = parameter.AllowReserved, |
| | 0 | 42 | | Schema = parameter.Schema?.Clone(), |
| | 0 | 43 | | Examples = parameter.Examples?.Clone(), |
| | 0 | 44 | | Example = JsonNodeClone(parameter.Example), |
| | 0 | 45 | | Content = parameter.Content?.Clone(), |
| | 0 | 46 | | Extensions = parameter.Extensions.Clone(), |
| | 0 | 47 | | AllowEmptyValue = parameter.AllowEmptyValue, |
| | 0 | 48 | | Deprecated = parameter.Deprecated |
| | 0 | 49 | | }; |
| | 0 | 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 |
| | 0 | 62 | | return parameter switch |
| | 0 | 63 | | { |
| | 0 | 64 | | OpenApiParameter param => param.Clone(), |
| | 0 | 65 | | OpenApiParameterReference paramRef => paramRef.Clone(), |
| | 0 | 66 | | _ => throw new InvalidOperationException("Unsupported IOpenApiParameter implementation."), |
| | 0 | 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 | | { |
| | 0 | 91 | | var clone = new OpenApiRequestBody |
| | 0 | 92 | | { |
| | 0 | 93 | | Description = requestBody.Description, |
| | 0 | 94 | | Required = requestBody.Required, |
| | 0 | 95 | | Content = requestBody.Content.Clone(), |
| | 0 | 96 | | Extensions = requestBody.Extensions.Clone() |
| | 0 | 97 | | }; |
| | 0 | 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 | | { |
| | 0 | 108 | | var clone = new OpenApiSchema |
| | 0 | 109 | | { |
| | 0 | 110 | | Description = requestBody.Description, |
| | 0 | 111 | | Properties = requestBody.Content?.Values.FirstOrDefault()?.Schema?.Properties.Clone(), |
| | 0 | 112 | | Extensions = requestBody.Extensions.Clone() |
| | 0 | 113 | | }; |
| | 0 | 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 | | { |
| | 5 | 141 | | if (extensions == null) |
| | | 142 | | { |
| | 5 | 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> |
| | 0 | 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 | | { |
| | 0 | 172 | | if (headers == null) |
| | | 173 | | { |
| | 0 | 174 | | return null; |
| | | 175 | | } |
| | | 176 | | |
| | 0 | 177 | | var clone = new Dictionary<string, IOpenApiHeader>(); |
| | 0 | 178 | | foreach (var kvp in headers) |
| | | 179 | | { |
| | 0 | 180 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 181 | | } |
| | 0 | 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) => |
| | 0 | 192 | | header switch |
| | 0 | 193 | | { |
| | 0 | 194 | | OpenApiHeader headerObj => headerObj.Clone(), |
| | 0 | 195 | | OpenApiHeaderReference headerRef => headerRef.Clone(), |
| | 0 | 196 | | _ => throw new InvalidOperationException("Unsupported IOpenApiHeader implementation.") |
| | 0 | 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 | | { |
| | 0 | 206 | | var clone = new OpenApiHeader |
| | 0 | 207 | | { |
| | 0 | 208 | | Description = header.Description, |
| | 0 | 209 | | Required = header.Required, |
| | 0 | 210 | | Deprecated = header.Deprecated, |
| | 0 | 211 | | Style = header.Style, |
| | 0 | 212 | | Explode = header.Explode, |
| | 0 | 213 | | AllowEmptyValue = header.AllowEmptyValue, |
| | 0 | 214 | | Schema = header.Schema?.Clone(), |
| | 0 | 215 | | Examples = header.Examples?.Clone(), |
| | 0 | 216 | | Example = JsonNodeClone(header.Example), |
| | 0 | 217 | | Content = header.Content?.Clone(), |
| | 0 | 218 | | Extensions = header.Extensions.Clone(), |
| | 0 | 219 | | AllowReserved = header.AllowReserved |
| | 0 | 220 | | }; |
| | 0 | 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 | | { |
| | 0 | 246 | | var clone = new OpenApiResponse |
| | 0 | 247 | | { |
| | 0 | 248 | | Description = response.Description, |
| | 0 | 249 | | Headers = response.Headers?.Clone(), |
| | 0 | 250 | | Content = Clone(response.Content), |
| | 0 | 251 | | Links = response.Links.Clone(), |
| | 0 | 252 | | Extensions = response.Extensions.Clone() |
| | 0 | 253 | | }; |
| | 0 | 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, OpenApiMediaType>? Clone(this IDictionary<string, OpenApiMediaType>? content) |
| | | 295 | | { |
| | 0 | 296 | | if (content == null) |
| | | 297 | | { |
| | 0 | 298 | | return null; |
| | | 299 | | } |
| | | 300 | | |
| | 0 | 301 | | var clone = new Dictionary<string, OpenApiMediaType>(); |
| | 0 | 302 | | foreach (var kvp in content) |
| | | 303 | | { |
| | 0 | 304 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 305 | | } |
| | 0 | 306 | | return clone; |
| | | 307 | | } |
| | | 308 | | |
| | | 309 | | /// <summary> |
| | | 310 | | /// Clones an OpenApiMediaType instance. |
| | | 311 | | /// </summary> |
| | | 312 | | /// <param name="mediaType">The OpenApiMediaType to clone.</param> |
| | | 313 | | /// <returns>A new OpenApiMediaType instance with the same properties as the input mediaType.</returns> |
| | | 314 | | public static OpenApiMediaType Clone(this OpenApiMediaType mediaType) |
| | | 315 | | { |
| | 0 | 316 | | var clone = new OpenApiMediaType |
| | 0 | 317 | | { |
| | 0 | 318 | | Schema = mediaType.Schema?.Clone(), |
| | 0 | 319 | | Example = mediaType.Example != null ? JsonNodeClone(mediaType.Example) : null, |
| | 0 | 320 | | Examples = mediaType.Examples.Clone(), |
| | 0 | 321 | | Encoding = mediaType.Encoding != null ? new Dictionary<string, OpenApiEncoding>(mediaType.Encoding) : null, |
| | 0 | 322 | | Extensions = mediaType.Extensions.Clone() |
| | 0 | 323 | | }; |
| | 0 | 324 | | return clone; |
| | | 325 | | } |
| | | 326 | | #endregion |
| | | 327 | | #region Example |
| | | 328 | | /// <summary> |
| | | 329 | | /// Clones a dictionary of OpenApiExample instances. |
| | | 330 | | /// </summary> |
| | | 331 | | /// <param name="examples">The dictionary to clone.</param> |
| | | 332 | | /// <returns>A new dictionary with cloned OpenApiExample instances.</returns> |
| | | 333 | | public static IDictionary<string, IOpenApiExample>? Clone(this IDictionary<string, IOpenApiExample>? examples) |
| | | 334 | | { |
| | 0 | 335 | | if (examples == null) |
| | | 336 | | { |
| | 0 | 337 | | return null; |
| | | 338 | | } |
| | | 339 | | |
| | 0 | 340 | | var clone = new Dictionary<string, IOpenApiExample>(); |
| | 0 | 341 | | foreach (var kvp in examples) |
| | | 342 | | { |
| | 0 | 343 | | clone[kvp.Key] = kvp.Value switch |
| | 0 | 344 | | { |
| | 0 | 345 | | OpenApiExample exampleObj => exampleObj.Clone(), |
| | 0 | 346 | | OpenApiExampleReference exampleRef => exampleRef.Clone(), |
| | 0 | 347 | | _ => throw new InvalidOperationException("Unsupported IOpenApiExample implementation."), |
| | 0 | 348 | | }; |
| | | 349 | | } |
| | 0 | 350 | | return clone; |
| | | 351 | | } |
| | | 352 | | |
| | | 353 | | /// <summary> |
| | | 354 | | /// Clones an OpenApiExampleReference instance. |
| | | 355 | | /// </summary> |
| | | 356 | | /// <param name="example">The OpenApiExampleReference to clone.</param> |
| | | 357 | | /// <returns>A new OpenApiExampleReference instance with the same properties as the input instance.</returns> |
| | | 358 | | public static OpenApiExampleReference Clone(this OpenApiExampleReference example) |
| | | 359 | | { |
| | 0 | 360 | | var clone = new OpenApiExampleReference(example.Reference.Id!) |
| | 0 | 361 | | { |
| | 0 | 362 | | Description = example.Description, |
| | 0 | 363 | | Summary = example.Summary |
| | 0 | 364 | | }; |
| | 0 | 365 | | return clone; |
| | | 366 | | } |
| | | 367 | | |
| | | 368 | | /// <summary> |
| | | 369 | | /// Clones an OpenApiExample instance. |
| | | 370 | | /// </summary> |
| | | 371 | | /// <param name="example">The OpenApiExample to clone.</param> |
| | | 372 | | /// <returns>A new OpenApiExample instance with the same properties as the input example.</returns> |
| | | 373 | | public static OpenApiExample Clone(this OpenApiExample example) |
| | | 374 | | { |
| | 2 | 375 | | var clone = new OpenApiExample |
| | 2 | 376 | | { |
| | 2 | 377 | | Summary = example.Summary, |
| | 2 | 378 | | Description = example.Description, |
| | 2 | 379 | | Value = example.Value != null ? JsonNodeClone(example.Value) : null, |
| | 2 | 380 | | ExternalValue = example.ExternalValue, |
| | 2 | 381 | | Extensions = example.Extensions.Clone() |
| | 2 | 382 | | }; |
| | 2 | 383 | | return clone; |
| | | 384 | | } |
| | | 385 | | #endregion |
| | | 386 | | #region Schema |
| | | 387 | | /// <summary> |
| | | 388 | | /// Clones an IOpenApiSchema instance. |
| | | 389 | | /// </summary> |
| | | 390 | | /// <param name="schema">The IOpenApiSchema to clone.</param> |
| | | 391 | | /// <returns>A new IOpenApiSchema instance with the same properties as the input schema.</returns> |
| | | 392 | | public static IOpenApiSchema Clone(this IOpenApiSchema schema) => |
| | 3 | 393 | | schema switch |
| | 3 | 394 | | { |
| | 0 | 395 | | OpenApiSchemaReference schemaRef => schemaRef.Clone(), |
| | 3 | 396 | | OpenApiSchema schemaObj => schemaObj.Clone(), |
| | 0 | 397 | | _ => throw new InvalidOperationException("Unsupported IOpenApiSchema implementation.") |
| | 3 | 398 | | }; |
| | | 399 | | |
| | | 400 | | /// <summary> |
| | | 401 | | /// Clones an OpenApiSchema instance. |
| | | 402 | | /// </summary> |
| | | 403 | | /// <param name="schema">The OpenApiSchema to clone.</param> |
| | | 404 | | /// <returns>A new OpenApiSchema instance with the same properties as the input schema.</returns> |
| | | 405 | | public static OpenApiSchema Clone(this OpenApiSchema schema) |
| | | 406 | | { |
| | 3 | 407 | | var clone = new OpenApiSchema() |
| | 3 | 408 | | { |
| | 3 | 409 | | Title = schema.Title, |
| | 3 | 410 | | Id = schema.Id, |
| | 3 | 411 | | Const = schema.Const, |
| | 3 | 412 | | Schema = schema.Schema, |
| | 3 | 413 | | Comment = schema.Comment, |
| | 3 | 414 | | Vocabulary = schema.Vocabulary != null ? new Dictionary<string, bool>(schema.Vocabulary) : null, |
| | 3 | 415 | | DynamicAnchor = schema.DynamicAnchor, |
| | 3 | 416 | | DynamicRef = schema.DynamicRef, |
| | 3 | 417 | | Definitions = schema.Definitions.Clone(), |
| | 3 | 418 | | UnevaluatedProperties = schema.UnevaluatedProperties, |
| | 3 | 419 | | ExclusiveMaximum = schema.ExclusiveMaximum, |
| | 3 | 420 | | ExclusiveMinimum = schema.ExclusiveMinimum, |
| | 3 | 421 | | |
| | 3 | 422 | | Type = schema.Type, |
| | 3 | 423 | | Format = schema.Format, |
| | 3 | 424 | | Description = schema.Description, |
| | 3 | 425 | | Maximum = schema.Maximum, |
| | 3 | 426 | | Minimum = schema.Minimum, |
| | 3 | 427 | | MaxLength = schema.MaxLength, |
| | 3 | 428 | | MinLength = schema.MinLength, |
| | 3 | 429 | | Pattern = schema.Pattern, |
| | 3 | 430 | | MultipleOf = schema.MultipleOf, |
| | 3 | 431 | | Default = JsonNodeClone(schema.Default), |
| | 3 | 432 | | ReadOnly = schema.ReadOnly, |
| | 3 | 433 | | WriteOnly = schema.WriteOnly, |
| | 3 | 434 | | AllOf = schema.AllOf?.Clone(), |
| | 3 | 435 | | OneOf = schema.OneOf?.Clone(), |
| | 3 | 436 | | AnyOf = schema.AnyOf?.Clone(), |
| | 3 | 437 | | Not = schema.Not?.Clone(), |
| | 3 | 438 | | Required = schema.Required != null ? new HashSet<string>(schema.Required) : null, |
| | 3 | 439 | | Items = schema.Items?.Clone(), |
| | 3 | 440 | | MaxItems = schema.MaxItems, |
| | 3 | 441 | | MinItems = schema.MinItems, |
| | 3 | 442 | | UniqueItems = schema.UniqueItems, |
| | 3 | 443 | | Properties = schema.Properties.Clone(), |
| | 3 | 444 | | PatternProperties = schema.PatternProperties?.Clone(), |
| | 3 | 445 | | MaxProperties = schema.MaxProperties, |
| | 3 | 446 | | MinProperties = schema.MinProperties, |
| | 3 | 447 | | AdditionalPropertiesAllowed = schema.AdditionalPropertiesAllowed, |
| | 3 | 448 | | AdditionalProperties = schema.AdditionalProperties?.Clone(), |
| | 3 | 449 | | Discriminator = schema.Discriminator != null ? new(schema.Discriminator) : null, |
| | 3 | 450 | | Example = schema.Example != null ? JsonNodeClone(schema.Example) : null, |
| | 3 | 451 | | Examples = schema.Examples != null ? [.. schema.Examples] : null, |
| | 3 | 452 | | Enum = schema.Enum != null ? [.. schema.Enum] : null, |
| | 3 | 453 | | ExternalDocs = schema.ExternalDocs != null ? new(schema.ExternalDocs) : null, |
| | 3 | 454 | | Deprecated = schema.Deprecated, |
| | 3 | 455 | | Xml = schema.Xml != null ? new(schema.Xml) : null, |
| | 3 | 456 | | Extensions = schema.Extensions.Clone(), |
| | 3 | 457 | | Metadata = schema is IMetadataContainer { Metadata: not null } mContainer ? new Dictionary<string, object>(m |
| | 3 | 458 | | UnrecognizedKeywords = schema.UnrecognizedKeywords != null ? new Dictionary<string, JsonNode>(schema.Unrecog |
| | 3 | 459 | | DependentRequired = schema.DependentRequired != null ? new Dictionary<string, HashSet<string>>(schema.Depend |
| | 3 | 460 | | }; |
| | 3 | 461 | | return clone; |
| | | 462 | | } |
| | | 463 | | /// <summary> |
| | | 464 | | /// Clones an OpenApiSchemaReference instance. |
| | | 465 | | /// </summary> |
| | | 466 | | /// <param name="schemaRef">The OpenApiSchemaReference to clone</param> |
| | | 467 | | /// <returns>A new OpenApiSchemaReference instance with the same properties as the input instance.</returns> |
| | | 468 | | public static OpenApiSchemaReference Clone(this OpenApiSchemaReference schemaRef) |
| | | 469 | | { |
| | 0 | 470 | | var cloneRef = new OpenApiSchemaReference(referenceId: schemaRef.Reference.Id!) |
| | 0 | 471 | | { |
| | 0 | 472 | | Reference = schemaRef.Reference, |
| | 0 | 473 | | Title = schemaRef.Title, |
| | 0 | 474 | | Description = schemaRef.Description |
| | 0 | 475 | | }; |
| | 0 | 476 | | return cloneRef; |
| | | 477 | | } |
| | | 478 | | /// <summary> |
| | | 479 | | /// Clones a list of OpenApiSchema instances. |
| | | 480 | | /// </summary> |
| | | 481 | | /// <param name="schemas">The list to clone.</param> |
| | | 482 | | /// <returns>A new list containing cloned OpenApiSchema instances.</returns> |
| | | 483 | | public static IList<IOpenApiSchema>? Clone(this IList<IOpenApiSchema>? schemas) |
| | | 484 | | { |
| | 0 | 485 | | if (schemas == null) |
| | | 486 | | { |
| | 0 | 487 | | return null; |
| | | 488 | | } |
| | 0 | 489 | | var cloneList = new List<IOpenApiSchema>(); |
| | 0 | 490 | | foreach (var schema in schemas) |
| | | 491 | | { |
| | 0 | 492 | | cloneList.Add(schema.Clone()); |
| | | 493 | | } |
| | 0 | 494 | | return cloneList; |
| | | 495 | | } |
| | | 496 | | /// <summary> |
| | | 497 | | /// Clones a dictionary of OpenApiSchema instances. |
| | | 498 | | /// </summary> |
| | | 499 | | /// <param name="schemas">The dictionary to clone.</param> |
| | | 500 | | /// <returns>A new dictionary containing cloned OpenApiSchema instances.</returns> |
| | | 501 | | public static Dictionary<string, IOpenApiSchema>? Clone(this IDictionary<string, IOpenApiSchema>? schemas) |
| | | 502 | | { |
| | 6 | 503 | | if (schemas == null) |
| | | 504 | | { |
| | 5 | 505 | | return null; |
| | | 506 | | } |
| | 1 | 507 | | var clone = new Dictionary<string, IOpenApiSchema>(); |
| | 6 | 508 | | foreach (var kvp in schemas) |
| | | 509 | | { |
| | 2 | 510 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 511 | | } |
| | 1 | 512 | | return clone; |
| | | 513 | | } |
| | | 514 | | #endregion |
| | | 515 | | #region Link |
| | | 516 | | /// <summary> |
| | | 517 | | /// Clones a dictionary of IOpenApiLink instances. |
| | | 518 | | /// </summary> |
| | | 519 | | /// <param name="links">The dictionary of IOpenApiLink instances to clone.</param> |
| | | 520 | | /// <returns>A new dictionary containing cloned IOpenApiLink instances.</returns> |
| | | 521 | | public static IDictionary<string, IOpenApiLink>? Clone(this IDictionary<string, IOpenApiLink>? links) |
| | | 522 | | { |
| | 0 | 523 | | if (links == null) |
| | | 524 | | { |
| | 0 | 525 | | return null; |
| | | 526 | | } |
| | | 527 | | |
| | 0 | 528 | | var clone = new Dictionary<string, IOpenApiLink>(); |
| | 0 | 529 | | foreach (var kvp in links) |
| | | 530 | | { |
| | 0 | 531 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 532 | | } |
| | 0 | 533 | | return clone; |
| | | 534 | | } |
| | | 535 | | |
| | | 536 | | /// <summary> |
| | | 537 | | /// Clones an IOpenApiLink instance. |
| | | 538 | | /// </summary> |
| | | 539 | | /// <param name="link">The IOpenApiLink to clone.</param> |
| | | 540 | | /// <returns>A new IOpenApiLink instance with the same properties as the input link.</returns> |
| | | 541 | | public static IOpenApiLink Clone(this IOpenApiLink link) => |
| | 0 | 542 | | link switch |
| | 0 | 543 | | { |
| | 0 | 544 | | OpenApiLink linkObj => linkObj.Clone(), |
| | 0 | 545 | | OpenApiLinkReference linkRef => linkRef.Clone(), |
| | 0 | 546 | | _ => throw new InvalidOperationException("Unsupported IOpenApiLink implementation.") |
| | 0 | 547 | | }; |
| | | 548 | | |
| | | 549 | | /// <summary> |
| | | 550 | | /// Clones an OpenApiLinkReference instance. |
| | | 551 | | /// </summary> |
| | | 552 | | /// <param name="link">The OpenApiLinkReference instance to clone.</param> |
| | | 553 | | /// <returns>A new OpenApiLinkReference instance with the same properties as the input instance.</returns> |
| | | 554 | | public static OpenApiLinkReference Clone(this OpenApiLinkReference link) |
| | | 555 | | { |
| | 0 | 556 | | var clone = new OpenApiLinkReference(link.Reference.Id!) |
| | 0 | 557 | | { |
| | 0 | 558 | | Reference = link.Reference |
| | 0 | 559 | | }; |
| | 0 | 560 | | return clone; |
| | | 561 | | } |
| | | 562 | | |
| | | 563 | | /// <summary> |
| | | 564 | | /// Clones an OpenApiLink instance. |
| | | 565 | | /// </summary> |
| | | 566 | | /// <param name="link">The OpenApiLink instance to clone.</param> |
| | | 567 | | /// <returns>A new OpenApiLink instance with the same properties as the input instance.</returns> |
| | | 568 | | public static OpenApiLink Clone(this OpenApiLink link) |
| | | 569 | | { |
| | 0 | 570 | | var clone = new OpenApiLink |
| | 0 | 571 | | { |
| | 0 | 572 | | OperationRef = link.OperationRef, |
| | 0 | 573 | | OperationId = link.OperationId, |
| | 0 | 574 | | Parameters = link.Parameters.Clone(), |
| | 0 | 575 | | RequestBody = link.RequestBody!.Clone(), |
| | 0 | 576 | | Description = link.Description, |
| | 0 | 577 | | Server = link.Server?.Clone(), |
| | 0 | 578 | | Extensions = link.Extensions.Clone() |
| | 0 | 579 | | }; |
| | 0 | 580 | | return clone; |
| | | 581 | | } |
| | | 582 | | #endregion |
| | | 583 | | |
| | | 584 | | /// <summary> |
| | | 585 | | /// Clones an OpenApiServer instance. |
| | | 586 | | /// </summary> |
| | | 587 | | /// <param name="server">The OpenApiServer instance to clone.</param> |
| | | 588 | | /// <returns>A new OpenApiServer instance with the same properties as the input instance.</returns> |
| | | 589 | | public static OpenApiServer Clone(this OpenApiServer server) |
| | | 590 | | { |
| | 0 | 591 | | var clone = new OpenApiServer |
| | 0 | 592 | | { |
| | 0 | 593 | | Url = server.Url, |
| | 0 | 594 | | Description = server.Description, |
| | 0 | 595 | | Variables = server.Variables != null ? new Dictionary<string, OpenApiServerVariable>(server.Variables) : nul |
| | 0 | 596 | | Extensions = server.Extensions.Clone() |
| | 0 | 597 | | }; |
| | 0 | 598 | | return clone; |
| | | 599 | | } |
| | | 600 | | |
| | | 601 | | #region RuntimeExpression |
| | | 602 | | /// <summary> |
| | | 603 | | /// Clones a RuntimeExpressionAnyWrapper instance. |
| | | 604 | | /// </summary> |
| | | 605 | | /// <param name="expressionWrapper">The RuntimeExpressionAnyWrapper instance to clone.</param> |
| | | 606 | | /// <returns>A new RuntimeExpressionAnyWrapper instance with the same properties as the input instance.</returns> |
| | | 607 | | public static RuntimeExpressionAnyWrapper Clone(this RuntimeExpressionAnyWrapper expressionWrapper) |
| | | 608 | | { |
| | 0 | 609 | | return new RuntimeExpressionAnyWrapper |
| | 0 | 610 | | { |
| | 0 | 611 | | Expression = expressionWrapper.Expression, |
| | 0 | 612 | | Any = expressionWrapper.Any != null ? JsonNodeClone(expressionWrapper.Any) : null |
| | 0 | 613 | | }; |
| | | 614 | | } |
| | | 615 | | |
| | | 616 | | /// <summary> |
| | | 617 | | /// Clones a dictionary of RuntimeExpressionAnyWrapper instances. |
| | | 618 | | /// </summary> |
| | | 619 | | /// <param name="parameters">The dictionary of RuntimeExpressionAnyWrapper instances to clone.</param> |
| | | 620 | | /// <returns>A new dictionary that is a deep clone of the input dictionary.</returns> |
| | | 621 | | public static IDictionary<string, RuntimeExpressionAnyWrapper>? Clone(this IDictionary<string, RuntimeExpressionAnyW |
| | | 622 | | { |
| | 0 | 623 | | if (parameters == null) |
| | | 624 | | { |
| | 0 | 625 | | return null; |
| | | 626 | | } |
| | | 627 | | |
| | 0 | 628 | | var clone = new Dictionary<string, RuntimeExpressionAnyWrapper>(); |
| | 0 | 629 | | foreach (var kvp in parameters) |
| | | 630 | | { |
| | 0 | 631 | | clone[kvp.Key] = kvp.Value.Clone(); |
| | | 632 | | } |
| | 0 | 633 | | return clone; |
| | | 634 | | } |
| | | 635 | | |
| | | 636 | | #endregion |
| | | 637 | | #region JsonNode |
| | | 638 | | /// <summary> |
| | | 639 | | /// Clones a JsonNode instance. |
| | | 640 | | /// </summary> |
| | | 641 | | /// <param name="value">The JsonNode to clone.</param> |
| | | 642 | | /// <returns>A new JsonNode instance that is a deep clone of the input value.</returns> |
| | 5 | 643 | | public static JsonNode? JsonNodeClone(JsonNode? value) => value?.DeepClone(); |
| | | 644 | | #endregion |
| | | 645 | | } |