-
Notifications
You must be signed in to change notification settings - Fork 270
fix/unique tags #2167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
fix/unique tags #2167
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
93c468e
feat: deduplicates tags at the document level
baywet 96e071f
chore: removes extraneous tags locations
baywet 512f75c
chore: fixes unit test setup unable to find implementation
baywet 7b7be4a
chore; fixes parsing to avoid unnecessary allocations
baywet 763c0c1
feat: tags references are now deduplicated as well
baywet 8997383
chore: linting
baywet File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.OpenApi; | ||
|
||
#nullable enable | ||
/// <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<OpenApiTag> | ||
{ | ||
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(OpenApiTag? x, OpenApiTag? 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(OpenApiTag obj) => obj?.Name is null ? 0 : StringComparer.GetHashCode(obj.Name); | ||
} | ||
#nullable restore |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Microsoft.OpenApi.Models; | ||
using Xunit; | ||
|
||
namespace Microsoft.OpenApi.Tests; | ||
|
||
public class OpenApiTagComparerTests | ||
{ | ||
private readonly OpenApiTagComparer _comparer = OpenApiTagComparer.Instance; | ||
[Fact] | ||
public void Defensive() | ||
{ | ||
Assert.NotNull(_comparer); | ||
|
||
Assert.True(_comparer.Equals(null, null)); | ||
Assert.False(_comparer.Equals(null, new OpenApiTag())); | ||
Assert.Equal(0, _comparer.GetHashCode(null)); | ||
Assert.Equal(0, _comparer.GetHashCode(new OpenApiTag())); | ||
} | ||
[Fact] | ||
public void SameNamesAreEqual() | ||
{ | ||
var openApiTag1 = new OpenApiTag { Name = "tag" }; | ||
var openApiTag2 = new OpenApiTag { Name = "tag" }; | ||
Assert.True(_comparer.Equals(openApiTag1, openApiTag2)); | ||
} | ||
[Fact] | ||
public void SameInstanceAreEqual() | ||
{ | ||
var openApiTag = new OpenApiTag { Name = "tag" }; | ||
Assert.True(_comparer.Equals(openApiTag, openApiTag)); | ||
} | ||
|
||
[Fact] | ||
public void DifferentCasingAreNotEquals() | ||
{ | ||
var openApiTag1 = new OpenApiTag { Name = "tag" }; | ||
var openApiTag2 = new OpenApiTag { Name = "TAG" }; | ||
Assert.False(_comparer.Equals(openApiTag1, openApiTag2)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.