Skip to content
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

- adds primary message to error generation #419

Merged
merged 2 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
{
"recommendations": [
"formulahendry.dotnet-test-explorer",
"ms-dotnettools.csharp",
"editorconfig.editorconfig"
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<string, IOpenApiExtension>
{ { OpenApiPrimaryErrorMessageExtension.Name, new OpenApiPrimaryErrorMessageExtension { IsPrimaryErrorMessage = true } } } }
},
{
"target", new OpenApiSchema { Type = "string", Nullable = true }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<TargetFrameworks>netstandard2.0</TargetFrameworks>
<PackageId>Microsoft.OpenApi.OData</PackageId>
<SignAssembly>true</SignAssembly>
<Version>1.5.0-preview3</Version>
<Version>1.5.0-preview4</Version>
<Description>This package contains the codes you need to convert OData CSDL to Open API Document of Model.</Description>
<Copyright>© Microsoft Corporation. All rights reserved.</Copyright>
<PackageTags>Microsoft OpenApi OData EDM</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Extension element for OpenAPI to add tag the primary error message to use on error types. x-ms-primary-error-message
/// </summary>
public class OpenApiPrimaryErrorMessageExtension : IOpenApiExtension
{
/// <summary>
/// Name of the extension as used in the description.
/// </summary>
public static string Name => "x-ms-primary-error-message";
/// <inheritdoc />
public void Write(IOpenApiWriter writer, OpenApiSpecVersion specVersion)
{
if(writer == null)
throw new ArgumentNullException(nameof(writer));
writer.WriteValue(IsPrimaryErrorMessage);
}
/// <summary>
/// Whether this property is the primary error message to use on error types.
/// </summary>
public bool IsPrimaryErrorMessage { get; set; }
/// <summary>
/// Parses the <see cref="IOpenApiAny"/> to <see cref="OpenApiPrimaryErrorMessageExtension"/>.
/// </summary>
/// <param name="source">The source object.</param>
/// <returns>The <see cref="OpenApiPrimaryErrorMessageExtension"/>.</returns>
public static OpenApiPrimaryErrorMessageExtension Parse(IOpenApiAny source)
{
if (source is not OpenApiBoolean rawObject) throw new ArgumentOutOfRangeException(nameof(source));
return new OpenApiPrimaryErrorMessageExtension() {
IsPrimaryErrorMessage = rawObject.Value
};
}
}
7 changes: 7 additions & 0 deletions src/Microsoft.OpenApi.OData.Reader/PublicAPI.Unshipped.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1034,7 +1034,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,7 @@ definitions:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
details:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ components:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
nullable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ definitions:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
details:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ components:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
nullable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5771,7 +5771,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4201,6 +4201,7 @@ definitions:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
details:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6505,7 +6505,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4689,6 +4689,7 @@ components:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
nullable: true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28144,7 +28144,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19773,6 +19773,7 @@ definitions:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
details:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31534,7 +31534,8 @@
"type": "string"
},
"message": {
"type": "string"
"type": "string",
"x-ms-primary-error-message": true
},
"target": {
"type": "string",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21959,6 +21959,7 @@ components:
type: string
message:
type: string
x-ms-primary-error-message: true
target:
type: string
nullable: true
Expand Down