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

Support request header parameters #3010

Merged

Conversation

krak3n
Copy link
Contributor

@krak3n krak3n commented Nov 11, 2022

Fixes #2932

Feature 🚀

Adds support for optional HTTP request header parameters to be defined on method operations, for example:

service FooService {
  rpc CreateFoo(CreateFooRequest) returns (CreateFooResponse) {
    option (google.api.http) = {
      post: "/foo"
      body: "*"
    };

    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      summary: "Create a new foo.";
      parameters: {
        headers: {
          name: "Authorization";
          description: "Basic Auth";
          required: true;
        };
      };
    };
  }
}

This should generate the following yaml:

paths:
  /v2/foo:
    post:
      summary: Create a new foo.
      operationId: CreateFoo
      responses:
        "200":
          description: OK
          schema:
            $ref: '#/definitions/CreateFooResponse'
      parameters:
        - name: foo
          in: body
          required: true
          schema:
            $ref: '#/definitions/CreateFooRequest'
        - name: Authorization
          in: header
          required: false

Changes

  • New protocol buffer definitions for HTTP Header Parameters
  • New loop in applyTemplate which loops over header parameters if defined and > 0 which builds and appends the header parameters. to the existing generated parameters.
  • Test cases for the above

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.

Thanks a lot for looking at this! Some minor comments, but in addition to those please add a section to the docs about this ✨ new feature ✨. This sort of thing has been requested a lot, thanks for helping bring it over the line. Please ensure the docs make it clear that adding headers to your schema via these annotations does nothing to change the behavior of the gateway and that these annotations should only be coupled with custom header parsing behavior in the application. Also that adding header parameters can chance the forward and backward compatibility guarantees of the schema. Thanks!

protoc-gen-openapiv2/options/openapiv2.proto Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Outdated Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Show resolved Hide resolved
protoc-gen-openapiv2/options/openapiv2.proto Outdated Show resolved Hide resolved
protoc-gen-openapiv2/internal/genopenapi/template_test.go Outdated Show resolved Hide resolved
protoc-gen-openapiv2/internal/genopenapi/template.go Outdated Show resolved Hide resolved
Co-Authored-By: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
@krak3n krak3n force-pushed the feat/request-header-parameters branch from 964f82f to b3a8280 Compare November 14, 2022 16:16
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.

The code changes look fine but could you please add something to the docs as well? Also, it looks like you need to regenerate the files again 😁.

@krak3n
Copy link
Contributor Author

krak3n commented Nov 15, 2022

@johanbrandhorst added documentation & regenerated the go code. I noticed when I run buf generate a load of swagger json also gets generated, does that need to be committed too?

@johanbrandhorst
Copy link
Collaborator

@johanbrandhorst added documentation & regenerated the go code. I noticed when I run buf generate a load of swagger json also gets generated, does that need to be committed too?

Just running buf generate will not generate the files the way we want, you'll need the command in the CONTRIBUTING.md file, which it looks like you're aware of. If there were more files needed, our generate job should detect it. Looks good from this side!

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.

LGTM

@johanbrandhorst johanbrandhorst merged commit e59b2ad into grpc-ecosystem:master Nov 15, 2022
@johanbrandhorst
Copy link
Collaborator

I think this is one of the most user requested features, so thank you for this great contribution! I'll consider making a new release shortly.

@krak3n krak3n deleted the feat/request-header-parameters branch November 15, 2022 23:16
andrewpollock pushed a commit to google/osv.dev that referenced this pull request Nov 28, 2022
)

