Skip to content

Support resolving OpenApiPaths entries from document #54847

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 4 commits into from
Apr 2, 2024

Conversation

captainsafia
Copy link
Member

This PR adds support for generating the OpenApiPaths from a given set of ApiDescriptions.

@captainsafia captainsafia requested a review from amcasey March 29, 2024 22:20
@captainsafia captainsafia requested review from a team, halter73 and brunolins16 as code owners March 29, 2024 22:20
@ghost ghost added the old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels label Mar 29, 2024
@captainsafia captainsafia added area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi area-minimal Includes 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 labels Mar 29, 2024
Copy link
Member

@amcasey amcasey left a comment

Choose a reason for hiding this comment

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

I mostly have a bunch of questions.

/// </summary>
/// <param name="apiDescription">The ApiDescription to resolve an operation type from.</param>
/// <returns>The <see cref="OperationType"/> associated with the given <paramref name="apiDescription"/>.</returns>
public static OperationType ToOperationType(this ApiDescription apiDescription) =>
Copy link
Member

Choose a reason for hiding this comment

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

Nit: since it's apiDescription.HttpMethod and not apiDescription that's being converted, I might call this "GetOperationType".

}
}
// "" -> "/"
if (routePattern.PathSegments.Count == 0)
Copy link
Member

Choose a reason for hiding this comment

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

It doesn't matter either way, but I actually had in mind strippedRoute.Length == 0.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should check this before the loop (and string builder allocation) and just return "/";?

Copy link
Member Author

Choose a reason for hiding this comment

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

Maybe we should check this before the loop (and string builder allocation) and just return "/";?

Yep -- this is a good call.

{
strippedRoute.Append('/');
var segment = routePattern.PathSegments[i];
foreach (var part in segment.Parts)
Copy link
Member

Choose a reason for hiding this comment

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

Is it possible for the segment to contain neither literal parts nor parameter parts? If so, you might end up with "//".

Copy link
Member Author

Choose a reason for hiding this comment

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

From looking at the routing logic, the other possible option this could be is a RoutePatternSeparatorPart. According to the the doc string it looks like it's only used as the separator for dot-based separators that appear at the end of a route. In that case, we'll want to to use the content of the separator directly.

/// the object to support filtering each
/// description instance into its appropriate document.
/// </remarks>
internal OpenApiPaths GetOpenApiPaths()
Copy link
Member

Choose a reason for hiding this comment

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

Why is this internal? I couldn't find a call outside this type.

Copy link
Member Author

Choose a reason for hiding this comment

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

It was originally internal for testing but I ended up not invoking the GetOpenApiPaths method directly.


// Act & Assert
var exception = Assert.Throws<InvalidOperationException>(() => apiDescription.ToOperationType());
Assert.Equal("Unsupported HTTP method: UNKNOWN", exception.Message);
Copy link
Member

Choose a reason for hiding this comment

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

Nit: A name that isn't all uppercase would validate that the message gives the method string verbatim.

ApplicationName = "TestApplication"
};
var docService = new OpenApiDocumentService(
"v1",
Copy link
Member

Choose a reason for hiding this comment

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

Would it be worthwhile to test with a document name other than the default?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep -- doesn't hurt to add another test here.

@captainsafia captainsafia merged commit 5608eca into feature/openapi Apr 2, 2024
@captainsafia captainsafia deleted the safia/openapi-docservice branch April 2, 2024 15:21
captainsafia added a commit that referenced this pull request Apr 2, 2024
* Add support for generating OpenAPI info and paths

* Address feedback

* Fix sample and options injection

* Address more feedback
captainsafia added a commit that referenced this pull request Apr 18, 2024
Co-authored-by: Martin Costello <[email protected]>
Co-authored-by: Rick Anderson <[email protected]>

This PR adds support for OpenAPI document generation, sans schema generation to Microsoft.AspNetCore.OpenApi. Relevant changes are available in individual PRs:

- Add entry-point APIs for OpenAPI support (#54789)
- Support resolving OpenApiPaths entries from document (#54847) 
- Support generating OpenAPI operation and associated fields (#54903) 
- Add APIs for OpenAPI document transformers (#54935) 
- Add support for generating OpenAPI parameters (#55041)
- Add support for generating OpenAPI responses (#55020) 
- Add support for generating OpenAPI request bodies (#55040)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants