Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Upgrade to Microsoft.OpenApi 2.x and support OpenAPI v3.1#59480

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
captainsafia merged 10 commits intomainfromsafia/openapi-v2
Jan 14, 2025

Conversation

captainsafia
Copy link
Member

@captainsafiacaptainsafia commentedDec 14, 2024
edited
Loading

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:

  • Using the newJsonSchemaType enum to represent schema types
  • RemovingIOpenApiAny in favor ofJsonNode per the change in the Microsoft.OpenApi library
  • Updates to code for parsing OpenAPI documents to use the new APIs

The biggest functional change in this PR is the removal of theOpenApiSchemaStore 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 schemaand support resolving that schema in one.

This change means that we can store schemas directly in theOpenApiDocument.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 (seemicrosoft/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 thetype property or taking advantage of theconst keyword. These changes will happen in follow up PRs.

xC0dex, slang25, KennethHoff, and 0xfeeddeadbeef reacted with rocket emojimartincostello reacted with eyes emoji
@ghostghost added the old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labelDec 14, 2024
@captainsafiacaptainsafia added area-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed old-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labelsDec 14, 2024
Copy link
Contributor

@CopilotCopilotAI left a 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();
Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

I filedmicrosoft/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)
Copy link
MemberAuthor

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 theOpenApiSchemaReferenceTransformer but since that's been removed in favor of usingOpenApiSchemaReference 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()
Copy link
MemberAuthor

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!);
Copy link
MemberAuthor

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 usedAssert.NotNull in other places so it's not the most consistent but 🤷🏽 .

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@darrelmiller Based on our discussions of Nullable vs Optional, I would not expectParameters to be Nullable, sincenull is not an allowed value for theParameters field of anOperation object. But perhaps I am still confused about this.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

@mikekistler For my edification, are you expected to produce an empty list in the case where an operation has no parameters? Is it possible to omit theparameters field altogether?

@captainsafia
Copy link
MemberAuthor

##[error].packages\system.runtime.compilerservices.unsafe\6.0.0\buildTransitive\netcoreapp2.0\System.Runtime.CompilerServices.Unsafe.targets(4,5): error : System.Runtime.CompilerServices.Unsafe doesn't support netcoreapp2.1. Consider updating your TargetFramework to netcoreapp3.1 or later.

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.

@@ -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();

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 fromstring toString (and for all these from lower to sentence case)? If it is, maybe usingnameof() would be better to avoid theToString() call at runtime (unless the compiler is smart enough to do that automatically?) when it's a constant value.

Copy link
MemberAuthor

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 byEnum.Parse<JsonSchemaType> without having case-insensitivity configured.

Wedo setignoreCase: 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 mapToString tonameof.nameof would work for simple types but things might get funky when we support nullability via thetype argument (JsonSchemaType.String | JsonSchemaType.Null) soToString felt like the best approach to rely on consistently.

martincostello reacted with thumbs up emoji
document.Components.Schemas ??= new Dictionary<string, OpenApiSchema>();
document.Components.Schemas[schemaId] = schema;
document.Workspace ??= new();
var location = document.BaseUri + "/components/schemas/" + schemaId;

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 ofBaseUri ending with a/ and messing up the concatenation?

Copy link
MemberAuthor

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 asetter 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.

martincostello reacted with thumbs up emoji
@captainsafia
Copy link
MemberAuthor

@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.

@dotnet-policy-servicedotnet-policy-servicebot added the pending-ci-rerunWhen assigned to a PR indicates that the CI checks should be rerun labelDec 26, 2024
@captainsafia
Copy link
MemberAuthor

/azp run

@dotnet-policy-servicedotnet-policy-servicebot removed the pending-ci-rerunWhen assigned to a PR indicates that the CI checks should be rerun labelJan 2, 2025
@azure-pipelinesAzure Pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@dotnet-policy-servicedotnet-policy-servicebot added the pending-ci-rerunWhen assigned to a PR indicates that the CI checks should be rerun labelJan 9, 2025
@captainsafiacaptainsafia removed the pending-ci-rerunWhen assigned to a PR indicates that the CI checks should be rerun labelJan 10, 2025
}
else
{
schema.Type = type;
schema.Type =Enum.Parse<JsonSchemaType>(type, ignoreCase: true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Why ignoreCase here and elsewhere when parsing type? Where does this value of type come from that it might not not be the correct case (and that would be okay)?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

When System.Text.Json gives us the schema type, it is serialized as a string in all lowercase (object,null,array, etc.) TheJsonSchemaType enum provided by the OpenAPI uses title casing.Enum.Parse is case-sensitive by default soobject won't map toObject unless we use this flag.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

The JsonSchemaType enum provided by the OpenAPI uses title casing.

Isn't that a bug?

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

No, as most enums use title-case (or I guess Pascal scale) for the type names. STJ maps them to an all lower-case identifierhere) which is why we get the inconsistency in casing between whatEnum.ToString() while generate and what the schema will produce.

{
"name": "Test"
}
"Test"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This does not look right. The top-levelTags field should be an array ofTagObjects, notstrings.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yep, this is a known bug that I filed on the library over atmicrosoft/OpenAPI.NET#1991. It was fixed but the version with the fix contains a different bugmicrosoft/OpenAPI.NET#2055 so I am sticking with the devil I know for now. 😅

@@ -28,12 +28,12 @@
"type": "string",
"externalDocs": {
"description": "Documentation for this OpenAPI schema",
"url": "https://example.com/api/docs/schemas/string"
"url": "https://example.com/api/docs/schemas/String"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This change looks odd. I suppose the URL is made up so there is no right or wrong -- but usually URLs follow conventions, like "all lowercase" (as the previous URL did) or "camel cased segments". Here there is a mix of lowercase and Pascal case, which is ... odd.

Copy link
MemberAuthor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Yep, this is result of the same reason in#59480 (comment). Because we useJsonSchemaType.String.ToString() the default casing produced is titlecase. We can fix this by just using the literal "string" directly.

Copy link
Contributor

@mikekistlermikekistler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

This looks really good!

I left a few comments (sorry I didn't lump them into a review -- I didn't expect to make so many), but the only one that is a real problem is the way tag objects are not rendered correctly.

Copy link
Contributor

@mikekistlermikekistler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Looks good! 👍

I think this can merge as is. Any remaining issues are minor and can be cleaned up in a follow-on PR.

@captainsafiacaptainsafia merged commitd4880ed intomainJan 14, 2025
27 checks passed
@captainsafiacaptainsafia deleted the safia/openapi-v2 branchJanuary 14, 2025 15:57
@dotnet-policy-servicedotnet-policy-servicebot added this to the10.0-preview1 milestoneJan 14, 2025
captainsafia added a commit that referenced this pull requestFeb 11, 2025
* Upgrade to Microsoft.OpenApi 2.x and support OpenAPI v3.1* Update usage in GetDocumentInsider* Fix packaging and AoT tests* Update AoT test project exemptions* Address feedback* Try ignoring whitespace* Update to 2.0.0-preview.6
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@martincostellomartincostellomartincostello left review comments

@wtgodbewtgodbewtgodbe left review comments

Copilot code reviewCopilotCopilot left review comments

@mikekistlermikekistlermikekistler approved these changes

Assignees
No one assigned
Labels
area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcarea-mvcIncludes: MVC, Actions and Controllers, Localization, CORS, most templatesfeature-openapi
Projects
None yet
Milestone
10.0-preview1
Development

Successfully merging this pull request may close these issues.

4 participants
@captainsafia@martincostello@wtgodbe@mikekistler

[8]ページ先頭

©2009-2025 Movatter.jp