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

Fix error ranges for OData actions #229

Merged
merged 4 commits into from
Jun 6, 2022
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 @@ -208,8 +208,15 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp
responses.Add(Constants.StatusCode200, response);
}

// Both action & function have the default response.
responses.Add(Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse());
if (context.Settings.ErrorResponsesAsDefault)
{
responses.Add(Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse());
}
else
{
responses.Add(Constants.StatusCodeClass4XX, Constants.StatusCodeClass4XX.GetResponse());
responses.Add(Constants.StatusCodeClass5XX, Constants.StatusCodeClass5XX.GetResponse());
}

return responses;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
- Adds list of all derived types for discriminator mapping #219
- Fixes reading restriction annotations for entity types defining navigation properties #220
- Enables configuring appending bound operations on derived types #221
- Add error ranges for OData actions when ErrorResponsesAsDefault is set to false #218
</PackageReleaseNotes>
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
using Xunit;

namespace Microsoft.OpenApi.OData.Tests;
public class OpenApiErrorSchemaGeneraratorTests
public class OpenApiErrorSchemaGeneratorTests
{
[Fact]
public void AddsEmptyInnerErrorWhenNoComplexTypeIsProvided()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,39 @@ public void CreateResponseForEdmActionReturnCorrectResponses(string actionName,
Assert.Equal(2, responses.Count);
Assert.Equal(new string[] { responseCode, "default" }, responses.Select(r => r.Key));
}

[Theory]
[InlineData("assignLicense", false, "200")]
[InlineData("activateService", false, "204")]
[InlineData("verifySignature", true, "200")]
public void CreateResponseForEdmActionWhenErrorResponsesAsDefaultIsSet(string actionName, bool errorAsDefault, string responseCode)
{
// Arrange
IEdmModel model = EdmModelHelper.GraphBetaModel;
var settings = new OpenApiConvertSettings
{
ErrorResponsesAsDefault = errorAsDefault,
};
ODataContext context = new ODataContext(model, settings);

// Act
OpenApiResponses responses;
IEdmOperation operation = model.SchemaElements.OfType<IEdmOperation>().First(o => o.Name == actionName);
Assert.NotNull(operation); // guard
ODataPath path = new(new ODataOperationSegment(operation));
responses = context.CreateResponses(operation, path);

// Assert
Assert.NotNull(responses);
Assert.NotEmpty(responses);
if (errorAsDefault)
{
Assert.Equal(new string[] { responseCode, "default" }, responses.Select(r => r.Key));
}
else
{
Assert.Equal(new string[] { responseCode, "4XX", "5XX" }, responses.Select(r => r.Key));
}
}
}
}