|
| 1 | +using System.Collections.Generic; |
| 2 | +using Microsoft.OpenApi.Interfaces; |
| 3 | +using Microsoft.OpenApi.Models; |
| 4 | + |
| 5 | +namespace Microsoft.OpenApi.Services |
| 6 | +{ |
| 7 | + internal class CopyReferences : OpenApiVisitorBase |
| 8 | + { |
| 9 | + private readonly OpenApiDocument target; |
| 10 | + public OpenApiComponents Components = new OpenApiComponents(); |
| 11 | + |
| 12 | + public CopyReferences(OpenApiDocument target) |
| 13 | + { |
| 14 | + this.target = target; |
| 15 | + } |
| 16 | + |
| 17 | + public override void Visit(IOpenApiReferenceable referenceable) |
| 18 | + { |
| 19 | + switch (referenceable) |
| 20 | + { |
| 21 | + case OpenApiSchema schema: |
| 22 | + EnsureComponentsExists(); |
| 23 | + EnsureSchemasExists(); |
| 24 | + if (!Components.Schemas.ContainsKey(schema.Reference.Id)) |
| 25 | + { |
| 26 | + Components.Schemas.Add(schema.Reference.Id, schema); |
| 27 | + } |
| 28 | + break; |
| 29 | + |
| 30 | + case OpenApiParameter parameter: |
| 31 | + EnsureComponentsExists(); |
| 32 | + EnsureParametersExists(); |
| 33 | + if (!Components.Parameters.ContainsKey(parameter.Reference.Id)) |
| 34 | + { |
| 35 | + Components.Parameters.Add(parameter.Reference.Id, parameter); |
| 36 | + } |
| 37 | + break; |
| 38 | + |
| 39 | + case OpenApiResponse response: |
| 40 | + EnsureComponentsExists(); |
| 41 | + EnsureResponsesExists(); |
| 42 | + if (!Components.Responses.ContainsKey(response.Reference.Id)) |
| 43 | + { |
| 44 | + Components.Responses.Add(response.Reference.Id, response); |
| 45 | + } |
| 46 | + break; |
| 47 | + |
| 48 | + default: |
| 49 | + break; |
| 50 | + } |
| 51 | + base.Visit(referenceable); |
| 52 | + } |
| 53 | + |
| 54 | + public override void Visit(OpenApiSchema schema) |
| 55 | + { |
| 56 | + // This is needed to handle schemas used in Responses in components |
| 57 | + if (schema.Reference != null) |
| 58 | + { |
| 59 | + EnsureComponentsExists(); |
| 60 | + EnsureSchemasExists(); |
| 61 | + if (!Components.Schemas.ContainsKey(schema.Reference.Id)) |
| 62 | + { |
| 63 | + Components.Schemas.Add(schema.Reference.Id, schema); |
| 64 | + } |
| 65 | + } |
| 66 | + base.Visit(schema); |
| 67 | + } |
| 68 | + |
| 69 | + private void EnsureComponentsExists() |
| 70 | + { |
| 71 | + if (target.Components == null) |
| 72 | + { |
| 73 | + target.Components = new OpenApiComponents(); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private void EnsureSchemasExists() |
| 78 | + { |
| 79 | + if (target.Components.Schemas == null) |
| 80 | + { |
| 81 | + target.Components.Schemas = new Dictionary<string, OpenApiSchema>(); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + private void EnsureParametersExists() |
| 86 | + { |
| 87 | + if (target.Components.Parameters == null) |
| 88 | + { |
| 89 | + target.Components.Parameters = new Dictionary<string, OpenApiParameter>(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private void EnsureResponsesExists() |
| 94 | + { |
| 95 | + if (target.Components.Responses == null) |
| 96 | + { |
| 97 | + target.Components.Responses = new Dictionary<string, OpenApiResponse>(); |
| 98 | + } |
| 99 | + } |
| 100 | + } |
| 101 | +} |
0 commit comments