From 639aa7eba514adfde2f8dba656dfd560952e0b0a Mon Sep 17 00:00:00 2001 From: Vincent Biret Date: Fri, 27 Oct 2023 10:15:59 -0400 Subject: [PATCH] - fixes a bug where the conversion would ommit the schema type --- .../V2/OpenApiOperationDeserializer.cs | 3 +++ src/Microsoft.OpenApi/Models/OpenApiParameter.cs | 2 +- .../V2Tests/OpenApiOperationTests.cs | 3 +++ .../V2Tests/OpenApiPathItemTests.cs | 5 +++++ 4 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs b/src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs index 098222299..f47ca1c6c 100644 --- a/src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs +++ b/src/Microsoft.OpenApi.Readers/V2/OpenApiOperationDeserializer.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; +using System.Security.Cryptography.X509Certificates; using Microsoft.OpenApi.Any; using Microsoft.OpenApi.Extensions; using Microsoft.OpenApi.Models; @@ -171,6 +172,8 @@ private static OpenApiRequestBody CreateFormBody(ParsingContext context, List k, _ => mediaType) }; + foreach(var value in formBody.Content.Values.Where(static x => x.Schema is not null && x.Schema.Properties.Any() && string.IsNullOrEmpty(x.Schema.Type))) + value.Schema.Type = "object"; return formBody; } diff --git a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs index 263548a15..9d1651ad8 100644 --- a/src/Microsoft.OpenApi/Models/OpenApiParameter.cs +++ b/src/Microsoft.OpenApi/Models/OpenApiParameter.cs @@ -336,7 +336,7 @@ public void SerializeAsV2WithoutReference(IOpenApiWriter writer) // In V2 parameter's type can't be a reference to a custom object schema or can't be of type object // So in that case map the type as string. else - if (Schema?.UnresolvedReference == true || Schema?.Type == "object") + if (Schema?.UnresolvedReference == true || "object".Equals(Schema?.Type, StringComparison.OrdinalIgnoreCase)) { writer.WriteProperty(OpenApiConstants.Type, "string"); } diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs index d33aa5be2..326c16969 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiOperationTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System; using System.Collections.Generic; using System.IO; using System.Text; @@ -80,6 +81,7 @@ public class OpenApiOperationTests { Schema = new() { + Type = "object", Properties = { ["name"] = new() @@ -103,6 +105,7 @@ public class OpenApiOperationTests { Schema = new() { + Type = "object", Properties = { ["name"] = new() diff --git a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs index 177109131..8891cb1bf 100644 --- a/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs +++ b/test/Microsoft.OpenApi.Readers.Tests/V2Tests/OpenApiPathItemTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT license. +using System; using System.Collections.Generic; using System.IO; using System.Linq; @@ -67,6 +68,7 @@ public class OpenApiPathItemTests { Schema = new() { + Type = "object", Properties = { ["name"] = new() @@ -90,6 +92,7 @@ public class OpenApiPathItemTests { Schema = new() { + Type = "object", Properties = { ["name"] = new() @@ -171,6 +174,7 @@ public class OpenApiPathItemTests { Schema = new() { + Type = "object", Properties = { ["name"] = new() @@ -199,6 +203,7 @@ public class OpenApiPathItemTests { Schema = new() { + Type = "object", Properties = { ["name"] = new()