Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -427,26 +427,45 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
if (countOfParameters == 1)
{
var requestParameter = requestParameters.First();
content.Schema = GenerateSchema(
content.Schema = GenerateSchemaIncludingFormFile(requestParameter, GenerateSchema(
requestParameter.ModelMetadata.ModelType,
schemaRepository,
requestParameter.PropertyInfo(),
requestParameter.ParameterInfo());
requestParameter.ParameterInfo()));
}
else
{
content.Schema = new OpenApiSchema()
{
AllOf = requestParameters.Select(s =>
GenerateSchema(
GenerateSchemaIncludingFormFile(s, GenerateSchema(
s.ModelMetadata.ModelType,
schemaRepository,
s.PropertyInfo(),
s.ParameterInfo()))
s.ParameterInfo())))
.ToList()
};
}
}
static OpenApiSchema GenerateSchemaIncludingFormFile(ApiParameterDescription apiParameterDescription, OpenApiSchema generatedSchema)
{
if (generatedSchema.Reference is null
&& ((generatedSchema.Type == "string" && generatedSchema.Format == "binary") || (generatedSchema.Type == "array" && generatedSchema.Items.Format == "binary")))
{
return new OpenApiSchema()
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>()
{
{
apiParameterDescription.Name,
generatedSchema
}
}
};
}
return generatedSchema;
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,87 @@
},
"responses": {
"200": {
"description": "OK"
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/WithOpenApi/IFromFile": {
"post": {
"tags": [
"WithOpenApi"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/WithOpenApi/IFromFileCollection": {
"post": {
"tags": [
"WithOpenApi"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"collection": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,87 @@
},
"responses": {
"200": {
"description": "OK"
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/WithOpenApi/IFromFile": {
"post": {
"tags": [
"WithOpenApi"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
},
"/WithOpenApi/IFromFileCollection": {
"post": {
"tags": [
"WithOpenApi"
],
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"type": "object",
"properties": {
"collection": {
"type": "array",
"items": {
"type": "string",
"format": "binary"
}
}
}
}
}
},
"required": true
},
"responses": {
"200": {
"description": "OK",
"content": {
"text/plain": {
"schema": {
"type": "string"
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2098,6 +2098,126 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithSe
Assert.Equal("TypeWithDefaultAttributeOnEnum", content.Value.Schema.AllOf[1].Reference.Id);
}

[Fact]
public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIFormFile()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithConsumesAttribute));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata =
[
new OpenApiOperation
{
OperationId = "OperationIdSetInMetadata",
RequestBody = new()
{
Content = new Dictionary<string, OpenApiMediaType>()
{
["application/someMediaType"] = new()
}
}
}
],
RouteValues = new Dictionary<string, string>
{
["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty)
}
};
var subject = Subject(
apiDescriptions:
[
ApiDescriptionFactory.Create(
actionDescriptor,
methodInfo,
groupName: "v1",
httpMethod: "POST",
relativePath: "resource",
parameterDescriptions:
[
new ApiParameterDescription()
{
Name = "param",
Source = BindingSource.Form,
ModelMetadata = ModelMetadataFactory.CreateForType(typeof(IFormFile))
}
]),
]
);

var document = subject.GetSwagger("v1");

Assert.Equal("OperationIdSetInMetadata", document.Paths["/resource"].Operations[OperationType.Post].OperationId);
var content = Assert.Single(document.Paths["/resource"].Operations[OperationType.Post].RequestBody.Content);
Assert.Equal("application/someMediaType", content.Key);
Assert.NotNull(content.Value.Schema);
Assert.Equal("object", content.Value.Schema.Type);
Assert.NotEmpty(content.Value.Schema.Properties);
Assert.NotNull(content.Value.Schema.Properties["param"]);
Assert.Equal("string", content.Value.Schema.Properties["param"].Type);
Assert.Equal("binary", content.Value.Schema.Properties["param"].Format);
}

[Fact]
public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIFormFileCollection()
{
var methodInfo = typeof(FakeController).GetMethod(nameof(FakeController.ActionWithConsumesAttribute));
var actionDescriptor = new ActionDescriptor
{
EndpointMetadata =
[
new OpenApiOperation
{
OperationId = "OperationIdSetInMetadata",
RequestBody = new()
{
Content = new Dictionary<string, OpenApiMediaType>()
{
["application/someMediaType"] = new()
}
}
}
],
RouteValues = new Dictionary<string, string>
{
["controller"] = methodInfo.DeclaringType.Name.Replace("Controller", string.Empty)
}
};
var subject = Subject(
apiDescriptions:
[
ApiDescriptionFactory.Create(
actionDescriptor,
methodInfo,
groupName: "v1",
httpMethod: "POST",
relativePath: "resource",
parameterDescriptions:
[
new ApiParameterDescription()
{
Name = "param",
Source = BindingSource.Form,
ModelMetadata = ModelMetadataFactory.CreateForType(typeof(IFormFileCollection))
}
]),
]
);

var document = subject.GetSwagger("v1");

Assert.Equal("OperationIdSetInMetadata", document.Paths["/resource"].Operations[OperationType.Post].OperationId);
var content = Assert.Single(document.Paths["/resource"].Operations[OperationType.Post].RequestBody.Content);
Assert.Equal("application/someMediaType", content.Key);
Assert.NotNull(content.Value.Schema);
Assert.Equal("object", content.Value.Schema.Type);
Assert.NotEmpty(content.Value.Schema.Properties);
Assert.NotNull(content.Value.Schema.Properties["param"]);
Assert.Equal("array", content.Value.Schema.Properties["param"].Type);
Assert.NotNull(content.Value.Schema.Properties["param"].Items);
Assert.Equal("string", content.Value.Schema.Properties["param"].Items.Type);
Assert.Equal("binary", content.Value.Schema.Properties["param"].Items.Format);
}

private static SwaggerGenerator Subject(
IEnumerable<ApiDescription> apiDescriptions,
SwaggerGeneratorOptions options = null,
Expand Down
Loading