|
| 1 | +// ------------------------------------------------------------ |
| 2 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. |
| 4 | +// ------------------------------------------------------------ |
| 5 | + |
| 6 | +using System; |
| 7 | +using System.Collections.Generic; |
| 8 | +using System.Linq; |
| 9 | +using Microsoft.OpenApi.Extensions; |
| 10 | +using Microsoft.OpenApi.Any; |
| 11 | +using Microsoft.OpenApi.Interfaces; |
| 12 | +using Microsoft.OpenApi.Writers; |
| 13 | + |
| 14 | +namespace Microsoft.OpenApi.MicrosoftExtensions; |
| 15 | + |
| 16 | +/// <summary> |
| 17 | +/// Extension element for OpenAPI to add enum values descriptions. |
| 18 | +/// Based of the AutoRest specification https://github.com/Azure/autorest/blob/main/docs/extensions/readme.md#x-ms-enum |
| 19 | +/// </summary> |
| 20 | +public class OpenApiEnumValuesDescriptionExtension : IOpenApiExtension |
| 21 | +{ |
| 22 | + /// <summary> |
| 23 | + /// Name of the extension as used in the description. |
| 24 | + /// </summary> |
| 25 | + public static string Name => "x-ms-enum"; |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// The of the enum. |
| 29 | + /// </summary> |
| 30 | + public string EnumName { get; set; } = string.Empty; |
| 31 | + |
| 32 | + /// <summary> |
| 33 | + /// Descriptions for the enum symbols, where the value MUST match the enum symbols in the main description |
| 34 | + /// </summary> |
| 35 | + public List<EnumDescription> ValuesDescriptions { get; set; } = new(); |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) |
| 39 | + { |
| 40 | + if (writer is null) throw new ArgumentNullException(nameof(writer)); |
| 41 | + if ((specVersion == OpenApiSpecVersion.OpenApi2_0 || specVersion == OpenApiSpecVersion.OpenApi3_0) && |
| 42 | + !string.IsNullOrEmpty(EnumName) && |
| 43 | + ValuesDescriptions.Any()) |
| 44 | + { // when we upgrade to 3.1, we don't need to write this extension as JSON schema will support writing enum values |
| 45 | + writer.WriteStartObject(); |
| 46 | + writer.WriteProperty(nameof(Name).ToFirstCharacterLowerCase(), EnumName); |
| 47 | + writer.WriteProperty("modelAsString", false); |
| 48 | + writer.WriteRequiredCollection("values", ValuesDescriptions, (w, x) => |
| 49 | + { |
| 50 | + w.WriteStartObject(); |
| 51 | + w.WriteProperty(nameof(x.Value).ToFirstCharacterLowerCase(), x.Value); |
| 52 | + w.WriteProperty(nameof(x.Description).ToFirstCharacterLowerCase(), x.Description); |
| 53 | + w.WriteProperty(nameof(x.Name).ToFirstCharacterLowerCase(), x.Name); |
| 54 | + w.WriteEndObject(); |
| 55 | + }); |
| 56 | + writer.WriteEndObject(); |
| 57 | + } |
| 58 | + } |
| 59 | + /// <summary> |
| 60 | + /// Parse the extension from the raw IOpenApiAny object. |
| 61 | + /// </summary> |
| 62 | + /// <param name="source">The source element to parse.</param> |
| 63 | + /// <returns>The <see cref="OpenApiEnumValuesDescriptionExtension"/>.</returns> |
| 64 | + /// <exception cref="ArgumentOutOfRangeException">When the source element is not an object</exception> |
| 65 | + public static OpenApiEnumValuesDescriptionExtension Parse(IOpenApiAny source) |
| 66 | + { |
| 67 | + if (source is not OpenApiObject rawObject) throw new ArgumentOutOfRangeException(nameof(source)); |
| 68 | + var extension = new OpenApiEnumValuesDescriptionExtension(); |
| 69 | + if (rawObject.TryGetValue("values", out var values) && values is OpenApiArray valuesArray) |
| 70 | + { |
| 71 | + extension.ValuesDescriptions.AddRange(valuesArray |
| 72 | + .OfType<OpenApiObject>() |
| 73 | + .Select(x => new EnumDescription(x))); |
| 74 | + } |
| 75 | + return extension; |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// <summary> |
| 80 | +/// Description of an enum symbol |
| 81 | +/// </summary> |
| 82 | +public class EnumDescription : IOpenApiElement |
| 83 | +{ |
| 84 | + /// <summary> |
| 85 | + /// Default constructor |
| 86 | + /// </summary> |
| 87 | + public EnumDescription() |
| 88 | + { |
| 89 | + |
| 90 | + } |
| 91 | + /// <summary> |
| 92 | + /// Constructor from a raw OpenApiObject |
| 93 | + /// </summary> |
| 94 | + /// <param name="source">The source object</param> |
| 95 | + public EnumDescription(OpenApiObject source) |
| 96 | + { |
| 97 | + if (source is null) throw new ArgumentNullException(nameof(source)); |
| 98 | + if (source.TryGetValue(nameof(Value).ToFirstCharacterLowerCase(), out var rawValue) && rawValue is OpenApiString value) |
| 99 | + Value = value.Value; |
| 100 | + if (source.TryGetValue(nameof(Description).ToFirstCharacterLowerCase(), out var rawDescription) && rawDescription is OpenApiString description) |
| 101 | + Description = description.Value; |
| 102 | + if (source.TryGetValue(nameof(Name).ToFirstCharacterLowerCase(), out var rawName) && rawName is OpenApiString name) |
| 103 | + Name = name.Value; |
| 104 | + } |
| 105 | + /// <summary> |
| 106 | + /// The description for the enum symbol |
| 107 | + /// </summary> |
| 108 | + public string Description { get; set; } = string.Empty; |
| 109 | + /// <summary> |
| 110 | + /// The symbol for the enum symbol to use for code-generation |
| 111 | + /// </summary> |
| 112 | + public string Name { get; set; } = string.Empty; |
| 113 | + /// <summary> |
| 114 | + /// The symbol as described in the main enum schema. |
| 115 | + /// </summary> |
| 116 | + public string Value { get; set; } = string.Empty; |
| 117 | +} |
0 commit comments