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

Handle Stream and PipeReader content types correctly #2784

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 @@ -50,7 +50,7 @@ private OpenApiSchema GenerateSchemaForMember(
var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
? GeneratePolymorphicSchema(schemaRepository, knownTypesDataContracts)
: GenerateConcreteSchema(dataContract, schemaRepository);

if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema.Reference != null)
Expand Down Expand Up @@ -112,7 +112,7 @@ private OpenApiSchema GenerateSchemaForParameter(
var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
? GeneratePolymorphicSchema(schemaRepository, knownTypesDataContracts)
: GenerateConcreteSchema(dataContract, schemaRepository);

if (_generatorOptions.UseAllOfToExtendReferenceSchemas && schema.Reference != null)
Expand Down Expand Up @@ -152,7 +152,7 @@ private OpenApiSchema GenerateSchemaForType(Type modelType, SchemaRepository sch
var dataContract = GetDataContractFor(modelType);

var schema = _generatorOptions.UseOneOfForPolymorphism && IsBaseTypeWithKnownTypesDefined(dataContract, out var knownTypesDataContracts)
? GeneratePolymorphicSchema(dataContract, schemaRepository, knownTypesDataContracts)
? GeneratePolymorphicSchema(schemaRepository, knownTypesDataContracts)
: GenerateConcreteSchema(dataContract, schemaRepository);

if (schema.Reference == null)
Expand Down Expand Up @@ -188,7 +188,6 @@ private bool IsBaseTypeWithKnownTypesDefined(DataContract dataContract, out IEnu
}

private OpenApiSchema GeneratePolymorphicSchema(
DataContract dataContract,
SchemaRepository schemaRepository,
IEnumerable<DataContract> knownTypesDataContracts)
{
Expand All @@ -200,14 +199,24 @@ private OpenApiSchema GeneratePolymorphicSchema(
};
}

private static readonly Type[] BinaryStringTypes = new[]
{
typeof(IFormFile),
typeof(FileResult),
typeof(System.IO.Stream),
#if NETCOREAPP3_0_OR_GREATER
typeof(System.IO.Pipelines.PipeReader),
#endif
};

private OpenApiSchema GenerateConcreteSchema(DataContract dataContract, SchemaRepository schemaRepository)
{
if (TryGetCustomTypeMapping(dataContract.UnderlyingType, out Func<OpenApiSchema> customSchemaFactory))
{
return customSchemaFactory();
}

if (dataContract.UnderlyingType.IsAssignableToOneOf(typeof(IFormFile), typeof(FileResult)))
if (dataContract.UnderlyingType.IsAssignableToOneOf(BinaryStringTypes))
{
return new OpenApiSchema { Type = "string", Format = "binary" };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public class NewtonsoftSchemaGeneratorTests
[Theory]
[InlineData(typeof(IFormFile))]
[InlineData(typeof(FileResult))]
public void GenerateSchema_GeneratesFileSchema_IfFormFileOrFileResultType(Type type)
[InlineData(typeof(System.IO.Stream))]
public void GenerateSchema_GeneratesFileSchema_BinaryStringResultType(Type type)
{
var schema = Subject().GenerateSchema(type, new SchemaRepository());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ public class JsonSerializerSchemaGeneratorTests
[Theory]
[InlineData(typeof(IFormFile))]
[InlineData(typeof(FileResult))]
public void GenerateSchema_GeneratesFileSchema_IfFormFileOrFileResultType(Type type)
[InlineData(typeof(System.IO.Stream))]
[InlineData(typeof(System.IO.Pipelines.PipeReader))]
public void GenerateSchema_GeneratesFileSchema_BinaryStringResultType(Type type)
{
var schema = Subject().GenerateSchema(type, new SchemaRepository());

Expand Down