Skip to content

Commit 0971117

Browse files
authored
Merge pull request #419 from microsoft/feature/x-ms-primary-error-message
- adds primary message to error generation
2 parents eeb3d41 + 4b9f7e0 commit 0971117

22 files changed

+137
-11
lines changed

.vscode/extensions.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
{
22
"recommendations": [
3-
"formulahendry.dotnet-test-explorer",
43
"ms-dotnettools.csharp",
54
"editorconfig.editorconfig"
65
]

src/Microsoft.OpenApi.OData.Reader/Generator/OpenApiErrorSchemaGenerator.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using Microsoft.OData.Edm;
9+
using Microsoft.OpenApi.Interfaces;
910
using Microsoft.OpenApi.Models;
1011
using Microsoft.OpenApi.OData.Common;
1112
using Microsoft.OpenApi.OData.Edm;
13+
using Microsoft.OpenApi.OData.OpenApiExtensions;
1214

1315
namespace Microsoft.OpenApi.OData.Generator
1416
{
@@ -133,7 +135,8 @@ public static OpenApiSchema CreateErrorMainSchema(string rootNamespaceName)
133135
"code", new OpenApiSchema { Type = "string", Nullable = false }
134136
},
135137
{
136-
"message", new OpenApiSchema { Type = "string", Nullable = false, }
138+
"message", new OpenApiSchema { Type = "string", Nullable = false, Extensions = new Dictionary<string, IOpenApiExtension>
139+
{ { OpenApiPrimaryErrorMessageExtension.Name, new OpenApiPrimaryErrorMessageExtension { IsPrimaryErrorMessage = true } } } }
137140
},
138141
{
139142
"target", new OpenApiSchema { Type = "string", Nullable = true }

src/Microsoft.OpenApi.OData.Reader/Microsoft.OpenAPI.OData.Reader.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
<TargetFrameworks>netstandard2.0</TargetFrameworks>
1616
<PackageId>Microsoft.OpenApi.OData</PackageId>
1717
<SignAssembly>true</SignAssembly>
18-
<Version>1.5.0-preview3</Version>
18+
<Version>1.5.0-preview4</Version>
1919
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
2020
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
2121
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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 Microsoft.OpenApi.Any;
8+
using Microsoft.OpenApi.Interfaces;
9+
using Microsoft.OpenApi.Writers;
10+
11+
namespace Microsoft.OpenApi.OData.OpenApiExtensions;
12+
13+
/// <summary>
14+
/// Extension element for OpenAPI to add tag the primary error message to use on error types. x-ms-primary-error-message
15+
/// </summary>
16+
public class OpenApiPrimaryErrorMessageExtension : IOpenApiExtension
17+
{
18+
/// <summary>
19+
/// Name of the extension as used in the description.
20+
/// </summary>
21+
public static string Name => "x-ms-primary-error-message";
22+
/// <inheritdoc />
23+
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
24+
{
25+
if(writer == null)
26+
throw new ArgumentNullException(nameof(writer));
27+
writer.WriteValue(IsPrimaryErrorMessage);
28+
}
29+
/// <summary>
30+
/// Whether this property is the primary error message to use on error types.
31+
/// </summary>
32+
public bool IsPrimaryErrorMessage { get; set; }
33+
/// <summary>
34+
/// Parses the <see cref="IOpenApiAny"/> to <see cref="OpenApiPrimaryErrorMessageExtension"/>.
35+
/// </summary>
36+
/// <param name="source">The source object.</param>
37+
/// <returns>The <see cref="OpenApiPrimaryErrorMessageExtension"/>.</returns>
38+
public static OpenApiPrimaryErrorMessageExtension Parse(IOpenApiAny source)
39+
{
40+
if (source is not OpenApiBoolean rawObject) throw new ArgumentOutOfRangeException(nameof(source));
41+
return new OpenApiPrimaryErrorMessageExtension() {
42+
IsPrimaryErrorMessage = rawObject.Value
43+
};
44+
}
45+
}

src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,13 @@ Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.OpenApiEnumF
5858
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.Style.get -> string
5959
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.Style.set -> void
6060
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiEnumFlagsExtension.Write(Microsoft.OpenApi.Writers.IOpenApiWriter writer, Microsoft.OpenApi.OpenApiSpecVersion specVersion) -> void
61+
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension
62+
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.OpenApiPrimaryErrorMessageExtension() -> void
63+
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.Write(Microsoft.OpenApi.Writers.IOpenApiWriter writer, Microsoft.OpenApi.OpenApiSpecVersion specVersion) -> void
64+
static Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.Name.get -> string
65+
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.IsPrimaryErrorMessage.get -> bool
66+
Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.IsPrimaryErrorMessage.set -> void
67+
static Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension.Parse(Microsoft.OpenApi.Any.IOpenApiAny source) -> Microsoft.OpenApi.OData.OpenApiExtensions.OpenApiPrimaryErrorMessageExtension
6168
Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey
6269
Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey.Action = 6 -> Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey
6370
Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey.Create = 2 -> Microsoft.OpenApi.OData.Vocabulary.Core.LinkRelKey
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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.IO;
8+
using Microsoft.OpenApi.Any;
9+
using Microsoft.OpenApi.Writers;
10+
using Xunit;
11+
12+
namespace Microsoft.OpenApi.OData.OpenApiExtensions.Tests;
13+
14+
public class OpenApiPrimaryErrorMessageExtensionTests
15+
{
16+
[Fact]
17+
public void ExtensionNameMatchesExpected()
18+
{
19+
// Act
20+
string name = OpenApiPrimaryErrorMessageExtension.Name;
21+
string expectedName = "x-ms-primary-error-message";
22+
23+
// Assert
24+
Assert.Equal(expectedName, name);
25+
}
26+
[Fact]
27+
public void WritesValue()
28+
{
29+
// Arrange
30+
OpenApiPrimaryErrorMessageExtension extension = new() {
31+
IsPrimaryErrorMessage = true
32+
};
33+
using TextWriter sWriter = new StringWriter();
34+
OpenApiJsonWriter writer = new(sWriter);
35+
36+
// Act
37+
extension.Write(writer, OpenApiSpecVersion.OpenApi3_0);
38+
string result = sWriter.ToString();
39+
40+
// Assert
41+
Assert.True(extension.IsPrimaryErrorMessage);
42+
Assert.Equal("true", result);
43+
}
44+
[Fact]
45+
public void ParsesValue()
46+
{
47+
// Arrange
48+
var value = new OpenApiBoolean(true);
49+
50+
// Act
51+
var extension = OpenApiPrimaryErrorMessageExtension.Parse(value);
52+
53+
// Assert
54+
Assert.True(extension.IsPrimaryErrorMessage);
55+
}
56+
}

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1034,7 +1034,8 @@
10341034
"type": "string"
10351035
},
10361036
"message": {
1037-
"type": "string"
1037+
"type": "string",
1038+
"x-ms-primary-error-message": true
10381039
},
10391040
"target": {
10401041
"type": "string"

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.V2.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -679,6 +679,7 @@ definitions:
679679
type: string
680680
message:
681681
type: string
682+
x-ms-primary-error-message: true
682683
target:
683684
type: string
684685
details:

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1137,7 +1137,8 @@
11371137
"type": "string"
11381138
},
11391139
"message": {
1140-
"type": "string"
1140+
"type": "string",
1141+
"x-ms-primary-error-message": true
11411142
},
11421143
"target": {
11431144
"type": "string",

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Basic.OpenApi.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ components:
748748
type: string
749749
message:
750750
type: string
751+
x-ms-primary-error-message: true
751752
target:
752753
type: string
753754
nullable: true

0 commit comments

Comments
 (0)