Skip to content

Commit 27bf1b6

Browse files
authored
[Feature] Use MediaType annotation to set the content type of operations with Edm.Stream return types (#375)
* Add new constant for MediaType * Use MediaType annotation to set the content types of ops of Edm.Stream return types * Update unit test and integration file
1 parent 72bba88 commit 27bf1b6

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using Microsoft.OpenApi.Models;
1010
using Microsoft.OpenApi.OData.Common;
1111
using Microsoft.OpenApi.OData.Edm;
12+
using Microsoft.OpenApi.OData.Vocabulary.Core;
1213

1314
namespace Microsoft.OpenApi.OData.Generator
1415
{
@@ -271,8 +272,13 @@ public static OpenApiResponse CreateOperationResponse(this ODataContext context,
271272
string mediaType = Constants.ApplicationJsonMediaType;
272273
if (operation.ReturnType.AsPrimitive()?.PrimitiveKind() == EdmPrimitiveTypeKind.Stream)
273274
{
274-
// Responses of types Edm.Stream should be application/octet-stream
275-
mediaType = Constants.ApplicationOctetStreamMediaType;
275+
mediaType = context.Model.GetString(operation, CoreConstants.MediaType);
276+
277+
if (string.IsNullOrEmpty(mediaType))
278+
{
279+
// Use default if MediaType annotation is not specified
280+
mediaType = Constants.ApplicationOctetStreamMediaType;
281+
}
276282
}
277283

278284
OpenApiResponse response = new()

src/Microsoft.OpenApi.OData.Reader/Vocabulary/Core/CoreConstants.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ internal static class CoreConstants
2525
/// </summary>
2626
public const string AcceptableMediaTypes = "Org.OData.Core.V1.AcceptableMediaTypes";
2727

28+
/// <summary>
29+
/// Org.OData.Core.V1.MediaType
30+
/// </summary>
31+
public const string MediaType = "Org.OData.Core.V1.MediaType";
32+
2833
/// <summary>
2934
/// Org.OData.Core.V1.Computed
3035
/// </summary>

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

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,40 @@ public void CreateResponseForEdmFunctionReturnCorrectResponses(bool isFunctionIm
231231
public void CreateResponseForEdmFunctionOfStreamReturnTypeReturnsCorrectResponse()
232232
{
233233
// Arrange
234-
string operationName = "getMailboxUsageStorage";
234+
string operationName1 = "getMailboxUsageStorage";
235+
string operationName2 = "incidentReport";
235236
IEdmModel model = EdmModelHelper.GraphBetaModel;
236237
ODataContext context = new(model);
237238

238239
// Act
239-
OpenApiResponses responses;
240-
IEdmOperation operation = model.SchemaElements.OfType<IEdmOperation>().First(o => o.Name == operationName);
241-
Assert.NotNull(operation); // guard
242-
ODataPath path = new(new ODataOperationSegment(operation));
243-
responses = context.CreateResponses(operation);
240+
IEdmOperation operation1 = model.SchemaElements.OfType<IEdmOperation>().First(o => o.Name == operationName1);
241+
IEdmOperation operation2 = model.SchemaElements.OfType<IEdmOperation>().First(o => o.Name == operationName2);
242+
Assert.NotNull(operation1);
243+
Assert.NotNull(operation2);
244+
ODataPath path1 = new(new ODataOperationSegment(operation1));
245+
ODataPath path2 = new(new ODataOperationSegment(operation2));
246+
OpenApiResponses responses1 = context.CreateResponses(operation1);
247+
OpenApiResponses responses2 = context.CreateResponses(operation2);
248+
249+
// Assert for operation1 --> getMailboxUsageStorage
250+
Assert.NotNull(responses1);
251+
Assert.NotEmpty(responses1);
252+
Assert.Equal(2, responses1.Count);
253+
Assert.Equal(new string[] { "200", "default" }, responses1.Select(r => r.Key));
254+
255+
OpenApiResponse response = responses1["200"];
256+
Assert.NotNull(response.Content);
257+
Assert.Equal("application/octet-stream", response.Content.First().Key);
244258

245-
// Assert
246-
Assert.NotNull(responses);
247-
Assert.NotEmpty(responses);
248-
Assert.Equal(2, responses.Count);
249-
Assert.Equal(new string[] { "200", "default" }, responses.Select(r => r.Key));
259+
// Assert for operation2 --> incidentReport
260+
Assert.NotNull(responses2);
261+
Assert.NotEmpty(responses2);
262+
Assert.Equal(2, responses2.Count);
263+
Assert.Equal(new string[] { "200", "default" }, responses2.Select(r => r.Key));
250264

251-
OpenApiResponse response = responses["200"];
265+
response = responses2["200"];
252266
Assert.NotNull(response.Content);
253-
OpenApiMediaType mediaType = response.Content["application/octet-stream"];
267+
Assert.Equal("text/html", response.Content.First().Key);
254268
}
255269

256270
[Theory]

test/Microsoft.OpenAPI.OData.Reader.Tests/Resources/Graph.Beta.OData.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102259,6 +102259,7 @@
102259102259
<Function Name="incidentReport" IsBound="true">
102260102260
<Parameter Name="bindingParameter" Type="graph.serviceHealthIssue" />
102261102261
<ReturnType Type="Edm.Stream" />
102262+
<Annotation Term="Org.OData.Core.V1.MediaType" String="text/html" />
102262102263
</Function>
102263102264
<Function Name="getRecentNotebooks" IsBound="true">
102264102265
<Parameter Name="bindingParameter" Type="Collection(graph.notebook)" />

0 commit comments

Comments
 (0)