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

protoc-gen-openapiv2: add example for AIP-133 #1524

Merged

Conversation

jonathaningram
Copy link
Contributor

@jonathaningram jonathaningram commented Jul 14, 2020

Originally opened against v1: #1514

This example illustrates that the book_id field is not converted into a query string parameter in the generated swagger.json file.

References to other Issues or PRs

This PR contains an example required for a fix for #559.

Generally speaking, the main issue is that for requests with a body, non-body fields need to be added as query parameters. I think this is the same conclusion that @wotzhs came to in their comment at #559 (comment).

Have you read the Contributing Guidelines?

Yes.

Brief description of what is fixed or changed

I added an example to examples/internal/proto/examplepb/a_bit_of_everything.proto that is very similar to the example in https://google.aip.dev/133#user-specified-ids that allows users to provide their own ID. The user-specified ID needs to become a query string parameter per aip-dev/google.aip.dev#542 (comment).

The gateway generator is fine, but the Swagger generator does not generate query string parameters when there is a body on the binding.

The generated parameters is currently:

{
    "parameters": [
          {
            "name": "parent",
            "description": "The publisher in which to create the book.\n\nFormat: `publishers/{publisher}`\n\nExample: `publishers/1257894000000000000`",
            "in": "path",
            "required": true,
            "type": "string"
          },
          {
            "name": "body",
            "description": "The book to create.",
            "in": "body",
            "required": true,
            "schema": {
              "$ref": "#/definitions/examplepbBook"
            }
          }
    ],
}

Notice that there is no mention of the book_id. For clients generated off the swagger.json there is literally no way for them to know they need to provide a book_id.

Thus parameters should include this:

{
  "name": "book_id",
  "description": "The ID to use for the book.\n\nThis must start with an alphanumeric character",
  "in": "query",
  "required": true,
  "type": "string"
}

Note: this is correlated by the reply on my AIP issue here: aip-dev/google.aip.dev#542 (comment)

Other comments

I started to fix this locally and I have a rough idea of what needs doing, but I wanted to start the PR with a broken example first and I will attempt to fix the issue using this as a base.

TODO

Thanks for reading :)

This example illustrates that the `book_id` field is not converted into
a query string parameter in the generated swagger.json file.
@jonathaningram
Copy link
Contributor Author

@johanbrandhorst hey! I added two TODO tasks to the original comment. Before I press on with those do you mind taking a peek at my changes? Some things:

  1. Is the failing timeout test flakiness or me?
  2. You'll see lots of changes that don't appear related to my AIP issue. If you inspect some of the things in examples/internal/proto/examplepb/a_bit_of_everything.proto you'll see that the additional fields were never mapped to query parameters (as they should have been, I believe). An example is CheckPostQueryParams. It maps the body to single_nested. All of the sibling fields were never mapped to query parameters—in my change you see they are now mapped.
  3. Does this look like it's on the right path?

@johanbrandhorst
Copy link
Collaborator

1. Is the failing timeout test flakiness or me?

I reran the test and it looks like it passed, so that test is clearly flaky. I've raised #1527 to fix it.

2. You'll see lots of changes that don't appear related to my AIP issue. If you inspect some of the things in examples/internal/proto/examplepb/a_bit_of_everything.proto you'll see that the additional fields were never mapped to query parameters (as they should have been, I believe). An example is `CheckPostQueryParams`. It maps the body to `single_nested`. All of the sibling fields were never mapped to query parameters—in my change you see they are now mapped.

This is great! Clearly this was never working correctly.

3. Does this look like it's on the right path?

I think it looks great so far.

@jonathaningram
Copy link
Contributor Author

Cool thanks @johanbrandhorst. I'll add that Go test next and then see how it looks, that might be all that's needed before this is ready for review.

non-body fields are added as query parameters
@jonathaningram jonathaningram marked this pull request as ready for review July 20, 2020 04:20
@jonathaningram
Copy link
Contributor Author

@johanbrandhorst and friends this is ready for review.

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 looks great! Thank you so much.

@johanbrandhorst johanbrandhorst merged commit 8e387f7 into grpc-ecosystem:v2 Jul 20, 2020
@johanbrandhorst
Copy link
Collaborator

Thanks for your contribution! How much work would it be to backport this to v1?

@jonathaningram
Copy link
Contributor Author

@johanbrandhorst you're welcome! It should be easy, I'll get on to that.

@jonathaningram jonathaningram deleted the v2-post-query-params branch July 21, 2020 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants