-
Notifications
You must be signed in to change notification settings - Fork 275
Expand file tree
/
Copy pathOpenApiTagComparer.cs
More file actions
45 lines (40 loc) · 1.48 KB
/
OpenApiTagComparer.cs
File metadata and controls
45 lines (40 loc) · 1.48 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
using System;
using System.Collections.Generic;
using Microsoft.OpenApi.Models.Interfaces;
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;
}
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.IsNullOrEmpty(obj?.Name) ? 0 : StringComparer.GetHashCode(obj!.Name);
}