-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add OpenAPI operation and schema transformer interfaces #56395
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
captainsafia
merged 7 commits into
dotnet:main
from
martincostello:gh-56022-operation-interceptor
Jul 16, 2024
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
55956c6
Add operation and schema transformer interfaces
martincostello 9ee1a54
Add operation and schema transform examples
martincostello b5f0213
Fix rebase
martincostello 84d5497
Run operation transforms before documents
martincostello 010c297
Move transformer
martincostello 0e4c5d4
Revert submodule
martincostello 28acf62
Apply review feedback
martincostello 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
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
42 changes: 42 additions & 0 deletions
42
src/OpenApi/sample/Transformers/AddExternalDocsTransformer.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,42 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using Microsoft.AspNetCore.OpenApi; | ||
using Microsoft.OpenApi.Models; | ||
|
||
namespace Sample.Transformers; | ||
|
||
public sealed class AddExternalDocsTransformer(IConfiguration configuration) : IOpenApiOperationTransformer, IOpenApiSchemaTransformer | ||
{ | ||
public Task TransformAsync(OpenApiOperation operation, OpenApiOperationTransformerContext context, CancellationToken cancellationToken) | ||
{ | ||
if (operation.OperationId is { Length: > 0 } id && | ||
Uri.TryCreate(configuration["DocumentationBaseUrl"], UriKind.Absolute, out var baseUri)) | ||
{ | ||
var url = new Uri(baseUri, $"/api/docs/operations/{Uri.EscapeDataString(id)}"); | ||
|
||
operation.ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = "Documentation for this OpenAPI endpoint", | ||
Url = url | ||
}; | ||
} | ||
|
||
return Task.CompletedTask; | ||
} | ||
|
||
public Task TransformAsync(OpenApiSchema schema, OpenApiSchemaTransformerContext context, CancellationToken cancellationToken) | ||
{ | ||
if (Uri.TryCreate(configuration["DocumentationBaseUrl"], UriKind.Absolute, out var baseUri)) | ||
{ | ||
var url = new Uri(baseUri, $"/api/docs/schemas/{Uri.EscapeDataString(schema.Type)}"); | ||
|
||
schema.ExternalDocs = new OpenApiExternalDocs | ||
{ | ||
Description = "Documentation for this OpenAPI schema", | ||
Url = url | ||
}; | ||
} | ||
return Task.CompletedTask; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
{ | ||
"DocumentationBaseUrl": "https://example.com", | ||
"Logging": { | ||
"LogLevel": { | ||
"Default": "Information", | ||
|
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.
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 haven't run these yet.
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.
Having trouble running these locally. I've run
restore.cmd
, done. activate.ps1
and runeng\build.cmd
, but trying todotnet run
these benchmarks I just get this: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.
Ooof -- I usually develop on macOS/Linux so I don't run into the IIS-specific issues with the build. 😅
One thing I would check is if you have the C++ Components installed in Visual Studio. Then, perhaps try doing a build in the
src/Servers
directory?I'll try running them locally as well to see what we can get here.
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'll try running them from WSL on my laptop.
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'll have to try again another day - on WSL I'm getting issues from Node trying to build Blazor, which the build fails and complains about if it's not been produced.
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.
Welp -- I had written up this comment alongside my earlier review and then GitHub played me and didn't post it. Let me know if you are seeing similar numbers after making the change to the ActivatedTransformer scenarios.
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.
No problem! I’ll re-run them tomorrow and see what happens. For whatever reason they seem to take A While™️ to run on my little-laptop-that-could.
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.
Here's what I get in WSL with the latest changes:
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.
Did another run on my end against 28acf62. The numbers were seeing have some variance but tell the same story about the cost of activation, particularly for schema transformers which get called more frequently in the pipeline.
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.
See #56829.