Skip to content

Fix OpenAPI schema generation for FileContentResult to use binary format #63180

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
29 changes: 26 additions & 3 deletions src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
using System.Text.Json.Serialization.Metadata;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Http.Json;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.ApiExplorer;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -27,7 +28,7 @@
/// </summary>
internal sealed class OpenApiSchemaService(
[ServiceKey] string documentName,
IOptions<JsonOptions> jsonOptions,

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux x64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl ARM64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux Musl x64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: Ubuntu x64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr (Tests: macOS)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: Ubuntu x64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: Linux ARM64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS arm64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Build: macOS x64)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci (Build Test: macOS)

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-quarantined-pr

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'

Check failure on line 31 in src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs

View check run for this annotation

Azure Pipelines / aspnetcore-ci

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

src/OpenApi/src/Services/Schemas/OpenApiSchemaService.cs(31,14): error CS0104: (NETCORE_ENGINEERING_TELEMETRY=Build) 'JsonOptions' is an ambiguous reference between 'Microsoft.AspNetCore.Http.Json.JsonOptions' and 'Microsoft.AspNetCore.Mvc.JsonOptions'
IOptionsMonitor<OpenApiOptions> optionsMonitor)
{
private readonly ConcurrentDictionary<Type, string?> _schemaIdCache = new();
Expand Down Expand Up @@ -58,15 +59,15 @@
TransformSchemaNode = (context, schema) =>
{
var type = context.TypeInfo.Type;
// Fix up schemas generated for IFormFile, IFormFileCollection, Stream, and PipeReader
// Fix up schemas generated for IFormFile, IFormFileCollection, Stream, PipeReader, and FileContentResult
// that appear as properties within complex types.
if (type == typeof(IFormFile) || type == typeof(Stream) || type == typeof(PipeReader))
if (type == typeof(IFormFile) || type == typeof(Stream) || type == typeof(PipeReader) || type == typeof(FileContentResult))
{
schema = new JsonObject
{
[OpenApiSchemaKeywords.TypeKeyword] = "string",
[OpenApiSchemaKeywords.FormatKeyword] = "binary",
[OpenApiConstants.SchemaId] = "IFormFile"
[OpenApiConstants.SchemaId] = GetBinarySchemaId(type)
};
}
else if (type == typeof(IFormFileCollection))
Expand Down Expand Up @@ -121,6 +122,28 @@
}
};

private static string GetBinarySchemaId(Type type)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has changed the meaning of the code. It was returning the same value for all types before, now they're all different. I doubt this is correct, but even if it is it will cause tests to fail so will need further changes.

I suggest you follow the [guide to build and test the code](https://github.com/dotnet/aspnetcore/blob/main/docs/BuildFromSource.md aspnetcore/docs/BuildFromSource.md) and change the code using an IDE and run the relevant tests so you can check none get broken, and also so you can add a test for this change.

{
if (type == typeof(IFormFile))
{
return "IFormFile";
}
else if (type == typeof(Stream))
{
return "Stream";
}
else if (type == typeof(PipeReader))
{
return "PipeReader";
}
else if (type == typeof(FileContentResult))
{
return "FileContentResult";
}

return type.Name;
}

private static JsonObject CreateSchemaForJsonPatch()
{
var addReplaceTest = new JsonObject()
Expand Down
Loading