-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #417 from microsoft/andrueastman/ms-flags
Pass Bitwise Enum information
- Loading branch information
Showing
29 changed files
with
207 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
src/Microsoft.OpenApi.OData.Reader/OpenApiExtensions/OpenApiEnumFlagsExtension.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// ------------------------------------------------------------ | ||
// 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 Microsoft.OpenApi.Interfaces; | ||
using Microsoft.OpenApi.OData.Common; | ||
using Microsoft.OpenApi.Writers; | ||
|
||
namespace Microsoft.OpenApi.OData.OpenApiExtensions; | ||
|
||
/// <summary> | ||
/// Extension element for OpenAPI to add deprecation information. x-ms-enum-flags | ||
/// </summary> | ||
public class OpenApiEnumFlagsExtension : IOpenApiExtension | ||
{ | ||
/// <summary> | ||
/// Name of the extension as used in the description. | ||
/// </summary> | ||
public string Name => "x-ms-enum-flags"; | ||
/// <summary> | ||
/// Whether the enum is a flagged enum. | ||
/// </summary> | ||
public bool IsFlags { get; set; } | ||
/// <summary> | ||
/// The serialization style of the flagged enum. | ||
/// </summary> | ||
public string Style { get; set; } | ||
/// <inheritdoc /> | ||
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) | ||
{ | ||
if(writer == null) | ||
throw new ArgumentNullException(nameof(writer)); | ||
|
||
writer.WriteStartObject(); | ||
writer.WriteProperty(nameof(IsFlags).ToFirstCharacterLowerCase(), IsFlags); | ||
writer.WriteProperty(nameof(Style).ToFirstCharacterLowerCase(),Style); | ||
writer.WriteEndObject(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
.../Microsoft.OpenAPI.OData.Reader.Tests/OpenApiExtensions/OpenApiEnumFlagsExtensionTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// ------------------------------------------------------------ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information. | ||
// ------------------------------------------------------------ | ||
|
||
using System.IO; | ||
using Microsoft.OpenApi.Writers; | ||
using Xunit; | ||
|
||
namespace Microsoft.OpenApi.OData.OpenApiExtensions.Tests; | ||
|
||
public class OpenApiEnumFlagsExtensionTests | ||
{ | ||
[Fact] | ||
public void ExtensionNameMatchesExpected() | ||
{ | ||
// Arrange | ||
OpenApiEnumFlagsExtension extension = new(); | ||
|
||
// Act | ||
string name = extension.Name; | ||
string expectedName = "x-ms-enum-flags"; | ||
|
||
// Assert | ||
Assert.Equal(expectedName, name); | ||
} | ||
|
||
[Fact] | ||
public void WritesDefaultValues() | ||
{ | ||
// Arrange | ||
OpenApiEnumFlagsExtension extension = new(); | ||
using TextWriter sWriter = new StringWriter(); | ||
OpenApiJsonWriter writer = new(sWriter); | ||
|
||
// Act | ||
extension.Write(writer, OpenApiSpecVersion.OpenApi3_0); | ||
string result = sWriter.ToString(); | ||
|
||
// Assert | ||
Assert.False(extension.IsFlags); | ||
Assert.Null(extension.Style); | ||
Assert.Contains("\"isFlags\": false", result); | ||
Assert.DoesNotContain("\"style\"", result); | ||
} | ||
|
||
[Fact] | ||
public void WritesAllDefaultValues() | ||
{ | ||
// Arrange | ||
OpenApiEnumFlagsExtension extension = new() { | ||
IsFlags = true | ||
}; | ||
using TextWriter sWriter = new StringWriter(); | ||
OpenApiJsonWriter writer = new(sWriter); | ||
|
||
// Act | ||
extension.Write(writer, OpenApiSpecVersion.OpenApi3_0); | ||
string result = sWriter.ToString(); | ||
|
||
// Assert | ||
Assert.True(extension.IsFlags); | ||
Assert.Null(extension.Style); | ||
Assert.Contains("\"isFlags\": true", result); | ||
Assert.DoesNotContain("\"style\":", result); | ||
} | ||
|
||
[Fact] | ||
public void WritesAllValues() | ||
{ | ||
// Arrange | ||
OpenApiEnumFlagsExtension extension = new() { | ||
IsFlags = true, | ||
Style = "simple" | ||
}; | ||
using TextWriter sWriter = new StringWriter(); | ||
OpenApiJsonWriter writer = new(sWriter); | ||
|
||
// Act | ||
extension.Write(writer, OpenApiSpecVersion.OpenApi3_0); | ||
string result = sWriter.ToString(); | ||
|
||
// Assert | ||
Assert.True(extension.IsFlags); | ||
Assert.NotNull(extension.Style); | ||
Assert.Contains("\"isFlags\": true", result); | ||
Assert.Contains("\"style\": \"simple\"", result); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.