|
| 1 | +/* |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | +
|
| 4 | + * MIT License |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | + * of this software and associated documentation files (the "Software"), to deal |
| 8 | + * in the Software without restriction, including without limitation the rights |
| 9 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 | + * copies of the Software, and to permit persons to whom the Software is |
| 11 | + * furnished to do so, subject to the following conditions: |
| 12 | + * |
| 13 | + * The above copyright notice and this permission notice shall be included in all |
| 14 | + * copies or substantial portions of the Software. |
| 15 | + * |
| 16 | + * THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | + * SOFTWARE. |
| 23 | + * |
| 24 | + */ |
| 25 | + |
| 26 | +// Adapted from https://github.com/microsoft/OpenAPI.NET/blob/3b61b45991dded1aaecb16330430628d26a406de/src/Microsoft.OpenApi/OpenApiTagComparer.cs#L1 |
| 27 | + |
| 28 | +namespace Microsoft.OpenApi; |
| 29 | + |
| 30 | +/// <summary> |
| 31 | +/// This comparer is used to maintain a globally unique list of tags encountered |
| 32 | +/// in a particular OpenAPI document. |
| 33 | +/// </summary> |
| 34 | +internal sealed class OpenApiTagComparer : |
| 35 | + IComparer<IOpenApiTag>, |
| 36 | + IComparer<OpenApiTagReference>, |
| 37 | + IEqualityComparer<IOpenApiTag>, |
| 38 | + IEqualityComparer<OpenApiTagReference> |
| 39 | +{ |
| 40 | + private static readonly Lazy<OpenApiTagComparer> _lazyInstance = new(() => new OpenApiTagComparer()); |
| 41 | + |
| 42 | + // Tag comparisons are case-sensitive by default. Although the OpenAPI specification |
| 43 | + // only outlines case sensitivity for property names, we extend this principle to |
| 44 | + // property values for tag names as well. |
| 45 | + // See https://spec.openapis.org/oas/v3.1.0#format. |
| 46 | + private static readonly StringComparer StringComparer = StringComparer.Ordinal; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Default instance for the comparer. |
| 50 | + /// </summary> |
| 51 | + internal static OpenApiTagComparer Instance => _lazyInstance.Value; |
| 52 | + |
| 53 | + public int Compare(IOpenApiTag x, IOpenApiTag y) |
| 54 | + { |
| 55 | + if (x is null) |
| 56 | + { |
| 57 | + return -1; |
| 58 | + } |
| 59 | + |
| 60 | + if (y is null) |
| 61 | + { |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + if (ReferenceEquals(x, y)) |
| 66 | + { |
| 67 | + return 0; |
| 68 | + } |
| 69 | + |
| 70 | + if (x is OpenApiTagReference referenceX && y is OpenApiTagReference referenceY) |
| 71 | + { |
| 72 | + return StringComparer.Compare(referenceX.Name ?? referenceX.Reference.Id, referenceY.Name ?? referenceY.Reference.Id); |
| 73 | + } |
| 74 | + |
| 75 | + return StringComparer.Compare(x.Name, y.Name); |
| 76 | + } |
| 77 | + |
| 78 | + public int Compare(OpenApiTagReference x, OpenApiTagReference y) |
| 79 | + { |
| 80 | + if (x is null) |
| 81 | + { |
| 82 | + return -1; |
| 83 | + } |
| 84 | + |
| 85 | + if (y is null) |
| 86 | + { |
| 87 | + return 1; |
| 88 | + } |
| 89 | + |
| 90 | + if (ReferenceEquals(x, y)) |
| 91 | + { |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + return StringComparer.Compare(x.Name ?? x.Reference.Id, y.Name ?? y.Reference.Id); |
| 96 | + } |
| 97 | + |
| 98 | + /// <inheritdoc/> |
| 99 | + public bool Equals(IOpenApiTag x, IOpenApiTag y) => Compare(x, y) == 0; |
| 100 | + |
| 101 | + /// <inheritdoc/> |
| 102 | + public bool Equals(OpenApiTagReference x, OpenApiTagReference y) => Compare(x, y) == 0; |
| 103 | + |
| 104 | + /// <inheritdoc/> |
| 105 | + public int GetHashCode(IOpenApiTag obj) |
| 106 | + { |
| 107 | + string value = obj?.Name; |
| 108 | + |
| 109 | + if (value is null && obj is OpenApiTagReference reference) |
| 110 | + { |
| 111 | + value = reference.Reference.Id; |
| 112 | + } |
| 113 | + |
| 114 | + return string.IsNullOrEmpty(value) ? 0 : StringComparer.GetHashCode(value); |
| 115 | + } |
| 116 | + |
| 117 | + /// <inheritdoc/> |
| 118 | + public int GetHashCode(OpenApiTagReference obj) |
| 119 | + { |
| 120 | + string value = obj?.Name ?? obj?.Reference?.Id; |
| 121 | + |
| 122 | + return string.IsNullOrEmpty(value) ? 0 : StringComparer.GetHashCode(value); |
| 123 | + } |
| 124 | +} |
0 commit comments