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

feat: add expand_slashed_path_patterns flag #4813

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

Conversation

czabaj
Copy link

@czabaj czabaj commented Oct 9, 2024

References to other Issues or PRs

Closes #4784 - a feature request

Have you read the Contributing Guidelines?

Yes 👍

Brief description of what is fixed or changed

This adds an experimental flag into the OpenAPI generator, that expands path patterns containing URI sub-paths into the URI with new path parameters. This improves the readability and OpenAPI compatibility of protobuf APIs based on Google AIP. For more context read the linked issue.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

This is a great start. Could you please make sure to also:

  1. Add this new flag to the bazel rules for the openapiv2 generator. Just follow the pattern for the other flags.
  2. Add a small section to our docs about this new flag with some example use cases? The file you want is https://github.com/grpc-ecosystem/grpc-gateway/blob/main/docs/docs/mapping/customizing_openapi_output.md.

Thanks!

protoc-gen-openapiv2/internal/genopenapi/template.go Outdated Show resolved Hide resolved
protoc-gen-openapiv2/internal/genopenapi/template.go Outdated Show resolved Hide resolved
@czabaj
Copy link
Author

czabaj commented Oct 14, 2024

I created a single clean function that patches the path parts & params only when the flag is turned on. It consumes the parts from the templateToParts function which I reverted to its original shape. While running the test, I encountered a bug in templateToParts, or at least, I think it is a bug.

If you pass into the templateToParts URI template with a path param pattern, that uses a snake-case URI, like

/test/{name=test_cases/*}/

where the path pattern contains snake-kase test_cases_ and the useJSONNamesForFields` is turned on, then this is split into parts

test
name=testCases/*

so the path pattern is camelcased. I believe this is wrong, since only the path parameter names should be camelcased, not other parts of the URI - am I right?

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

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

I believe this is wrong, since only the path parameter names should be camelcased, not other parts of the URI - am I right?

Yeah this looks wrong, thanks for finding it. Can you fix it?

@@ -163,6 +163,18 @@ type Registry struct {

// enableRpcDeprecation whether to process grpc method's deprecated option
enableRpcDeprecation bool

// expandSlashedPathPatterns, if true, for a path parameter carying a sub-path, described via parameter pattern (i.e.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// expandSlashedPathPatterns, if true, for a path parameter carying a sub-path, described via parameter pattern (i.e.
// expandSlashedPathPatterns, if true, for a path parameter carrying a sub-path, described via parameter pattern (i.e.

@@ -1145,6 +1156,78 @@ func renderServiceTags(services []*descriptor.Service, reg *descriptor.Registry)
return tags
}

// expandPathPatterns search the URI parts for path parameters with pattern and when the pattern contains a sub-path,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// expandPathPatterns search the URI parts for path parameters with pattern and when the pattern contains a sub-path,
// expandPathPatterns searches the URI parts for path parameters with pattern and when the pattern contains a sub-path,

// Parameters:
// - pathParts: the URI parts parsed from the path template with `templateToParts` function
// - pathParams: the path parameters of the service binding, this slice will be mutated when the path pattern contains
// a sub-path with wildcard.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// a sub-path with wildcard.
// a sub-path with a wildcard.

Comment on lines +1169 to +1170
// The modified pathParts. Also mutates the pathParams slice.
func expandPathPatterns(pathParts []string, pathParams *[]descriptor.Parameter) []string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Since this mutates an input, what do you think about returning a parameter of the same type instead? It's a bit gross IMO to mutate the input slice and returning it will make it more explicit.

Suggested change
// The modified pathParts. Also mutates the pathParams slice.
func expandPathPatterns(pathParts []string, pathParams *[]descriptor.Parameter) []string {
// The modified pathParts and pathParams slice.
func expandPathPatterns(pathParts []string, pathParams []descriptor.Parameter) ([]string, []descriptor.Parameter) {

Comment on lines +1169 to +1170
// The modified pathParts. Also mutates the pathParams slice.
func expandPathPatterns(pathParts []string, pathParams *[]descriptor.Parameter) []string {
Copy link
Collaborator

Choose a reason for hiding this comment

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

I just realized that we have a package for parsing these paths already: https://github.com/grpc-ecosystem/grpc-gateway/blob/main/internal/httprule. What do you think about reusing this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Openapi generator - explode the AIP style path parameters into URL with multiple path parameters
2 participants