diff --git a/src/Microsoft.OpenApi/Extensions/CollectionExtensions.cs b/src/Microsoft.OpenApi/Extensions/CollectionExtensions.cs deleted file mode 100644 index 303c8ffed..000000000 --- a/src/Microsoft.OpenApi/Extensions/CollectionExtensions.cs +++ /dev/null @@ -1,50 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace Microsoft.OpenApi -{ - /// - /// Dictionary extension methods - /// - internal static class CollectionExtensions - { - /// - /// Returns a new dictionary with entries sorted by key using a custom comparer. - /// - internal static IDictionary Sort( - this IDictionary source, - IComparer comparer) - where TKey : notnull - { -#if NET7_0_OR_GREATER - ArgumentNullException.ThrowIfNull(nameof(source)); - ArgumentNullException.ThrowIfNull(nameof(comparer)); -#else - if (source == null) - throw new ArgumentNullException(nameof(source)); - if (comparer == null) - throw new ArgumentNullException(nameof(comparer)); -#endif - return source.OrderBy(kvp => kvp.Key, comparer) - .ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - } - - /// - /// Sorts any IEnumerable using the specified comparer and returns a List. - /// - internal static List Sort(this IEnumerable source, IComparer comparer) - { -#if NET7_0_OR_GREATER - ArgumentNullException.ThrowIfNull(source); - ArgumentNullException.ThrowIfNull(comparer); -#else - if (source == null) - throw new ArgumentNullException(nameof(source)); - if (comparer == null) - throw new ArgumentNullException(nameof(comparer)); -#endif - return source.OrderBy(item => item, comparer).ToList(); - } - } -} diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs index 5974924c0..f2e4aa5a9 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterExtensions.cs @@ -419,13 +419,6 @@ private static void WriteCollectionInternal( writer.WriteStartArray(); if (elements != null) { - var settings = writer.GetSettings(); - - if (settings?.KeyComparer is IComparer typedComparer) - { - elements = elements.Sort(typedComparer); - } - foreach (var item in elements) { if (item != null) @@ -464,12 +457,6 @@ private static void WriteMapInternal( if (elements != null) { - var settings = writer.GetSettings(); - if (settings?.KeyComparer != null) - { - elements = elements.Sort(settings.KeyComparer); // sort using custom comparer - } - foreach (var item in elements) { writer.WritePropertyName(item.Key); diff --git a/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs b/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs index cbd3344b2..1164bf849 100644 --- a/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs +++ b/src/Microsoft.OpenApi/Writers/OpenApiWriterSettings.cs @@ -1,6 +1,4 @@ -using System.Collections.Generic; - -namespace Microsoft.OpenApi +namespace Microsoft.OpenApi { /// /// Configuration settings to control how OpenAPI documents are written @@ -23,10 +21,5 @@ internal bool ShouldInlineReference(OpenApiReference reference) return (reference.IsLocal && InlineLocalReferences) || (reference.IsExternal && InlineExternalReferences); } - - /// - /// Specifies a comparer used to sort string-based collection keys, such as components or tags. - /// - public IComparer? KeyComparer { get; set; } } } diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi2_0.verified.txt b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi2_0.verified.txt deleted file mode 100644 index 316c35558..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi2_0.verified.txt +++ /dev/null @@ -1,57 +0,0 @@ -swagger: '2.0' -info: - title: Test API - version: '1.0' -paths: - pet: - get: - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully - newPet: - post: - consumes: - - application/json - parameters: - - in: body - name: body - description: Information about a new pet in the system - schema: - $ref: '#/definitions/Pet' - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully -definitions: - errorModel: - type: object - required: - - code - - message - properties: - code: - type: integer - format: int32 - message: - type: string - newPet: - type: object - required: - - name - properties: - id: - type: integer - format: int64 - name: - type: string - Pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - tag: - type: string \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi3_0.verified.txt b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi3_0.verified.txt deleted file mode 100644 index ca83e6790..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi3_0.verified.txt +++ /dev/null @@ -1,56 +0,0 @@ -openapi: 3.0.4 -info: - title: Test API - version: '1.0' -paths: - pet: - get: - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully - newPet: - post: - requestBody: - description: Information about a new pet in the system - content: - application/json: - schema: - $ref: '#/components/schemas/Pet' - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully -components: - schemas: - errorModel: - required: - - code - - message - type: object - properties: - code: - type: integer - format: int32 - message: - type: string - newPet: - required: - - name - type: object - properties: - id: - type: integer - format: int64 - name: - type: string - Pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - tag: - type: string \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi3_1.verified.txt b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi3_1.verified.txt deleted file mode 100644 index 78392d7e5..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentSucceeds_version=OpenApi3_1.verified.txt +++ /dev/null @@ -1,56 +0,0 @@ -openapi: '3.1.1' -info: - title: Test API - version: '1.0' -paths: - pet: - get: - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully - newPet: - post: - requestBody: - description: Information about a new pet in the system - content: - application/json: - schema: - $ref: '#/components/schemas/Pet' - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully -components: - schemas: - errorModel: - required: - - code - - message - type: object - properties: - code: - type: integer - format: int32 - message: - type: string - newPet: - required: - - name - type: object - properties: - id: - type: integer - format: int64 - name: - type: string - Pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - tag: - type: string \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi2_0.verified.txt b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi2_0.verified.txt deleted file mode 100644 index e285124a9..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi2_0.verified.txt +++ /dev/null @@ -1,57 +0,0 @@ -swagger: '2.0' -info: - title: Test API - version: '1.0' -paths: - pet: - get: - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully - newPet: - post: - consumes: - - application/json - parameters: - - in: body - name: body - description: Information about a new pet in the system - schema: - $ref: '#/definitions/Pet' - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully -definitions: - errorModel: - type: object - required: - - code - - message - properties: - code: - type: integer - format: int32 - message: - type: string - newPet: - type: object - required: - - name - properties: - id: - type: integer - format: int64 - name: - type: string - pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - tag: - type: string \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi3_0.verified.txt b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi3_0.verified.txt deleted file mode 100644 index da60b8ac2..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi3_0.verified.txt +++ /dev/null @@ -1,56 +0,0 @@ -openapi: 3.0.4 -info: - title: Test API - version: '1.0' -paths: - pet: - get: - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully - newPet: - post: - requestBody: - description: Information about a new pet in the system - content: - application/json: - schema: - $ref: '#/components/schemas/Pet' - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully -components: - schemas: - errorModel: - required: - - code - - message - type: object - properties: - code: - type: integer - format: int32 - message: - type: string - newPet: - required: - - name - type: object - properties: - id: - type: integer - format: int64 - name: - type: string - pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - tag: - type: string \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi3_1.verified.txt b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi3_1.verified.txt deleted file mode 100644 index dbab6f41a..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.SortOpenApiDocumentUsingCustomComparerSucceeds_version=OpenApi3_1.verified.txt +++ /dev/null @@ -1,56 +0,0 @@ -openapi: '3.1.1' -info: - title: Test API - version: '1.0' -paths: - pet: - get: - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully - newPet: - post: - requestBody: - description: Information about a new pet in the system - content: - application/json: - schema: - $ref: '#/components/schemas/Pet' - responses: - '200': - description: Return a 200 status to indicate that the data was received successfully -components: - schemas: - errorModel: - required: - - code - - message - type: object - properties: - code: - type: integer - format: int32 - message: - type: string - newPet: - required: - - name - type: object - properties: - id: - type: integer - format: int64 - name: - type: string - pet: - required: - - id - - name - properties: - id: - type: integer - format: int64 - name: - type: string - tag: - type: string \ No newline at end of file diff --git a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.cs b/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.cs deleted file mode 100644 index 49d8e325a..000000000 --- a/test/Microsoft.OpenApi.Tests/Extensions/CollectionExtensionsTests.cs +++ /dev/null @@ -1,221 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.IO; -using System.Net.Http; -using System.Threading.Tasks; -using VerifyXunit; -using Xunit; - -namespace Microsoft.OpenApi.Tests.Extensions -{ - public class CollectionExtensionsTests - { - public static readonly OpenApiDocument Document = new OpenApiDocument - { - Info = new OpenApiInfo { Title = "Test API", Version = "1.0" }, - Paths = new OpenApiPaths - { - ["pet"] = new OpenApiPathItem - { - Operations = new () - { - [HttpMethod.Get] = new OpenApiOperation - { - Responses = new OpenApiResponses - { - ["200"] = new OpenApiResponse - { - Description = "Return a 200 status to indicate that the data was received successfully" - } - } - } - } - }, - ["newPet"] = new OpenApiPathItem - { - Operations = new() - { - [HttpMethod.Post] = new OpenApiOperation - { - RequestBody = new OpenApiRequestBody - { - Description = "Information about a new pet in the system", - Content = new Dictionary() - { - ["application/json"] = new OpenApiMediaType - { - Schema = new OpenApiSchemaReference("Pet", null) - } - } - }, - Responses = new OpenApiResponses - { - ["200"] = new OpenApiResponse - { - Description = "Return a 200 status to indicate that the data was received successfully" - } - } - } - } - } - }, - Components = new OpenApiComponents - { - Schemas = new Dictionary() - { - ["pet"] = new OpenApiSchema() - { - Required = new HashSet - { - "id", "name" - }, - Properties = new Dictionary() - { - ["name"] = new OpenApiSchema() - { - Type = JsonSchemaType.String - }, - ["id"] = new OpenApiSchema() - { - Type = JsonSchemaType.Integer, - Format = "int64" - }, - ["tag"] = new OpenApiSchema() - { - Type = JsonSchemaType.String - } - }, - }, - ["newPet"] = new OpenApiSchema() - { - Type = JsonSchemaType.Object, - Required = new HashSet - { - "name" - }, - Properties = new Dictionary() - { - ["name"] = new OpenApiSchema() - { - Type = JsonSchemaType.String - }, - ["id"] = new OpenApiSchema() - { - Type = JsonSchemaType.Integer, - Format = "int64" - } - } - }, - ["errorModel"] = new OpenApiSchema() - { - Type = JsonSchemaType.Object, - Required = new HashSet - { - "code", - "message" - }, - Properties = new Dictionary() - { - ["message"] = new OpenApiSchema() - { - Type = JsonSchemaType.String - }, - ["code"] = new OpenApiSchema() - { - Type = JsonSchemaType.Integer, - Format = "int32" - } - } - } - } - } - }; - - [Theory] - [MemberData(nameof(OpenApiSpecVersions))] - public async Task SortOpenApiDocumentUsingCustomComparerSucceeds(OpenApiSpecVersion version) - { - // Arrange - var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); - var settings = new OpenApiWriterSettings - { - KeyComparer = StringComparer.OrdinalIgnoreCase - }; - var writer = new OpenApiYamlWriter(outputStringWriter, settings); - - // Act - Document.SerializeAs(version, writer); - await writer.FlushAsync(); - - // Assert - await Verifier.Verify(outputStringWriter).UseParameters(version); - } - - [Fact] - public async Task SortHashSetsWorks() - { - // Arrange - var expected = @"required: - - id - - name - - tag"; - var schema = new OpenApiSchema - { - Required = new HashSet { "tag", "id", "name" }, - }; - - var outputStringWriter = new StringWriter(CultureInfo.InvariantCulture); - var settings = new OpenApiWriterSettings - { - KeyComparer = StringComparer.OrdinalIgnoreCase - }; - var writer = new OpenApiYamlWriter(outputStringWriter, settings); - - // Act - schema.SerializeAsV3(writer); - await writer.FlushAsync(); - var sortedString = outputStringWriter.ToString(); - - // Assert - Assert.Equal(expected.MakeLineBreaksEnvironmentNeutral(), sortedString.MakeLineBreaksEnvironmentNeutral()); - } - - [Fact] - public void SortTagsByNameUsingComparerWorks() - { - // Arrange - var tags = new HashSet - { - new() { Name = "three" }, - new() { Name = "two" }, - new() { Name = "one" } - }; - - // Act - var sortedTags = tags.Sort(new OpenApiTagNameComparer()); - - // Assert - Assert.Equal(3, sortedTags.Count); - Assert.True(sortedTags[0].Name == "one"); - } - - public static TheoryData OpenApiSpecVersions() - { - var values = new TheoryData(); - foreach (var value in Enum.GetValues()) - { - values.Add(value); - } - return values; - } - } - - public class OpenApiTagNameComparer : IComparer - { - public int Compare(OpenApiTag tag1, OpenApiTag tag2) - { - return string.Compare(tag1.Name, tag2.Name, StringComparison.OrdinalIgnoreCase); - } - } -} diff --git a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj index 58950a1ea..37a454bec 100644 --- a/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj +++ b/test/Microsoft.OpenApi.Tests/Microsoft.OpenApi.Tests.csproj @@ -39,30 +39,6 @@ - - CollectionExtensionsTests.cs - - - - CollectionExtensionsTests.cs - - - - CollectionExtensionsTests.cs - - - - CollectionExtensionsTests.cs - - - - CollectionExtensionsTests.cs - - - - CollectionExtensionsTests.cs - - OpenApiCallbackReferenceTests.cs diff --git a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt index 19248ec7b..6eec1e204 100644 --- a/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt +++ b/test/Microsoft.OpenApi.Tests/PublicApi/PublicApi.approved.txt @@ -1594,7 +1594,6 @@ namespace Microsoft.OpenApi public OpenApiWriterSettings() { } public bool InlineExternalReferences { get; set; } public bool InlineLocalReferences { get; set; } - public System.Collections.Generic.IComparer? KeyComparer { get; set; } } public class OpenApiXml : Microsoft.OpenApi.IOpenApiElement, Microsoft.OpenApi.IOpenApiExtensible, Microsoft.OpenApi.IOpenApiSerializable {