-
Notifications
You must be signed in to change notification settings - Fork 10.1k
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
Upgrade to Microsoft.OpenApi 2.x and support OpenAPI v3.1 #59480
base: main
Are you sure you want to change the base?
Conversation
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.
Copilot reviewed 38 out of 53 changed files in this pull request and generated no comments.
Files not reviewed (15)
- eng/Versions.props: Language not supported
- src/OpenApi/src/Comparers/OpenApiReferenceComparer.cs: Evaluated as low risk
- src/OpenApi/src/Extensions/OpenApiSchemaExtensions.cs: Evaluated as low risk
- src/OpenApi/perf/Microbenchmarks/OpenApiSchemaComparerBenchmark.cs: Evaluated as low risk
- src/OpenApi/src/Comparers/OpenApiSchemaComparer.cs: Evaluated as low risk
- src/OpenApi/src/Comparers/ComparerHelpers.cs: Evaluated as low risk
- src/OpenApi/src/Comparers/OpenApiAnyComparer.cs: Evaluated as low risk
- src/OpenApi/src/Comparers/OpenApiDiscriminatorComparer.cs: Evaluated as low risk
- src/OpenApi/src/Comparers/OpenApiExternalDocsComparer.cs: Evaluated as low risk
- src/OpenApi/src/Comparers/OpenApiXmlComparer.cs: Evaluated as low risk
- src/OpenApi/src/Services/OpenApiOptions.cs: Evaluated as low risk
- src/OpenApi/sample/Transformers/AddExternalDocsTransformer.cs: Evaluated as low risk
- src/OpenApi/perf/Microbenchmarks/TransformersBenchmark.cs: Evaluated as low risk
- src/OpenApi/src/Extensions/OpenApiServiceCollectionExtensions.cs: Evaluated as low risk
- src/OpenApi/src/Schemas/OpenApiJsonSchema.Helpers.cs: Evaluated as low risk
}; | ||
document.Paths = await GetOpenApiPathsAsync(document, scopedServiceProvider, operationTransformers, schemaTransformers, cancellationToken); | ||
document.Tags = document.Tags?.Distinct(OpenApiTagComparer.Instance).ToList(); |
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 filed microsoft/OpenAPI.NET#1983 to eventually make this a lot smoother.
// resolution of references in the document. | ||
document.Workspace ??= new(); | ||
document.Workspace.RegisterComponents(document); | ||
if (document.Components?.Schemas is not null) |
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.
We do this to ensure that the schemas are always ordered lexicographically by reference ID in the document. This was previously done in the OpenApiSchemaReferenceTransformer
but since that's been removed in favor of using OpenApiSchemaReference
directly, we need to do the sorting somewhere.
@@ -199,7 +200,7 @@ await VerifyOpenApiDocument(builder, options, document => | |||
}); | |||
} | |||
|
|||
[Fact] | |||
[ConditionalFact(Skip = "https://github.com/dotnet/aspnetcore/issues/58619")] | |||
public async Task HandlesDuplicateSchemaReferenceIdsGeneratedByOverload() |
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.
This is one of the tests we have to temporarily skip while we sort out how schema deduping looks in the new model.
@@ -51,13 +51,13 @@ public async Task GetOpenApiParameters_RouteParametersAreAlwaysRequired() | |||
// Assert | |||
await VerifyOpenApiDocument(builder, document => | |||
{ | |||
var pathParameter = Assert.Single(document.Paths["/api/todos/{id}"].Operations[OperationType.Get].Parameters); | |||
var pathParameter = Assert.Single(document.Paths["/api/todos/{id}"].Operations[OperationType.Get].Parameters!); |
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.
Microsoft.OpenApi marks the parameter list property as nullable in the new model so we use a null suppression here and elsewhere to quiet nullability warnings.
I used Assert.NotNull
in other places so it's not the most consistent but 🤷🏽 .
Not sure why we are getting these warnings now. They are coming from the ApiDescription.Server and adjacent dependencies. We're planning on changing this in .NET 10 anyways (see #58353), so I'll go ahead and remove the out-of-support runtime target in this PR. |
.../Microsoft.AspNetCore.OpenApi.Tests/Extensions/OpenApiEndpointRouteBuilderExtensionsTests.cs
Outdated
Show resolved
Hide resolved
...NetCore.OpenApi.Tests/Services/OpenApiSchemaService/OpenApiSchemaService.ParameterSchemas.cs
Outdated
Show resolved
Hide resolved
@@ -86,7 +86,7 @@ internal static void ApplyValidationAttributes(this JsonNode schema, IEnumerable | |||
{ | |||
if (attribute is Base64StringAttribute) | |||
{ | |||
schema[OpenApiSchemaKeywords.TypeKeyword] = "string"; | |||
schema[OpenApiSchemaKeywords.TypeKeyword] = JsonSchemaType.String.ToString(); |
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.
Is it right that this is changing from string
to String
(and for all these from lower to sentence case)? If it is, maybe using nameof()
would be better to avoid the ToString()
call at runtime (unless the compiler is smart enough to do that automatically?) when it's a constant value.
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.
Is it right that this is changing from string to String (and for all these from lower to sentence case)?
Depends on the definition of write. I primarily did this to make the values more resilient to being read by Enum.Parse<JsonSchemaType>
without having case-insensitivity configured.
We do set ignoreCase: true
everywhere we parse these values and users should never interact with the opaque version of the schema but this feels...safer? 😓
Is it right that this is changing from string to String (and for all these from lower to sentence case)? If it is, maybe using nameof() would be better to avoid the ToString() call at runtime (unless the compiler is smart enough to do that automatically?) when it's a constant value.
The compiler doesn't map ToString
to nameof
. nameof
would work for simple types but things might get funky when we support nullability via the type
argument (JsonSchemaType.String | JsonSchemaType.Null
) so ToString
felt like the best approach to rely on consistently.
document.Components.Schemas ??= new Dictionary<string, OpenApiSchema>(); | ||
document.Components.Schemas[schemaId] = schema; | ||
document.Workspace ??= new(); | ||
var location = document.BaseUri + "/components/schemas/" + schemaId; |
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.
Is there any risk here of BaseUri
ending with a /
and messing up the concatenation?
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.
The BaseUri is currently implicitly configured by OpenAPI.NET (ref). The default value ends with a GUID and no trailing slash. They don't expose a setter
for the property and I believe it's primarily there to have a base document URL for resolving schemas so I don't tink the risk is likely.
@BrennanConroy Can I get your eye on this? I think you have the most context at the moment. @DeagleGross Some of this code might be new to you but the delta here might be a good opportunity to share some info about the space here. |
Contributes towards #58619.
This PR updates our Microsoft.OpenApi dependency to the new major version (v2) to support Open API v3.1.
Changes in this PR include:
JsonSchemaType
enum to represent schema typesIOpenApiAny
in favor ofJsonNode
per the change in the Microsoft.OpenApi libraryThe biggest functional change in this PR is the removal of the
OpenApiSchemaStore
class in favor of the newOpenApiSchemaReference
type. TheOpenApiSchemaStore
was our previous strategy of maintaining resolved references to OpenAPI schemas and their reference IDs. TheOpenApiSchemaReference
type supports this behavior by allowing you to hold a reference to a schema and support resolving that schema in one.This change means that we can store schemas directly in the
OpenApiDocument.Components.Schemas
property and provide resolved references to them for use in transformers without additional overhead on our part.This does temporarily introduce a regression into the logic we had for disambiguating duplicate schemas. Moving forward, I'd like to use this as a forcing function to implement the comparers in the Microsoft.OpenApi library (see microsoft/OpenAPI.NET#414) and avoid the overhead of having to maintain the comparers in our own codebase (removed here).
Another note: this change just lays the groundwork for this upgrade. There's still some more work required to support actual JSON schema features, like changing the way that nullability is modeled to use the
type
property or taking advantage of theconst
keyword. These changes will happen in follow up PRs.