Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,34 @@ namespace Microsoft.OpenApi.MicrosoftExtensions;
/// </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 />
/// <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 is null) throw new ArgumentNullException(nameof(writer));
writer.WriteValue(IsPrimaryErrorMessage);
if (writer is 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>

/// <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
public static OpenApiPrimaryErrorMessageExtension Parse(IOpenApiAny source)
{
if (source is not OpenApiBoolean rawObject) throw new ArgumentOutOfRangeException(nameof(source));
return new OpenApiPrimaryErrorMessageExtension
{
IsPrimaryErrorMessage = rawObject.Value
};
}
IsPrimaryErrorMessage = rawObject.Value
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,47 @@ 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);
}
[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);
}
}
Loading