Skip to content

Commit

Permalink
Missing Encoding and RequiredProperties when IformFile withOpenApi (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
jgarciadelanoceda authored Jul 10, 2024
1 parent 43b8521 commit 1d99754
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
{
foreach (var content in requestContentTypes)
{
content.Encoding = new Dictionary<string, OpenApiEncoding>();
var requestParameters = apiDescription.ParameterDescriptions.Where(desc => desc.IsFromBody() || desc.IsFromForm());
var countOfParameters = requestParameters.Count();
if (countOfParameters > 0)
Expand All @@ -431,7 +432,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
requestParameter.ModelMetadata.ModelType,
schemaRepository,
requestParameter.PropertyInfo(),
requestParameter.ParameterInfo()));
requestParameter.ParameterInfo()), content);
}
else
{
Expand All @@ -442,25 +443,30 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
s.ModelMetadata.ModelType,
schemaRepository,
s.PropertyInfo(),
s.ParameterInfo())))
s.ParameterInfo()), content))
.ToList()
};
}
}

static OpenApiSchema GenerateSchemaIncludingFormFile(ApiParameterDescription apiParameterDescription, OpenApiSchema generatedSchema)
static OpenApiSchema GenerateSchemaIncludingFormFile(ApiParameterDescription apiParameterDescription, OpenApiSchema generatedSchema, OpenApiMediaType mediaType)
{
if (generatedSchema.Reference is null
&& ((generatedSchema.Type == "string" && generatedSchema.Format == "binary") || (generatedSchema.Type == "array" && generatedSchema.Items.Format == "binary")))
if (generatedSchema.Reference is null && apiParameterDescription.IsFromForm())
{
return new OpenApiSchema()
mediaType.Encoding.Add(apiParameterDescription.Name, new OpenApiEncoding { Style = ParameterStyle.Form });
if ((generatedSchema.Type == "string" && generatedSchema.Format == "binary")
|| (generatedSchema.Type == "array" && generatedSchema.Items.Type == "string" && generatedSchema.Items.Format == "binary"))
{
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>()
return new OpenApiSchema()
{
[apiParameterDescription.Name] = generatedSchema
}
};
Type = "object",
Properties = new Dictionary<string, OpenApiSchema>()
{
[apiParameterDescription.Name] = generatedSchema
},
Required = apiParameterDescription.IsRequired ? new SortedSet<string>() { apiParameterDescription.Name } : null
};
}
}
return generatedSchema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,21 @@
"content": {
"multipart/form-data": {
"schema": {
"required": [
"file"
],
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
},
"encoding": {
"file": {
"style": "form"
}
}
}
},
Expand Down Expand Up @@ -163,6 +171,9 @@
"content": {
"multipart/form-data": {
"schema": {
"required": [
"collection"
],
"type": "object",
"properties": {
"collection": {
Expand All @@ -173,6 +184,11 @@
}
}
}
},
"encoding": {
"collection": {
"style": "form"
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,13 +128,21 @@
"content": {
"multipart/form-data": {
"schema": {
"required": [
"file"
],
"type": "object",
"properties": {
"file": {
"type": "string",
"format": "binary"
}
}
},
"encoding": {
"file": {
"style": "form"
}
}
}
},
Expand Down Expand Up @@ -163,6 +171,9 @@
"content": {
"multipart/form-data": {
"schema": {
"required": [
"collection"
],
"type": "object",
"properties": {
"collection": {
Expand All @@ -173,6 +184,11 @@
}
}
}
},
"encoding": {
"collection": {
"style": "form"
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,9 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIF
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);
Assert.NotNull(content.Value.Encoding);
Assert.NotNull(content.Value.Encoding["param"]);
Assert.Equal(ParameterStyle.Form, content.Value.Encoding["param"].Style);
}

[Fact]
Expand Down Expand Up @@ -2216,6 +2219,9 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIF
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);
Assert.NotNull(content.Value.Encoding);
Assert.NotNull(content.Value.Encoding["param"]);
Assert.Equal(ParameterStyle.Form, content.Value.Encoding["param"].Style);
}

private static SwaggerGenerator Subject(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
Nullable: false,
Deprecated: false,
UnresolvedReference: false
},
Encoding: {
param: {
Style: Form
}
}
}
}
Expand All @@ -44,5 +49,5 @@
}
},
Components: {},
HashCode: 3B411279DDA5AD71B248D9E65E29E2545971131294B8FB032C0EC91640277615B8D600D78530054A7DA3754611589518B2C9773BB48A813B9951B46DE633743A
HashCode: 7D034A2620C1D85B3AC60194DFA59693F727DE4704B2D02E124FDA37F843426C258EF2BEB84E6B8E8D315E23A4BCBE1F423B479E6CDF8AFFB8514D49B9A3CC9E
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
Nullable: false,
Deprecated: false,
UnresolvedReference: false
},
Encoding: {
param: {
Style: Form
}
}
}
}
Expand All @@ -53,5 +58,5 @@
}
},
Components: {},
HashCode: 64312D7E174EFA8B92E7869E39FD7367BB6A464F0C5E8A72D0B010AE96AAE157EE91BC02BEDFEC2B01CCC6BAA4E7FA79156782C09D435428AE8F732D3C9EB1B9
HashCode: 073D8B8E67D5100DD8AF06EC69A3C02B8DBF29E46621ED6EB590DEA519F2C8941398F6B95292D891CC4E18C2F4D5D38A8F904545CFFC219E4FF4613AD605E5A5
}

0 comments on commit 1d99754

Please sign in to comment.