Skip to content
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

More thorough support for retrieving OpenAPI routes in a case-sensitive manner #59568

Open
wants to merge 10 commits into
base: main
Choose a base branch
from

Conversation

sander1095
Copy link
Contributor

@sander1095 sander1095 commented Dec 19, 2024

More thorough support for retrieving OpenAPI routes in a case-sensitive manner

  • You've read the Contributor Guide and Code of Conduct.
  • You've included unit or integration tests for your change, where applicable.
  • You've included inline docs for your change, where applicable.
  • There's an open issue for the PR that you are making. If you'd like to propose a new feature or change, please open an issue to discuss the change or find an existing issue.

This PR fixes some oversights from #59199 around retrieving OpenAPI documents case-insensitively.

Description

This PR modifies missed files from #59199 that use the registered document name as a key for retrieving services or options. This needs to be done case-insensitively to:

  • Ensure documents can be retrieved correctly
    • Trying to retrieve services around OpenAPI documents that are registered with a differently cased document name, will result in an exception, which is not desired!
  • Ensure documents are generated and retrieved with the correct OpenAPI version
    • If the configuration options are retrieved with an incorrectly cased document name argument, a default instance of OpenApiOptions is returned, which might contain different settings than what the user had configured.

This PR also adds some more comments and improves existing ones based on the previous PR around case-insensitive OpenAPI document names.

Fixes #59175

@sander1095 sander1095 requested review from captainsafia and a team as code owners December 19, 2024 15:10
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Dec 19, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label Dec 19, 2024
@martincostello martincostello added feature-openapi area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc and removed needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically labels Dec 19, 2024
Copy link
Member

@captainsafia captainsafia left a comment

Choose a reason for hiding this comment

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

Thanks for catching this and following up with a PR!

This has got me thinking that we should audit other places where we pass in documentName in the same way. As a result, it looks like we'll also want to make a change in OpenApiDocumentProvider. The same test around the API version should work here.

@sander1095 sander1095 changed the title Small fix around getting options when retrieving OpenAPI routes in a case-sensitive manner More thorough support for retrieving OpenAPI routes in a case-sensitive manner Dec 20, 2024
@sander1095
Copy link
Contributor Author

@captainsafia I've implemented your requested changes, added extra comments, improved existing ones and updated the title and description of PR to better reflect the changes.

Let me know if you want me to do anything else! :)

@@ -106,6 +105,9 @@ public static IServiceCollection AddOpenApi(this IServiceCollection services, Ac
public static IServiceCollection AddOpenApi(this IServiceCollection services)
=> services.AddOpenApi(OpenApiConstants.DefaultDocumentName);

/// <param name="services">The <see cref="IServiceCollection"/> to register services onto.</param>
Copy link
Member

Choose a reason for hiding this comment

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

We can remove the docstrings on private methods.

@@ -25,10 +25,17 @@ internal sealed class OpenApiDocumentProvider(IServiceProvider serviceProvider)
/// <param name="writer">A text writer associated with the document to write to.</param>
public async Task GenerateAsync(string documentName, TextWriter writer)
{
// We need to retrieve the document name in a case-insensitive manner to support case-insensitive document name resolution.
Copy link
Member

Choose a reason for hiding this comment

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

I think we can just reference the comment in the IServiceCollection extensions file instead of duplicating the content here.

// It's registered as "casesensitive" but we're passing in "CaseSensitive", which doesn't exist.
// Therefore, if the test doesn't throw, we know it has passed correctly.
// We still do a small check to validate the document, just in case. But the main test is that it doesn't throw.
ValidateOpenApiDocument(stringWriter, _ => { });
Copy link
Member

Choose a reason for hiding this comment

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

Nit: Add an assertion here to validate the OpenAPI version as above?

@sander1095
Copy link
Contributor Author

Thanks for the review! I'll implement requested changes after my holiday (in ~2 weeks)

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 community-contribution Indicates that the PR has been added by a community member feature-openapi
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OpenAPI document names are case-sensitive in urls
3 participants