forked from microsoft/OpenAPI.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenApiTagComparer.cs
More file actions
58 lines (53 loc) · 1.93 KB
/
OpenApiTagComparer.cs
File metadata and controls
58 lines (53 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Models.Interfaces;
using Microsoft.OpenApi.Models.References;
namespace Microsoft.OpenApi;
/// <summary>
/// This comparer is used to maintain a globally unique list of tags encountered
/// in a particular OpenAPI document.
/// </summary>
internal sealed class OpenApiTagComparer : IEqualityComparer<IOpenApiTag>
{
private static readonly Lazy<OpenApiTagComparer> _lazyInstance = new(() => new OpenApiTagComparer());
/// <summary>
/// Default instance for the comparer.
/// </summary>
internal static OpenApiTagComparer Instance { get => _lazyInstance.Value; }
/// <inheritdoc/>
public bool Equals(IOpenApiTag? x, IOpenApiTag? y)
{
if (x is null && y is null)
{
return true;
}
if (x is null || y is null)
{
return false;
}
if (ReferenceEquals(x, y))
{
return true;
}
if (x is OpenApiTagReference referenceX && y is OpenApiTagReference referenceY)
{
return StringComparer.Equals(referenceX.Name ?? referenceX.Reference.Id, referenceY.Name ?? referenceY.Reference.Id);
}
return StringComparer.Equals(x.Name, y.Name);
}
// Tag comparisons are case-sensitive by default. Although the OpenAPI specification
// only outlines case sensitivity for property names, we extend this principle to
// property values for tag names as well.
// See https://spec.openapis.org/oas/v3.1.0#format.
internal static readonly StringComparer StringComparer = StringComparer.Ordinal;
/// <inheritdoc/>
public int GetHashCode(IOpenApiTag obj)
{
string? value = obj?.Name;
if (value is null && obj is OpenApiTagReference reference)
{
value = reference.Reference.Id;
}
return string.IsNullOrEmpty(value) ? 0 : StringComparer.GetHashCode(value);
}
}