Skip to content

fix: set format to binary for file uploads #2305

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 2 commits into from
Apr 7, 2025
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -18,7 +18,7 @@
/// </summary>
internal static partial class OpenApiV2Deserializer
{
private static readonly FixedFieldMap<OpenApiParameter> _parameterFixedFields =

Check warning on line 21 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 23 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 21 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 23 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)

Check warning on line 21 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Refactor this field to reduce its Cognitive Complexity from 23 to the 15 allowed. (https://rules.sonarsource.com/csharp/RSPEC-3776)
new()
{
{
Expand Down Expand Up @@ -72,8 +72,13 @@
{
var type = n.GetScalarValue();
if (type != null)
{
GetOrCreateSchema(o).Type = type.ToJsonSchemaType();
{
var schema = GetOrCreateSchema(o);
schema.Type = type.ToJsonSchemaType();
if ("file".Equals(type, StringComparison.OrdinalIgnoreCase))
{
schema.Format = "binary";
}
}
}
},
Expand Down Expand Up @@ -209,7 +214,7 @@
}
}

private static void LoadParameterExamplesExtension(OpenApiParameter parameter, ParseNode node, OpenApiDocument? hostDocument)

Check warning on line 217 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'hostDocument'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 217 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'hostDocument'. (https://rules.sonarsource.com/csharp/RSPEC-1172)
{
var examples = LoadExamplesExtension(node);
node.Context.SetTempStorage(TempStorageKeys.Examples, examples, parameter);
Expand All @@ -219,11 +224,11 @@
{
return p.Schema switch {
OpenApiSchema schema => schema,
_ => (OpenApiSchema)(p.Schema = new OpenApiSchema()),

Check warning on line 227 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Extract the assignment of 'p.Schema' from this expression. (https://rules.sonarsource.com/csharp/RSPEC-1121)

Check warning on line 227 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Extract the assignment of 'p.Schema' from this expression. (https://rules.sonarsource.com/csharp/RSPEC-1121)

Check warning on line 227 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Extract the assignment of 'p.Schema' from this expression. (https://rules.sonarsource.com/csharp/RSPEC-1121)
};
}

private static void ProcessIn(OpenApiParameter o, ParseNode n, OpenApiDocument hostDocument)

Check warning on line 231 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'hostDocument'. (https://rules.sonarsource.com/csharp/RSPEC-1172)

Check warning on line 231 in src/Microsoft.OpenApi/Reader/V2/OpenApiParameterDeserializer.cs

View workflow job for this annotation

GitHub Actions / Build

Remove this unused method parameter 'hostDocument'. (https://rules.sonarsource.com/csharp/RSPEC-1172)
{
var value = n.GetScalarValue();
switch (value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
using Microsoft.OpenApi.Models;
using Microsoft.OpenApi.Reader.ParseNodes;
using Microsoft.OpenApi.Reader.V2;
using Microsoft.OpenApi.Tests;
using Microsoft.OpenApi.Writers;
using Xunit;

namespace Microsoft.OpenApi.Readers.Tests.V2Tests
Expand Down Expand Up @@ -279,5 +281,32 @@ public void ParseParameterWithEnumShouldSucceed()
.Excluding((IMemberInfo memberInfo) =>
memberInfo.Path.EndsWith("Parent")));
}

[Fact]
public void ParseFormDataParameterShouldSucceed()
{
// Arrange
var expected = @"{
""type"": ""string"",
""description"": ""file to upload"",
""format"": ""binary""
}";
MapNode node;
using (var stream = Resources.GetStream(Path.Combine(SampleFolderPath, "formDataParameter.json")))
{
node = TestHelper.CreateYamlMapNode(stream);
}

// Act
var operation = OpenApiV2Deserializer.LoadOperation(node, new());
var schema = operation.RequestBody?.Content["multipart/form-data"].Schema.Properties["file"];
var writer = new StringWriter();
schema.SerializeAsV2(new OpenApiJsonWriter(writer));
var json = writer.ToString();

// Assert
Assert.Equal("binary", schema.Format);
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), json.MakeLineBreaksEnvironmentNeutral());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"tags": [ "pet" ],
"summary": "uploads an image",
"description": "",
"operationId": "uploadFile",
"consumes": [ "multipart/form-data" ],
"produces": [ "application/json" ],
"parameters": [
{
"name": "petId",
"in": "path",
"description": "ID of pet to update",
"required": true,
"type": "integer",
"format": "int64"
},
{
"name": "additionalMetadata",
"in": "formData",
"description": "Additional data to pass to server",
"required": false,
"type": "string"
},
{
"name": "file",
"in": "formData",
"description": "file to upload",
"required": false,
"type": "file"
}
]
}