Skip to content

Commit 47f10d3

Browse files
fix: set format to binary for file uploads (#2305)
* fix: set format to binary for file uploads * chore: ordinal comparison --------- Co-authored-by: Vincent Biret <vibiret@microsoft.com>
1 parent e73aa96 commit 47f10d3

File tree

3 files changed

+68
-2
lines changed

3 files changed

+68
-2
lines changed

src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

+7-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,13 @@ internal static partial class OpenApiV2Deserializer
7272
{
7373
var type = n.GetScalarValue();
7474
if (type != null)
75-
{
76-
GetOrCreateSchema(o).Type = type.ToJsonSchemaType();
75+
{
76+
var schema = GetOrCreateSchema(o);
77+
schema.Type = type.ToJsonSchemaType();
78+
if ("file".Equals(type, StringComparison.OrdinalIgnoreCase))
79+
{
80+
schema.Format = "binary";
81+
}
7782
}
7883
}
7984
},

test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiParameterTests.cs

+29
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
using Microsoft.OpenApi.Models;
99
using Microsoft.OpenApi.Reader.ParseNodes;
1010
using Microsoft.OpenApi.Reader.V2;
11+
using Microsoft.OpenApi.Tests;
12+
using Microsoft.OpenApi.Writers;
1113
using Xunit;
1214

1315
namespace Microsoft.OpenApi.Readers.Tests.V2Tests
@@ -279,5 +281,32 @@ public void ParseParameterWithEnumShouldSucceed()
279281
.Excluding((IMemberInfo memberInfo) =>
280282
memberInfo.Path.EndsWith("Parent")));
281283
}
284+
285+
[Fact]
286+
public void ParseFormDataParameterShouldSucceed()
287+
{
288+
// Arrange
289+
var expected = @"{
290+
""type"": ""string"",
291+
""description"": ""file to upload"",
292+
""format"": ""binary""
293+
}";
294+
MapNode node;
295+
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "formDataParameter.json")))
296+
{
297+
node = TestHelper.CreateYamlMapNode(stream);
298+
}
299+
300+
// Act
301+
var operation = OpenApiV2Deserializer.LoadOperation(node, new());
302+
var schema = operation.RequestBody?.Content["multipart/form-data"].Schema.Properties["file"];
303+
var writer = new StringWriter();
304+
schema.SerializeAsV2(new OpenApiJsonWriter(writer));
305+
var json = writer.ToString();
306+
307+
// Assert
308+
Assert.Equal("binary", schema.Format);
309+
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), json.MakeLineBreaksEnvironmentNeutral());
310+
}
282311
}
283312
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"tags": [ "pet" ],
3+
"summary": "uploads an image",
4+
"description": "",
5+
"operationId": "uploadFile",
6+
"consumes": [ "multipart/form-data" ],
7+
"produces": [ "application/json" ],
8+
"parameters": [
9+
{
10+
"name": "petId",
11+
"in": "path",
12+
"description": "ID of pet to update",
13+
"required": true,
14+
"type": "integer",
15+
"format": "int64"
16+
},
17+
{
18+
"name": "additionalMetadata",
19+
"in": "formData",
20+
"description": "Additional data to pass to server",
21+
"required": false,
22+
"type": "string"
23+
},
24+
{
25+
"name": "file",
26+
"in": "formData",
27+
"description": "file to upload",
28+
"required": false,
29+
"type": "file"
30+
}
31+
]
32+
}

0 commit comments

Comments
 (0)