-
Notifications
You must be signed in to change notification settings - Fork 64
adds support for enum descriptions #168
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
cde2c08
7192f2a
535a7e1
e13db15
e65e32b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.Linq; | ||
| using Microsoft.OpenApi.Interfaces; | ||
| using Microsoft.OpenApi.OData.Common; | ||
| using Microsoft.OpenApi.Writers; | ||
|
|
||
| namespace Microsoft.OpenApi.OData.OpenApiExtensions; | ||
|
|
||
| /// <summary> | ||
| /// Extension element for OpenAPI to add enum values descriptions. | ||
| /// Based of the AutoRest specification https://github.com/Azure/autorest/blob/main/docs/extensions/readme.md#x-ms-enum | ||
| /// </summary> | ||
| internal class OpenApiEnumValuesDescriptionExtension : IOpenApiExtension | ||
| { | ||
| /// <summary> | ||
| /// Name of the extension as used in the description. | ||
| /// </summary> | ||
| public string Name => "x-ms-enum"; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. static or static readonly since it's constant? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this property is required by the interface, and this syntax makes it readonly. |
||
|
|
||
| /// <summary> | ||
| /// The of the enum. | ||
| /// </summary> | ||
| public string EnumName { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Descriptions for the enum symbols, where the value MUST match the enum symbols in the main description | ||
| /// </summary> | ||
| public List<EnumDescription> ValuesDescriptions { get; set; } = new(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Doesn't need the "set" for the collection since you have new(), right? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the set is here so people can assign the whole collection (used in unit tests) |
||
|
|
||
| /// <inheritdoc /> | ||
| public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) | ||
| { | ||
| if(writer == null) | ||
| throw new ArgumentNullException(nameof(writer)); | ||
| if((specVersion == OpenApiSpecVersion.OpenApi2_0 || specVersion == OpenApiSpecVersion.OpenApi3_0) && | ||
| !string.IsNullOrEmpty(EnumName) && | ||
| ValuesDescriptions.Any()) | ||
| { // when we upgrade to 3.1, we don't need to write this extension as JSON schema will support writing enum values | ||
| writer.WriteStartObject(); | ||
| writer.WriteProperty(nameof(Name).ToFirstCharacterLowerCase(), EnumName); | ||
| writer.WriteProperty("modelAsString", false); | ||
| writer.WriteRequiredCollection("values", ValuesDescriptions, (w, x) => { | ||
| w.WriteStartObject(); | ||
| w.WriteProperty(nameof(x.Value).ToFirstCharacterLowerCase(), x.Value); | ||
| w.WriteProperty(nameof(x.Description).ToFirstCharacterLowerCase(), x.Description); | ||
| w.WriteProperty(nameof(x.Name).ToFirstCharacterLowerCase(), x.Name); | ||
| w.WriteEndObject(); | ||
| }); | ||
| writer.WriteEndObject(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| internal class EnumDescription : IOpenApiElement | ||
| { | ||
| /// <summary> | ||
| /// The description for the enum symbol | ||
| /// </summary> | ||
| public string Description { get; set; } | ||
| /// <summary> | ||
| /// The symbol for the enum symbol to use for code-generation | ||
| /// </summary> | ||
| public string Name { get; set; } | ||
| /// <summary> | ||
| /// The symbol as described in the main enum schema. | ||
| /// </summary> | ||
| public string Value { get; set; } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| // ------------------------------------------------------------ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
| // ------------------------------------------------------------ | ||
|
|
||
| using System.Collections.Generic; | ||
| using System.IO; | ||
| using Microsoft.OpenApi.Writers; | ||
| using Xunit; | ||
|
|
||
| namespace Microsoft.OpenApi.OData.OpenApiExtensions.Tests; | ||
|
|
||
| public class OpenApiEnumValuesDescriptionExtensionTexts | ||
| { | ||
| [Fact] | ||
| public void ExtensionNameMatchesExpected() | ||
| { | ||
| // Arrange | ||
| OpenApiEnumValuesDescriptionExtension extension = new(); | ||
|
|
||
| // Act | ||
| string name = extension.Name; | ||
| string expectedName = "x-ms-enum"; | ||
|
|
||
| // Assert | ||
| Assert.Equal(expectedName, name); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void WritesNothingWhenNoValues() | ||
| { | ||
| // Arrange | ||
| OpenApiEnumValuesDescriptionExtension extension = new(); | ||
| using TextWriter sWriter = new StringWriter(); | ||
| OpenApiJsonWriter writer = new(sWriter); | ||
|
|
||
| // Act | ||
| extension.Write(writer, OpenApiSpecVersion.OpenApi3_0); | ||
| string result = sWriter.ToString(); | ||
|
|
||
| // Assert | ||
| Assert.Null(extension.EnumName); | ||
| Assert.Empty(extension.ValuesDescriptions); | ||
| Assert.Empty(result); | ||
| } | ||
| [Fact] | ||
| public void WritesEnumDescription() | ||
| { | ||
| // Arrange | ||
| OpenApiEnumValuesDescriptionExtension extension = new(); | ||
| extension.EnumName = "TestEnum"; | ||
| extension.ValuesDescriptions = new() | ||
| { | ||
| new() { | ||
| Description = "TestDescription", | ||
| Value = "TestValue", | ||
| Name = "TestName" | ||
| } | ||
| }; | ||
| using TextWriter sWriter = new StringWriter(); | ||
| OpenApiJsonWriter writer = new(sWriter); | ||
baywet marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // Act | ||
| extension.Write(writer, OpenApiSpecVersion.OpenApi3_0); | ||
| string result = sWriter.ToString(); | ||
|
|
||
| // Assert | ||
| Assert.Contains("values", result); | ||
| Assert.Contains("modelAsString\": false", result); | ||
| Assert.Contains("name\": \"TestEnum", result); | ||
| Assert.Contains("description\": \"TestDescription", result); | ||
| Assert.Contains("value\": \"TestValue", result); | ||
| Assert.Contains("name\": \"TestName", result); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28511,12 +28511,34 @@ | |
| }, | ||
| "Microsoft.OData.Service.Sample.TrippinInMemory.Models.PersonGender": { | ||
| "title": "PersonGender", | ||
| "description": "Gender of the person.", | ||
| "enum": [ | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we extend the item in the array to be a complex object, which has the "name" and description? In this case, we don't need to add an extension to repeat the name or value for enum member? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here I'm just implementing this spec so people also get compatibility with auto rest should they be using it afterwards https://github.com/Azure/autorest/blob/main/docs/extensions/readme.md#x-ms-enum |
||
| "Male", | ||
| "Female", | ||
| "Unknow" | ||
| ], | ||
| "type": "string" | ||
| "type": "string", | ||
| "x-ms-enum": { | ||
| "name": "PersonGender", | ||
| "modelAsString": false, | ||
| "values": [ | ||
| { | ||
| "value": "Male", | ||
| "description": "The Male gender.", | ||
| "name": "Male" | ||
| }, | ||
| { | ||
| "value": "Female", | ||
| "description": "The Female gender.", | ||
| "name": "Female" | ||
| }, | ||
| { | ||
| "value": "Unknow", | ||
| "description": "Unknown gender or prefers not to say.", | ||
| "name": "Unknow" | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "Microsoft.OData.Service.Sample.TrippinInMemory.Models.Feature": { | ||
| "title": "Feature", | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.