Skip to content

Commit 1d99754

Browse files
Missing Encoding and RequiredProperties when IformFile withOpenApi (#2979)
1 parent 43b8521 commit 1d99754

6 files changed

+67
-13
lines changed

src/Swashbuckle.AspNetCore.SwaggerGen/SwaggerGenerator/SwaggerGenerator.cs

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
420420
{
421421
foreach (var content in requestContentTypes)
422422
{
423+
content.Encoding = new Dictionary<string, OpenApiEncoding>();
423424
var requestParameters = apiDescription.ParameterDescriptions.Where(desc => desc.IsFromBody() || desc.IsFromForm());
424425
var countOfParameters = requestParameters.Count();
425426
if (countOfParameters > 0)
@@ -431,7 +432,7 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
431432
requestParameter.ModelMetadata.ModelType,
432433
schemaRepository,
433434
requestParameter.PropertyInfo(),
434-
requestParameter.ParameterInfo()));
435+
requestParameter.ParameterInfo()), content);
435436
}
436437
else
437438
{
@@ -442,25 +443,30 @@ private OpenApiOperation GenerateOpenApiOperationFromMetadata(ApiDescription api
442443
s.ModelMetadata.ModelType,
443444
schemaRepository,
444445
s.PropertyInfo(),
445-
s.ParameterInfo())))
446+
s.ParameterInfo()), content))
446447
.ToList()
447448
};
448449
}
449450
}
450451

451-
static OpenApiSchema GenerateSchemaIncludingFormFile(ApiParameterDescription apiParameterDescription, OpenApiSchema generatedSchema)
452+
static OpenApiSchema GenerateSchemaIncludingFormFile(ApiParameterDescription apiParameterDescription, OpenApiSchema generatedSchema, OpenApiMediaType mediaType)
452453
{
453-
if (generatedSchema.Reference is null
454-
&& ((generatedSchema.Type == "string" && generatedSchema.Format == "binary") || (generatedSchema.Type == "array" && generatedSchema.Items.Format == "binary")))
454+
if (generatedSchema.Reference is null && apiParameterDescription.IsFromForm())
455455
{
456-
return new OpenApiSchema()
456+
mediaType.Encoding.Add(apiParameterDescription.Name, new OpenApiEncoding { Style = ParameterStyle.Form });
457+
if ((generatedSchema.Type == "string" && generatedSchema.Format == "binary")
458+
|| (generatedSchema.Type == "array" && generatedSchema.Items.Type == "string" && generatedSchema.Items.Format == "binary"))
457459
{
458-
Type = "object",
459-
Properties = new Dictionary<string, OpenApiSchema>()
460+
return new OpenApiSchema()
460461
{
461-
[apiParameterDescription.Name] = generatedSchema
462-
}
463-
};
462+
Type = "object",
463+
Properties = new Dictionary<string, OpenApiSchema>()
464+
{
465+
[apiParameterDescription.Name] = generatedSchema
466+
},
467+
Required = apiParameterDescription.IsRequired ? new SortedSet<string>() { apiParameterDescription.Name } : null
468+
};
469+
}
464470
}
465471
return generatedSchema;
466472
}

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.SwaggerEndpoint_ReturnsValidSwaggerJson_For_WebApi_swaggerRequestUri=v1.verified.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,21 @@
128128
"content": {
129129
"multipart/form-data": {
130130
"schema": {
131+
"required": [
132+
"file"
133+
],
131134
"type": "object",
132135
"properties": {
133136
"file": {
134137
"type": "string",
135138
"format": "binary"
136139
}
137140
}
141+
},
142+
"encoding": {
143+
"file": {
144+
"style": "form"
145+
}
138146
}
139147
}
140148
},
@@ -163,6 +171,9 @@
163171
"content": {
164172
"multipart/form-data": {
165173
"schema": {
174+
"required": [
175+
"collection"
176+
],
166177
"type": "object",
167178
"properties": {
168179
"collection": {
@@ -173,6 +184,11 @@
173184
}
174185
}
175186
}
187+
},
188+
"encoding": {
189+
"collection": {
190+
"style": "form"
191+
}
176192
}
177193
}
178194
},

