Skip to content

Commit 7dbebe9

Browse files
authored
Add support for FileContentResult in OpenAPI schemas (#63504)
Updated OpenApiSchemaService to handle FileContentResult, ensuring schemas are generated as `string` with `binary` format. Introduced a new test in OpenApiSchemaServiceTests to validate OpenAPI response handling for FileContentResult, ensuring correct schema and content type representation.
1 parent 6061280 commit 7dbebe9

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,9 @@ internal sealed class OpenApiSchemaService(
5858
TransformSchemaNode = (context, schema) =>
5959
{
6060
var type = context.TypeInfo.Type;
61-
// Fix up schemas generated for IFormFile, IFormFileCollection, Stream, and PipeReader
61+
// Fix up schemas generated for IFormFile, IFormFileCollection, Stream, PipeReader and FileContentResult
6262
// that appear as properties within complex types.
63-
if (type == typeof(IFormFile) || type == typeof(Stream) || type == typeof(PipeReader))
63+
if (type == typeof(IFormFile) || type == typeof(Stream) || type == typeof(PipeReader) || type == typeof(Mvc.FileContentResult))
6464
{
6565
schema = new JsonObject
6666
{

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Extensions/JsonTypeInfoExtensionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public class Baz
6363
[typeof(JsonPatchDocument<Todo>), "JsonPatchDocument"],
6464
[typeof(Stream), "Stream"],
6565
[typeof(PipeReader), "PipeReader"],
66+
[typeof(FileContentResult), "FileContentResult"],
6667
[typeof(Results<Ok<TodoWithDueDate>, Ok<Todo>>), "ResultsOfOkOfTodoWithDueDateAndOkOfTodo"],
6768
[typeof(Ok<Todo>), "OkOfTodo"],
6869
[typeof(NotFound<TodoWithDueDate>), "NotFoundOfTodoWithDueDate"],

src/OpenApi/test/Microsoft.AspNetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ResponseSchemas.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System.ComponentModel;
55
using System.Net.Http;
6+
using System.Net.Mime;
67
using System.Text.Json.Nodes;
78
using Microsoft.AspNetCore.Builder;
89
using Microsoft.AspNetCore.Http;
@@ -1031,6 +1032,29 @@ await VerifyOpenApiDocument(actionDescriptor, document =>
10311032
});
10321033
}
10331034

1035+
[Fact]
1036+
public async Task GetOpenApiResponse_HandlesFileContentResultTypeResponse()
1037+
{
1038+
// Arrange
1039+
var builder = CreateBuilder();
1040+
1041+
// Act
1042+
builder.MapPost("/filecontentresult", () => { return new FileContentResult([], MediaTypeNames.Application.Octet); })
1043+
.Produces<FileContentResult>(contentType: MediaTypeNames.Application.Octet);
1044+
1045+
// Assert
1046+
await VerifyOpenApiDocument(builder, document =>
1047+
{
1048+
var operation = document.Paths["/filecontentresult"].Operations[HttpMethod.Post];
1049+
var responses = Assert.Single(operation.Responses);
1050+
var response = responses.Value;
1051+
Assert.True(response.Content.TryGetValue("application/octet-stream", out var mediaType));
1052+
var schema = mediaType.Schema;
1053+
Assert.Equal(JsonSchemaType.String, schema.Type);
1054+
Assert.Equal("binary", schema.Format);
1055+
});
1056+
}
1057+
10341058
[ApiController]
10351059
[Produces("application/json")]
10361060
public class TestController

0 commit comments

Comments
 (0)