Skip to content

Commit

Permalink
Merge pull request #2784 from martincostello/handle-stream-and-pipere…
Browse files Browse the repository at this point in the history
…ader

Handle Stream and PipeReader content types correctly
  • Loading branch information
domaindrivendev authored Apr 24, 2024
2 parents 67f4d8a + 044a995 commit a5dce4f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
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 @@ -124,7 +124,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 @@ -164,7 +164,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 @@ -200,7 +200,6 @@ private bool IsBaseTypeWithKnownTypesDefined(DataContract dataContract, out IEnu
}

private OpenApiSchema GeneratePolymorphicSchema(
DataContract dataContract,
SchemaRepository schemaRepository,
IEnumerable<DataContract> knownTypesDataContracts)
{
Expand All @@ -212,14 +211,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 @@ -25,7 +25,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

0 comments on commit a5dce4f

Please sign in to comment.