test/Swashbuckle.AspNetCore.IntegrationTests/SwaggerVerifyIntegrationTest.TypesAreRenderedCorrectly.verified.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,21 @@
128128
"content": {
129129
"multipart/form-data": {
130130
"schema": {
131+
"required": [
132+
"file"
133+
],
131134
"type": "object",
132135
"properties": {
133136
"file": {
134137
"type": "string",
135138
"format": "binary"
136139
}
137140
}
141+
},
142+
"encoding": {
143+
"file": {
144+
"style": "form"
145+
}
138146
}
139147
}
140148
},
@@ -163,6 +171,9 @@
163171
"content": {
164172
"multipart/form-data": {
165173
"schema": {
174+
"required": [
175+
"collection"
176+
],
166177
"type": "object",
167178
"properties": {
168179
"collection": {
@@ -173,6 +184,11 @@
173184
}
174185
}
175186
}
187+
},
188+
"encoding": {
189+
"collection": {
190+
"style": "form"
191+
}
176192
}
177193
}
178194
},

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGenerator/SwaggerGeneratorTests.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2155,6 +2155,9 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIF
21552155
Assert.NotNull(content.Value.Schema.Properties["param"]);
21562156
Assert.Equal("string", content.Value.Schema.Properties["param"].Type);
21572157
Assert.Equal("binary", content.Value.Schema.Properties["param"].Format);
2158+
Assert.NotNull(content.Value.Encoding);
2159+
Assert.NotNull(content.Value.Encoding["param"]);
2160+
Assert.Equal(ParameterStyle.Form, content.Value.Encoding["param"].Style);
21582161
}
21592162

21602163
[Fact]
@@ -2216,6 +2219,9 @@ public void GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIF
22162219
Assert.NotNull(content.Value.Schema.Properties["param"].Items);
22172220
Assert.Equal("string", content.Value.Schema.Properties["param"].Items.Type);
22182221
Assert.Equal("binary", content.Value.Schema.Properties["param"].Items.Format);
2222+
Assert.NotNull(content.Value.Encoding);
2223+
Assert.NotNull(content.Value.Encoding["param"]);
2224+
Assert.Equal(ParameterStyle.Form, content.Value.Encoding["param"].Style);
22192225
}
22202226

22212227
private static SwaggerGenerator Subject(

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGeneratorVerifyTests/SwaggerGeneratorVerifyTests.GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIFormFile.verified.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
Nullable: false,
3434
Deprecated: false,
3535
UnresolvedReference: false
36+
},
37+
Encoding: {
38+
param: {
39+
Style: Form
40+
}
3641
}
3742
}
3843
}
@@ -44,5 +49,5 @@
4449
}
4550
},
4651
Components: {},
47-
HashCode: 3B411279DDA5AD71B248D9E65E29E2545971131294B8FB032C0EC91640277615B8D600D78530054A7DA3754611589518B2C9773BB48A813B9951B46DE633743A
52+
HashCode: 7D034A2620C1D85B3AC60194DFA59693F727DE4704B2D02E124FDA37F843426C258EF2BEB84E6B8E8D315E23A4BCBE1F423B479E6CDF8AFFB8514D49B9A3CC9E
4853
}

test/Swashbuckle.AspNetCore.SwaggerGen.Test/SwaggerGeneratorVerifyTests/SwaggerGeneratorVerifyTests.GetSwagger_GenerateConsumesSchemas_ForProvidedOpenApiOperationWithIFormFileCollection.verified.txt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@
4242
Nullable: false,
4343
Deprecated: false,
4444
UnresolvedReference: false
45+
},
46+
Encoding: {
47+
param: {
48+
Style: Form
49+
}
4550
}
4651
}
4752
}
@@ -53,5 +58,5 @@
5358
}
5459
},
5560
Components: {},
56-
HashCode: 64312D7E174EFA8B92E7869E39FD7367BB6A464F0C5E8A72D0B010AE96AAE157EE91BC02BEDFEC2B01CCC6BAA4E7FA79156782C09D435428AE8F732D3C9EB1B9
61+
HashCode: 073D8B8E67D5100DD8AF06EC69A3C02B8DBF29E46621ED6EB590DEA519F2C8941398F6B95292D891CC4E18C2F4D5D38A8F904545CFFC219E4FF4613AD605E5A5
5762
}

0 commit comments

Comments
 (0)