[![Mend
Renovate](https://app.renovatebot.com/images/banner.svg)](https://renovatebot.com)

This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
|
[github.com/grpc-ecosystem/grpc-gateway/v2](https://togithub.com/grpc-ecosystem/grpc-gateway)
| require | minor | `v2.13.0` -> `v2.14.0` |

---

### Release Notes

<details>
<summary>grpc-ecosystem/grpc-gateway</summary>

###
[`v2.14.0`](https://togithub.com/grpc-ecosystem/grpc-gateway/releases/tag/v2.14.0)

[Compare
Source](https://togithub.com/grpc-ecosystem/grpc-gateway/compare/v2.13.0...v2.14.0)

#### New features

This release contains two significant new OpenAPIv2 generator features,
contributed by [@&#8203;krak3n](https://togithub.com/krak3n):

1. A new option to [disable rendering of 200 OK
responses](https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_openapi_output/#disable-default-responses).
This is useful if you define custom responses for your endpoints and you
modify the return code a forward response writer. Note that this does
not change the behavior of the gateway itself.
2. A new annotation for [defining header
parameters](https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_openapi_output/#custom-http-header-request-parameters).
This lets to define header parameters you want to be rendered in the
swagger.json output in addition to those defined in your API messages.
Note that this does not change the behavior of the gateway itself and
must be coupled with custom header parsing in your application.

#### What's Changed

- release: Update release.yml with option to workaround SLSA generator
failure by [@&#8203;asraa](https://togithub.com/asraa) in
[grpc-ecosystem/grpc-gateway#2987
- release: add a workflow_dispatch trigger for testing by
[@&#8203;asraa](https://togithub.com/asraa) in
[grpc-ecosystem/grpc-gateway#2989
- Use io/os instread of ioutil and use suitable verb by
[@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) in
[grpc-ecosystem/grpc-gateway#2991
- runtime pkg cleanup by
[@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) in
[grpc-ecosystem/grpc-gateway#2993
- mux: fix path components mutation by
[@&#8203;jonathaningram](https://togithub.com/jonathaningram) in
[grpc-ecosystem/grpc-gateway#3001
- fix: set consumes definition per operation by
[@&#8203;stomy13](https://togithub.com/stomy13) in
[grpc-ecosystem/grpc-gateway#2995
- protoc gen oas v2 cleanup by
[@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) in
[grpc-ecosystem/grpc-gateway#2996
- Use ReplaceAll instead of Replace with -1 pos by
[@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) in
[grpc-ecosystem/grpc-gateway#3003
- Errors cleanup by
[@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) in
[grpc-ecosystem/grpc-gateway#3004
- Cleanup by
[@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) in
[grpc-ecosystem/grpc-gateway#3012
- Support disabling default response rendering by
[@&#8203;krak3n](https://togithub.com/krak3n) in
[grpc-ecosystem/grpc-gateway#3006
- Support request header parameters by
[@&#8203;krak3n](https://togithub.com/krak3n) in
[grpc-ecosystem/grpc-gateway#3010

#### New Contributors

- [@&#8203;asraa](https://togithub.com/asraa) made their first
contribution in
[grpc-ecosystem/grpc-gateway#2987
- [@&#8203;sashamelentyev](https://togithub.com/sashamelentyev) made
their first contribution in
[grpc-ecosystem/grpc-gateway#2991
- [@&#8203;stomy13](https://togithub.com/stomy13) made their first
contribution in
[grpc-ecosystem/grpc-gateway#2995
- [@&#8203;krak3n](https://togithub.com/krak3n) made their first
contribution in
[grpc-ecosystem/grpc-gateway#3006

**Full Changelog**:
grpc-ecosystem/grpc-gateway@v2.13.0...v2.14.0

</details>

---

### Configuration

📅 **Schedule**: Branch creation - "before 6am on monday" in timezone
Australia/Sydney, Automerge - At any time (no schedule defined).

🚦 **Automerge**: Disabled by config. Please merge this manually once you
are satisfied.

♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the
rebase/retry checkbox.

🔕 **Ignore**: Close this PR and you won't be reminded about this update
again.

---

- [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check
this box

---

This PR has been generated by [Mend
Renovate](https://www.mend.io/free-developer-tools/renovate/). View
repository job log
[here](https://app.renovatebot.com/dashboard#github/google/osv.dev).

<!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzNC4zNy4wIiwidXBkYXRlZEluVmVyIjoiMzQuMzcuMCJ9-->
@amitavaghosh1
Copy link

Thanks @krak3n

@imrenagi
Copy link

imrenagi commented Sep 18, 2023

hi @amitavaghosh1 @johanbrandhorst if I have a HTTP header that is used across all API (not for security), is there any way to use $ref on the endpoint parameter? I cant find how to define the parameters definition yet. Please let me know if you have any pointers. thanks!

something like this in swagger.

parameters:
  shardId:  
    in: header
    name: x-something-something
    required: false
    type: string

paths:
  /users:
    get:
      summary: Gets a list of users.
      parameters:
        - $ref: '#/parameters/shardId'

I was thinking about doing such thing on the operation

service UserService {
  rpc Get(Req) returns (Res) {
    option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_operation) = {
      parameters: {
        headers: {
           { ref: "<reference to shared definition for header>" }
        };
      };
    };
  }
}

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.

Add header annotations to OpenAPIv2 generator
4 participants