Skip to content

Commit

Permalink
port domaindrivendev#2784 Handle Stream and PipeReader content types …
Browse files Browse the repository at this point in the history
…correctly
  • Loading branch information
Havunen committed May 5, 2024
1 parent 1247e24 commit 378e2ce
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,14 @@ private OpenApiSchema GeneratePolymorphicSchema(
};
}

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

private OpenApiSchema GenerateConcreteSchema(
DataContract dataContract,
SchemaRepository schemaRepository,
Expand All @@ -246,7 +254,7 @@ bool isNullable
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 @@ -21,6 +21,19 @@ namespace DotSwashbuckle.AspNetCore.Newtonsoft.Test
{
public class NewtonsoftSchemaGeneratorTests
{
[Theory]
[InlineData(typeof(IFormFile))]
[InlineData(typeof(FileResult))]
[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());

Assert.Equal("string", schema.Type);
Assert.Equal("binary", schema.Format);
}

[Fact]
public void GenerateSchema_SetsUniqueItems_IfEnumerableTypeIsReadOnlySet()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace DotSwashbuckle.AspNetCore.SwaggerGen.Test
{
public class JsonSerializerSchemaGeneratorTests
{

[Fact]
public void GenerateSchema_SetsUniqueItems_IfEnumerableTypeIsReadOnlySet()
{
Expand All @@ -35,7 +36,9 @@ public void GenerateSchema_SetsUniqueItems_IfEnumerableTypeIsReadOnlySet()
[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 378e2ce

Please sign in to comment.