-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add support for reference-based schemas in OpenAPI document #56175
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
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
311866d
Add support for reference-based schemas in OpenAPI document
captainsafia b4d6a6b
Use ReferenceEquals in equality checks and address feedback
captainsafia 9086b3e
Support generating friendly reference names
captainsafia 5b44e5e
Add schema spec tests, move controller tests, move dedupe logic
captainsafia 7372bb8
Add more scenarios to snapsho tests
captainsafia 623fa19
Harden OpenApi comparer implementations
captainsafia 67508ad
Add tests for schema transformers + refs
captainsafia cde17fb
Address more feedback
captainsafia 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
63 changes: 63 additions & 0 deletions
63
src/OpenApi/perf/Microbenchmarks/OpenApiSchemaComparerBenchmark.cs
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,63 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Globalization; | ||
using BenchmarkDotNet.Attributes; | ||
using Microsoft.OpenApi.Any; | ||
using Microsoft.OpenApi.Interfaces; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi.Microbenchmarks; | ||
|
||
public class OpenApiSchemaComparerBenchmark | ||
{ | ||
[Params(1, 10, 100)] | ||
public int ElementCount { get; set; } | ||
|
||
private OpenApiSchema _schema; | ||
|
||
[GlobalSetup(Target = nameof(OpenApiSchema_GetHashCode))] | ||
public void OpenApiSchema_Setup() | ||
{ | ||
_schema = new OpenApiSchema | ||
{ | ||
AdditionalProperties = GenerateInnerSchema(), | ||
AdditionalPropertiesAllowed = true, | ||
AllOf = Enumerable.Range(0, ElementCount).Select(_ => GenerateInnerSchema()).ToList(), | ||
AnyOf = Enumerable.Range(0, ElementCount).Select(_ => GenerateInnerSchema()).ToList(), | ||
Deprecated = true, | ||
Default = new OpenApiString("default"), | ||
Description = "description", | ||
Discriminator = new OpenApiDiscriminator(), | ||
Example = new OpenApiString("example"), | ||
ExclusiveMaximum = true, | ||
ExclusiveMinimum = true, | ||
Extensions = new Dictionary<string, IOpenApiExtension> | ||
{ | ||
["key"] = new OpenApiString("value") | ||
}, | ||
ExternalDocs = new OpenApiExternalDocs(), | ||
Enum = Enumerable.Range(0, ElementCount).Select(_ => (IOpenApiAny)new OpenApiString("enum")).ToList(), | ||
OneOf = Enumerable.Range(0, ElementCount).Select(_ => GenerateInnerSchema()).ToList(), | ||
}; | ||
|
||
static OpenApiSchema GenerateInnerSchema() => new OpenApiSchema | ||
{ | ||
Properties = Enumerable.Range(0, 10).ToDictionary(i => i.ToString(CultureInfo.InvariantCulture), _ => new OpenApiSchema()), | ||
Deprecated = true, | ||
Default = new OpenApiString("default"), | ||
Description = "description", | ||
Example = new OpenApiString("example"), | ||
Extensions = new Dictionary<string, IOpenApiExtension> | ||
{ | ||
["key"] = new OpenApiString("value") | ||
}, | ||
}; | ||
} | ||
|
||
[Benchmark] | ||
public void OpenApiSchema_GetHashCode() | ||
{ | ||
OpenApiSchemaComparer.Instance.GetHashCode(_schema); | ||
} | ||
} |
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,75 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using Microsoft.OpenApi.Any; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
internal sealed class OpenApiAnyComparer : IEqualityComparer<IOpenApiAny> | ||
{ | ||
public static OpenApiAnyComparer Instance { get; } = new OpenApiAnyComparer(); | ||
|
||
public bool Equals(IOpenApiAny? x, IOpenApiAny? y) | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
if (x is null && y is null) | ||
{ | ||
return true; | ||
} | ||
if (x is null || y is null) | ||
{ | ||
return false; | ||
} | ||
if (object.ReferenceEquals(x, y)) | ||
{ | ||
return true; | ||
} | ||
|
||
return x.AnyType == y.AnyType && | ||
(x switch | ||
{ | ||
OpenApiNull _ => y is OpenApiNull, | ||
OpenApiArray arrayX => y is OpenApiArray arrayY && arrayX.SequenceEqual(arrayY, Instance), | ||
OpenApiObject objectX => y is OpenApiObject objectY && objectX.Keys.Count == objectY.Keys.Count && objectX.Keys.All(key => objectY.ContainsKey(key) && Equals(objectX[key], objectY[key])), | ||
OpenApiBinary binaryX => y is OpenApiBinary binaryY && binaryX.Value.SequenceEqual(binaryY.Value), | ||
OpenApiInteger integerX => y is OpenApiInteger integerY && integerX.Value == integerY.Value, | ||
OpenApiLong longX => y is OpenApiLong longY && longX.Value == longY.Value, | ||
OpenApiDouble doubleX => y is OpenApiDouble doubleY && doubleX.Value == doubleY.Value, | ||
OpenApiFloat floatX => y is OpenApiFloat floatY && floatX.Value == floatY.Value, | ||
OpenApiBoolean booleanX => y is OpenApiBoolean booleanY && booleanX.Value == booleanY.Value, | ||
OpenApiString stringX => y is OpenApiString stringY && stringX.Value == stringY.Value, | ||
OpenApiPassword passwordX => y is OpenApiPassword passwordY && passwordX.Value == passwordY.Value, | ||
OpenApiByte byteX => y is OpenApiByte byteY && byteX.Value.SequenceEqual(byteY.Value), | ||
OpenApiDate dateX => y is OpenApiDate dateY && dateX.Value == dateY.Value, | ||
OpenApiDateTime dateTimeX => y is OpenApiDateTime dateTimeY && dateTimeX.Value == dateTimeY.Value, | ||
_ => x.Equals(y) | ||
}); | ||
} | ||
|
||
public int GetHashCode(IOpenApiAny obj) | ||
{ | ||
var hashCode = new HashCode(); | ||
hashCode.Add(obj.AnyType); | ||
if (obj is IOpenApiPrimitive primitive) | ||
{ | ||
hashCode.Add(primitive.PrimitiveType); | ||
} | ||
hashCode.Add<object?>(obj switch | ||
{ | ||
OpenApiBinary binary => binary.Value, | ||
captainsafia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
OpenApiInteger integer => integer.Value, | ||
OpenApiLong @long => @long.Value, | ||
OpenApiDouble @double => @double.Value, | ||
OpenApiFloat @float => @float.Value, | ||
OpenApiBoolean boolean => boolean.Value, | ||
OpenApiString @string => @string.Value, | ||
OpenApiPassword password => password.Value, | ||
OpenApiByte @byte => @byte.Value, | ||
OpenApiDate date => date.Value, | ||
OpenApiDateTime dateTime => dateTime.Value, | ||
_ => null | ||
}); | ||
|
||
return hashCode.ToHashCode(); | ||
} | ||
} |
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 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
internal sealed class OpenApiDiscriminatorComparer : IEqualityComparer<OpenApiDiscriminator> | ||
{ | ||
public static OpenApiDiscriminatorComparer Instance { get; } = new OpenApiDiscriminatorComparer(); | ||
|
||
public bool Equals(OpenApiDiscriminator? x, OpenApiDiscriminator? y) | ||
{ | ||
if (x is null && y is null) | ||
{ | ||
return true; | ||
} | ||
if (x is null || y is null) | ||
{ | ||
return false; | ||
} | ||
if (object.ReferenceEquals(x, y)) | ||
{ | ||
return true; | ||
} | ||
|
||
return x.PropertyName == y.PropertyName && | ||
x.Mapping.Count == y.Mapping.Count && | ||
x.Mapping.Keys.All(key => y.Mapping.ContainsKey(key) && x.Mapping[key] == y.Mapping[key]); | ||
} | ||
|
||
public int GetHashCode(OpenApiDiscriminator obj) | ||
{ | ||
var hashCode = new HashCode(); | ||
hashCode.Add(obj.PropertyName); | ||
hashCode.Add(obj.Mapping.Count); | ||
return hashCode.ToHashCode(); | ||
} | ||
} |
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,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System.Linq; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
internal sealed class OpenApiExternalDocsComparer : IEqualityComparer<OpenApiExternalDocs> | ||
{ | ||
public static OpenApiExternalDocsComparer Instance { get; } = new OpenApiExternalDocsComparer(); | ||
|
||
public bool Equals(OpenApiExternalDocs? x, OpenApiExternalDocs? y) | ||
{ | ||
if (x is null && y is null) | ||
{ | ||
return true; | ||
} | ||
if (x is null || y is null) | ||
{ | ||
return false; | ||
} | ||
if (object.ReferenceEquals(x, y)) | ||
{ | ||
return true; | ||
} | ||
|
||
return x.Description == y.Description && | ||
x.Url == y.Url && | ||
x.Extensions.Count == y.Extensions.Count | ||
&& x.Extensions.Keys.All(k => y.Extensions.ContainsKey(k) && y.Extensions[k] == x.Extensions[k]); | ||
} | ||
|
||
public int GetHashCode(OpenApiExternalDocs obj) | ||
{ | ||
var hashCode = new HashCode(); | ||
hashCode.Add(obj.Description); | ||
hashCode.Add(obj.Url); | ||
hashCode.Add(obj.Extensions.Count); | ||
return hashCode.ToHashCode(); | ||
} | ||
} |
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,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Microsoft.AspNetCore.OpenApi; | ||
|
||
internal sealed class OpenApiReferenceComparer : IEqualityComparer<OpenApiReference> | ||
{ | ||
public static OpenApiReferenceComparer Instance { get; } = new OpenApiReferenceComparer(); | ||
|
||
public bool Equals(OpenApiReference? x, OpenApiReference? y) | ||
{ | ||
if (x is null && y is null) | ||
{ | ||
return true; | ||
} | ||
if (x is null || y is null) | ||
{ | ||
return false; | ||
} | ||
if (object.ReferenceEquals(x, y)) | ||
{ | ||
return true; | ||
} | ||
|
||
return x.ExternalResource == y.ExternalResource && | ||
x.HostDocument?.HashCode == y.HostDocument?.HashCode && | ||
x.Id == y.Id && | ||
x.Type == y.Type; | ||
} | ||
|
||
public int GetHashCode(OpenApiReference obj) | ||
{ | ||
var hashCode = new HashCode(); | ||
hashCode.Add(obj.ExternalResource); | ||
hashCode.Add(obj.Id); | ||
if (obj.Type is not null) | ||
{ | ||
hashCode.Add(obj.Type); | ||
} | ||
return hashCode.ToHashCode(); | ||
} | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually give such types a private constructor, but I'm guessing you considered that to be too much ceremony.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mean providing a private constructor over a static
Instance
property or both? What's he value of the private constructor in the later case?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I usually provide both so no one fails to notice the
Instance
member and instantiates it themselves.