diff --git a/.vscode/extensions.json b/.vscode/extensions.json index ec8565fa..7d3949f2 100644 --- a/.vscode/extensions.json +++ b/.vscode/extensions.json @@ -1,6 +1,5 @@ { "recommendations": [ - "formulahendry.dotnet-test-explorer", "ms-dotnettools.csharp", "editorconfig.editorconfig" ] diff --git a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiErrorSchemaGenerator.cs b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiErrorSchemaGenerator.cs index 879a2665..a5fa11c1 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiErrorSchemaGenerator.cs +++ b/src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiErrorSchemaGenerator.cs @@ -6,9 +6,11 @@ using System.Collections.Generic; using System.Linq; using Microsoft.OData.Edm; +using Microsoft.OpenApi.Interfaces; using Microsoft.OpenApi.Models; using Microsoft.OpenApi.OData.Common; using Microsoft.OpenApi.OData.Edm; +using Microsoft.OpenApi.OData.OpenApiExtensions; namespace Microsoft.OpenApi.OData.Generator { @@ -133,7 +135,8 @@ public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName) "code", new OpenApiSchema { Type = "string", Nullable = false } }, { - "message", new OpenApiSchema { Type = "string", Nullable = false, } + "message", new OpenApiSchema { Type = "string", Nullable = false, Extensions = new Dictionary + { { OpenApiPrimaryErrorMessageExtension.Name, new OpenApiPrimaryErrorMessageExtension { IsPrimaryErrorMessage = true } } } } }, { "target", new OpenApiSchema { Type = "string", Nullable = true } diff --git a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj index a5a2aed7..4d2a870e 100644 --- a/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj +++ b/src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj @@ -15,7 +15,7 @@ netstandard2.0 Microsoft.OpenApi.OData true - 1.5.0-preview3 + 1.5.0-preview4 This package contains the codes you need to convert OData CSDL to Open API Document of Model. © Microsoft Corporation. All rights reserved. Microsoft OpenApi OData EDM diff --git a/src/Microsoft.OpenApi.OData.Reader/OpenApiExtensions/OpenApiPrimaryErrorMessageExtension.cs b/src/Microsoft.OpenApi.OData.Reader/OpenApiExtensions/OpenApiPrimaryErrorMessageExtension.cs new file mode 100644 index 00000000..4aa4ed6e --- /dev/null +++ b/src/Microsoft.OpenApi.OData.Reader/OpenApiExtensions/OpenApiPrimaryErrorMessageExtension.cs @@ -0,0 +1,45 @@ +// ------------------------------------------------------------ +// 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.Any; +using Microsoft.OpenApi.Interfaces; +using Microsoft.OpenApi.Writers; + +namespace Microsoft.OpenApi.OData.OpenApiExtensions; + +/// +/// Extension element for OpenAPI to add tag the primary error message to use on error types. x-ms-primary-error-message +/// +public class OpenApiPrimaryErrorMessageExtension : IOpenApiExtension +{ + /// + /// Name of the extension as used in the description. + /// + public static string Name => "x-ms-primary-error-message"; + /// + public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion) + { + if(writer == null) + throw new ArgumentNullException(nameof(writer)); + writer.WriteValue(IsPrimaryErrorMessage); + } + /// + /// Whether this property is the primary error message to use on error types. + /// + public bool IsPrimaryErrorMessage { get; set; } + /// + /// Parses the to . + /// + /// The source object. + /// The . + public static OpenApiPrimaryErrorMessageExtension Parse(IOpenApiAny source) + { + if (source is not OpenApiBoolean rawObject) throw new ArgumentOutOfRangeException(nameof(source)); + return new OpenApiPrimaryErrorMessageExtension() { + IsPrimaryErrorMessage = rawObject.Value + }; + } +} \ No newline at end of file diff --git a/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt b/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt index b4684880..2059591e 100644 --- a/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt +++ b/src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt @@ -58,6 +58,13 @@ Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.OpenApiEnumF Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.Style.get -> string Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.Style.set -> void Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.Write(Microsoft.OpenApi.Writers.IOpenApiWriter writer, Microsoft.OpenApi.OpenApiSpecVersion specVersion) -> void +Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension +Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.OpenApiPrimaryErrorMessageExtension() -> void +Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.Write(Microsoft.OpenApi.Writers.IOpenApiWriter writer, Microsoft.OpenApi.OpenApiSpecVersion specVersion) -> void +static Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.Name.get -> string +Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.IsPrimaryErrorMessage.get -> bool +Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.IsPrimaryErrorMessage.set -> void +static Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.Parse(Microsoft.OpenApi.Any.IOpenApiAny source) -> Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey.Action = 6 -> Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey.Create = 2 -> Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/OpenApiExtensions/OpenApiPrimaryErrorMessageExtensionTests.cs b/test/Microsoft.OpenAPI.OData.Reader.Tests/OpenApiExtensions/OpenApiPrimaryErrorMessageExtensionTests.cs new file mode 100644 index 00000000..6b400671 --- /dev/null +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/OpenApiExtensions/OpenApiPrimaryErrorMessageExtensionTests.cs @@ -0,0 +1,56 @@ +// ------------------------------------------------------------ +// 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.IO; +using Microsoft.OpenApi.Any; +using Microsoft.OpenApi.Writers; +using Xunit; + +namespace Microsoft.OpenApi.OData.OpenApiExtensions.Tests; + +public class OpenApiPrimaryErrorMessageExtensionTests +{ + [Fact] + public void ExtensionNameMatchesExpected() + { + // Act + string name = OpenApiPrimaryErrorMessageExtension.Name; + string expectedName = "x-ms-primary-error-message"; + + // Assert + Assert.Equal(expectedName, name); + } + [Fact] + public void WritesValue() + { + // Arrange + OpenApiPrimaryErrorMessageExtension extension = new() { + IsPrimaryErrorMessage = 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.IsPrimaryErrorMessage); + Assert.Equal("true", result); + } + [Fact] + public void ParsesValue() + { + // Arrange + var value = new OpenApiBoolean(true); + + // Act + var extension = OpenApiPrimaryErrorMessageExtension.Parse(value); + + // Assert + Assert.True(extension.IsPrimaryErrorMessage); + } +} diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.json index fc0be48c..5be9fd5a 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.json @@ -1034,7 +1034,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string" diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml index d7b71b02..efedb95e 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml @@ -679,6 +679,7 @@ definitions: type: string message: type: string + x-ms-primary-error-message: true target: type: string details: diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.json index 9af2ecaa..c2e137ea 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.json @@ -1137,7 +1137,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string", diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.yaml index d9c07846..81208bcf 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.yaml @@ -748,6 +748,7 @@ components: type: string message: type: string + x-ms-primary-error-message: true target: type: string nullable: true diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.json index 9048e01d..972656d0 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.json @@ -33,7 +33,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string" diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.yaml index 6f24fc47..1f101507 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.V2.yaml @@ -25,6 +25,7 @@ definitions: type: string message: type: string + x-ms-primary-error-message: true target: type: string details: diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.json index 9d42e533..6ed11956 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.json @@ -35,7 +35,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string", diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.yaml index cb39dcf2..9d790631 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Empty.OpenApi.yaml @@ -25,6 +25,7 @@ components: type: string message: type: string + x-ms-primary-error-message: true target: type: string nullable: true diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.json index 0a7e96a2..b636b2a2 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.json @@ -5771,7 +5771,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string" diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.yaml index b3417eb3..c77bf1b6 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.V2.yaml @@ -4201,6 +4201,7 @@ definitions: type: string message: type: string + x-ms-primary-error-message: true target: type: string details: diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.json index fb014b15..1fb9df64 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.json @@ -6505,7 +6505,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string", diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.yaml index 41391f52..bbae78d1 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Multiple.Schema.OpenApi.yaml @@ -4689,6 +4689,7 @@ components: type: string message: type: string + x-ms-primary-error-message: true target: type: string nullable: true diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json index 63189d75..b48b8ec7 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.json @@ -28144,7 +28144,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string" diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml index 86df0ff5..f7e9a04f 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.V2.yaml @@ -19773,6 +19773,7 @@ definitions: type: string message: type: string + x-ms-primary-error-message: true target: type: string details: diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json index 8f9b290f..d748228e 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.json @@ -31534,7 +31534,8 @@ "type": "string" }, "message": { - "type": "string" + "type": "string", + "x-ms-primary-error-message": true }, "target": { "type": "string", diff --git a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml index 5f613dc2..5e7f3fbc 100644 --- a/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml +++ b/test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/TripService.OpenApi.yaml @@ -21959,6 +21959,7 @@ components: type: string message: type: string + x-ms-primary-error-message: true target: type: string nullable: true