Skip to content

Commit fba1c33

Browse files
Fix error ranges for OData actions (#229)
* Fix typo on test class name * Add error ranges for OData actions when ErrorResponsesAsDefault is set to false * Add tests for OData action error ranges when ErrorResponsesAsDefault is set * Add release notes
1 parent 6ab40ab commit fba1c33

File tree

4 files changed

+45
-3
lines changed

4 files changed

+45
-3
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,15 @@ public static OpenApiResponses CreateResponses(this ODataContext context, IEdmOp
208208
responses.Add(Constants.StatusCode200, response);
209209
}
210210

211-
// Both action & function have the default response.
212-
responses.Add(Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse());
211+
if (context.Settings.ErrorResponsesAsDefault)
212+
{
213+
responses.Add(Constants.StatusCodeDefault, Constants.StatusCodeDefault.GetResponse());
214+
}
215+
else
216+
{
217+
responses.Add(Constants.StatusCodeClass4XX, Constants.StatusCodeClass4XX.GetResponse());
218+
responses.Add(Constants.StatusCodeClass5XX, Constants.StatusCodeClass5XX.GetResponse());
219+
}
213220

214221
return responses;
215222
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
- Adds list of all derived types for discriminator mapping #219
2525
- Fixes reading restriction annotations for entity types defining navigation properties #220
2626
- Enables configuring appending bound operations on derived types #221
27+
- Add error ranges for OData actions when ErrorResponsesAsDefault is set to false #218
2728
</PackageReleaseNotes>
2829
<AssemblyName>Microsoft.OpenApi.OData.Reader</AssemblyName>
2930
<AssemblyOriginatorKeyFile>..\..\tool\Microsoft.OpenApi.OData.snk</AssemblyOriginatorKeyFile>

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiErrorSchemaGeneraratorTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
using Xunit;
1010

1111
namespace Microsoft.OpenApi.OData.Tests;
12-
public class OpenApiErrorSchemaGeneraratorTests
12+
public class OpenApiErrorSchemaGeneratorTests
1313
{
1414
[Fact]
1515
public void AddsEmptyInnerErrorWhenNoComplexTypeIsProvided()

test/Microsoft.OpenAPI.OData.Reader.Tests/Generator/OpenApiResponseGeneratorTests.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,5 +306,39 @@ public void CreateResponseForEdmActionReturnCorrectResponses(string actionName,
306306
Assert.Equal(2, responses.Count);
307307
Assert.Equal(new string[] { responseCode, "default" }, responses.Select(r => r.Key));
308308
}
309+
310+
[Theory]
311+
[InlineData("assignLicense", false, "200")]
312+
[InlineData("activateService", false, "204")]
313+
[InlineData("verifySignature", true, "200")]
314+
public void CreateResponseForEdmActionWhenErrorResponsesAsDefaultIsSet(string actionName, bool errorAsDefault, string responseCode)
315+
{
316+
// Arrange
317+
IEdmModel model = EdmModelHelper.GraphBetaModel;
318+
var settings = new OpenApiConvertSettings
319+
{
320+
ErrorResponsesAsDefault = errorAsDefault,
321+
};
322+
ODataContext context = new ODataContext(model, settings);
323+
324+
// Act
325+
OpenApiResponses responses;
326+
IEdmOperation operation = model.SchemaElements.OfType<IEdmOperation>().First(o => o.Name == actionName);
327+
Assert.NotNull(operation); // guard
328+
ODataPath path = new(new ODataOperationSegment(operation));
329+
responses = context.CreateResponses(operation, path);
330+
331+
// Assert
332+
Assert.NotNull(responses);
333+
Assert.NotEmpty(responses);
334+
if (errorAsDefault)
335+
{
336+
Assert.Equal(new string[] { responseCode, "default" }, responses.Select(r => r.Key));
337+
}
338+
else
339+
{
340+
Assert.Equal(new string[] { responseCode, "4XX", "5XX" }, responses.Select(r => r.Key));
341+
}
342+
}
309343
}
310344
}

0 commit comments

Comments
 (0)