Skip to content
Open
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
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System;
Expand All @@ -20,7 +20,7 @@
{
private const string EncounteredNullableExtensionTrueMetadataKey = "encounteredNullable";

private static readonly FixedFieldMap<OpenApiSchema> _openApiSchemaFixedFields = new()

Check failure on line 23 in src/Microsoft.OpenApi/Reader/V2/OpenApiSchemaDeserializer.cs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this field to reduce its Cognitive Complexity from 41 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=microsoft_OpenAPI.NET&issues=AaF-5NF_NyFchgfNN3d9&open=AaF-5NF_NyFchgfNN3d9&pullRequest=3151
{
{
"title",
Expand Down Expand Up @@ -170,6 +170,11 @@
if (type != null)
{
o.Type = type.ToJsonSchemaType();
// OpenAPI 2.0 response schema allows type: file, which maps to format: binary in OpenAPI 3.x
if ("file".Equals(type, StringComparison.OrdinalIgnoreCase))
{
o.Format = "binary";
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.

using System.Collections.Generic;
Expand Down Expand Up @@ -186,5 +186,40 @@ public async Task SerializeSchemaWithOnlyNullableShouldSucceed()

Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), schemaString.MakeLineBreaksEnvironmentNeutral());
}

[Fact]
public void ParseSchemaWithTypeFileShouldSetTypeStringAndFormatBinary()
{
// Arrange
var node = JsonNode.Parse("{\"type\": \"file\"}");

// Act
var schema = OpenApiV2Deserializer.LoadSchema(node, new(), new ParsingContext(new()));

// Assert
Assert.NotNull(schema);
Assert.Equal(JsonSchemaType.String, schema.Type);
Assert.Equal("binary", schema.Format);
}

[Fact]
public void ParseResponseSchemaWithTypeFileShouldSetTypeStringAndFormatBinary()
{
// Arrange
var node = JsonNode.Parse("{\"produces\": [\"application/octet-stream\"], \"responses\": {\"200\": {\"description\": \"File response\", \"schema\": {\"type\": \"file\"}}}}");

// Act
var operation = OpenApiV2Deserializer.LoadOperation(node, new(), new ParsingContext(new()));

// Assert
Assert.NotNull(operation);
Assert.NotNull(operation.Responses);
var response = Assert.IsType<OpenApiResponse>(operation.Responses["200"]);
Assert.NotNull(response.Content);
var mediaType = Assert.Single(response.Content.Values);
Assert.NotNull(mediaType.Schema);
Assert.Equal(JsonSchemaType.String, mediaType.Schema.Type);
Assert.Equal("binary", mediaType.Schema.Format);
}
}
}
Loading