Skip to content

Commit 4cf52e1

Browse files
fix(v2): map OpenAPI 2.0 response schema type file to binary format (#3150) (#3151)
* test(v2): add unit test for OpenAPI 2.0 schema type file mapping to binary format * fix(v2): set format binary when reading OpenAPI 2.0 schema with type file * test(v2): add response schema integration test for type file * docs(v2): document OpenAPI 2.0 file type to binary format mapping in schema deserializer * test(v2): parse enclosing operation with produces to populate response content schema Co-authored-by: Hardik K <hardikkaurani1@gmail.com>
1 parent aa5e2b5 commit 4cf52e1

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System;
@@ -170,6 +170,11 @@ internal static partial class OpenApiV2Deserializer
170170
if (type != null)
171171
{
172172
o.Type = type.ToJsonSchemaType();
173+
// OpenAPI 2.0 response schema allows type: file, which maps to format: binary in OpenAPI 3.x
174+
if ("file".Equals(type, StringComparison.OrdinalIgnoreCase))
175+
{
176+
o.Format = "binary";
177+
}
173178
}
174179
}
175180
},

‎test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiSchemaTests.cs‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) Microsoft Corporation. All rights reserved.
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under the MIT license.
33

44
using System.Collections.Generic;
@@ -186,5 +186,40 @@ public async Task SerializeSchemaWithOnlyNullableShouldSucceed()
186186

187187
Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), schemaString.MakeLineBreaksEnvironmentNeutral());
188188
}
189+
190+
[Fact]
191+
public void ParseSchemaWithTypeFileShouldSetTypeStringAndFormatBinary()
192+
{
193+
// Arrange
194+
var node = JsonNode.Parse("{\"type\": \"file\"}");
195+
196+
// Act
197+
var schema = OpenApiV2Deserializer.LoadSchema(node, new(), new ParsingContext(new()));
198+
199+
// Assert
200+
Assert.NotNull(schema);
201+
Assert.Equal(JsonSchemaType.String, schema.Type);
202+
Assert.Equal("binary", schema.Format);
203+
}
204+
205+
[Fact]
206+
public void ParseResponseSchemaWithTypeFileShouldSetTypeStringAndFormatBinary()
207+
{
208+
// Arrange
209+
var node = JsonNode.Parse("{\"produces\": [\"application/octet-stream\"], \"responses\": {\"200\": {\"description\": \"File response\", \"schema\": {\"type\": \"file\"}}}}");
210+
211+
// Act
212+
var operation = OpenApiV2Deserializer.LoadOperation(node, new(), new ParsingContext(new()));
213+
214+
// Assert
215+
Assert.NotNull(operation);
216+
Assert.NotNull(operation.Responses);
217+
var response = Assert.IsType<OpenApiResponse>(operation.Responses["200"]);
218+
Assert.NotNull(response.Content);
219+
var mediaType = Assert.Single(response.Content.Values);
220+
Assert.NotNull(mediaType.Schema);
221+
Assert.Equal(JsonSchemaType.String, mediaType.Schema.Type);
222+
Assert.Equal("binary", mediaType.Schema.Format);
223+
}
189224
}
190225
}

0 commit comments

Comments
 (0)