Skip to content

fix: required properties of message type are required in OpenAPI #2904

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

Conversation

gostajonasson
Copy link
Contributor

Properties of message type having the annotation [(google.api.field_behavior) = REQUIRED] are not marked as required in the generated OpenAPI specification.
This PR fixes that.

A maintainer should sanity check this PR since it naively removes logic someone added on purpose linking to a "JSON Reference syntax".

Fixes #2837

@google-cla
Copy link

google-cla bot commented Sep 26, 2022

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@keyz
Copy link
Contributor

keyz commented Sep 28, 2022

Would love to see this merged!

@gostajonasson -- I added a few tests (in my fork) to verify your change, and I think you have the right fix.

  • keyz@91060e6: before applying the fix, required annotations are missing
  • keyz@deab66e: after applying the fix, openapi files seems legit with no regressions

Feel free to copy anything from my fork if it helps to get your PR merged :)

@johanbrandhorst
Copy link
Collaborator

Huh, this is a surprisingly small fix. I can't say I know why that check was there. @MakDon what do you think about the changes? You've contributed a fix in this space recently.

@MakDon
Copy link
Contributor

MakDon commented Sep 28, 2022

Huh, this is a surprisingly small fix. I can't say I know why that check was there. @MakDon what do you think about the changes? You've contributed a fix in this space recently.

I will take a look at it ASAP :D

@gostajonasson
Copy link
Contributor Author

Thanks @keyz!
@johanbrandhorst @MakDon just tell me if you want me to add the tests from @keyz to this PR.

@MakDon
Copy link
Contributor

MakDon commented Sep 28, 2022

It seems that this solution would cause another issue, see #1937 AND #1944
I am trying to confirm if this PR would break the generation again.

IMO, it is better to add the tests from @keyz
I am trying to confirm why the test TestSchemaOfField is not broken when running the CI

@johanbrandhorst
Copy link
Collaborator

More tests shouldn't hurt, so please feel free to add them here. Thanks @MakDon for taking a look!

@gostajonasson
Copy link
Contributor Author

It seems that this solution would cause another issue, see #1937 AND #1944

Yes you are correct @MakDon, this PR is naive and I need help fixing it.
This PR would cause message types marked with the OUTPUT_ONLY field behaviour as read only like this:

  Location pickup_location = 1 [(google.api.field_behavior) = OUTPUT_ONLY];
    properties:
      pickupLocation:
        $ref: '#/definitions/Location'
        readOnly: true

Which isn't recommended since other members than $ref should be ignored per the JSON Reference spec.

But I'm also seeing that the description field is added (somewhere else in the code) next to $ref members so that's an existing bug.

Personally I think that the best fix is outside of the schemaOfField function and not output any members next to $ref (if openapiSchemaObject.Ref is set) in the final OpenAPI spec.
What I mean is that the schemaOfField function should not be responsible for filtering out fields just because it is of message type. That should a renderer do since it's OK to list the required fields outside of the property:

  Shipment:
    type: object
    properties:
      pickupLocation:
        $ref: '#/definitions/Location'
    description: A description.
    required:
      - pickupLocation

@gostajonasson gostajonasson force-pushed the make_required_messages_required_in_openapi branch from 5870b53 to d3eab36 Compare October 1, 2022 21:35
@gostajonasson
Copy link
Contributor Author

Pushed a fix for the bug that @MakDon mentioned my initial fix would cause.
Right now this PR fixes all bugs for my use case.
Will add the tests from @keyz later.

@johanbrandhorst
Copy link
Collaborator

Hi @gostajonasson, thanks again for working on this, do you think you'll bring this over the line in the next week or so? I'm just planning out the next release timing. Thanks!

@gostajonasson
Copy link
Contributor Author

gostajonasson commented Oct 12, 2022

Hi @gostajonasson, thanks again for working on this, do you think you'll bring this over the line in the next week or so? I'm just planning out the next release timing. Thanks!

@johanbrandhorst Yes I hope so! Sorry for the delay.

@gostajonasson
Copy link
Contributor Author

@keyz Do you think I should add everything from the two commits you linked to?
I'm not familiar with this repo and don't know exactly what to copy to test the fix in this PR.

@keyz
Copy link
Contributor

keyz commented Oct 12, 2022

@gostajonasson you can:

  1. start with copying what I did in examples/internal/proto/examplepb/a_bit_of_everything.proto (commit keyz@91060e6).
  2. after that, follow CONTRIBUTING.md to regenerate the fixtures
  3. you might need to tweak examples/internal/clients/abe/BUILD.bazel, but my commits (+ keyz@deab66e) should contain everything you need

@gostajonasson gostajonasson force-pushed the make_required_messages_required_in_openapi branch 2 times, most recently from 61a3894 to 1547c87 Compare October 13, 2022 17:54
@gostajonasson
Copy link
Contributor Author

@keyz Thanks for the instructions! I've followed them and pushed the changes. Happy for a review and a workflow run. 🤗

@gostajonasson gostajonasson force-pushed the make_required_messages_required_in_openapi branch from 1547c87 to ca03173 Compare October 13, 2022 18:01
@gostajonasson gostajonasson force-pushed the make_required_messages_required_in_openapi branch from ca03173 to 81c7453 Compare October 13, 2022 18:09
@gostajonasson gostajonasson force-pushed the make_required_messages_required_in_openapi branch from 81c7453 to db42d76 Compare October 13, 2022 18:46
@johanbrandhorst johanbrandhorst merged commit e3b2511 into grpc-ecosystem:master Oct 13, 2022
@johanbrandhorst
Copy link
Collaborator

Thanks for your contribution!

@johanbrandhorst
Copy link
Collaborator

another-rex referenced this pull request in google/osv.dev Nov 4, 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.11.3` -> `v2.13.0` |

---

### Release Notes

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

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

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

#### What's Changed

- Updated gRPC code Cancelled replaced with HTTP code 499 by
[@&#8203;tech-sumit](https://togithub.com/tech-sumit) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2957](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2957)
- fix: remove default service tag generation for methods by
[@&#8203;kkolur](https://togithub.com/kkolur) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2960](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2960)
- Use tag instead of has pin for SLSA generator by
[@&#8203;laurentsimon](https://togithub.com/laurentsimon) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2969](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2969)
- Add Conduit to adopters by
[@&#8203;hariso](https://togithub.com/hariso) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2981](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2981)
- feat(gen-openapiv2): support trailing comments by
[@&#8203;ionling](https://togithub.com/ionling) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2965](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2965)
- feat(gen-openapiv2): keep fields next to "$ref" fields by
[@&#8203;gostajonasson](https://togithub.com/gostajonasson) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2986](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2986)

#### New Contributors

- [@&#8203;tech-sumit](https://togithub.com/tech-sumit) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2957](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2957)
- [@&#8203;hariso](https://togithub.com/hariso) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2981](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2981)
- [@&#8203;ionling](https://togithub.com/ionling) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2965](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2965)

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

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

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

#### What's Changed

- fix: support for oneof fields in request bodies by
[@&#8203;isbang](https://togithub.com/isbang) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2867](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2867)
- mux: calculate verb correctly for cases like DELETE /foo/bar:archive
when user provided wrong method by
[@&#8203;jonathaningram](https://togithub.com/jonathaningram) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2870](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2870)
- Update googleapis dependency by
[@&#8203;johanbrandhorst](https://togithub.com/johanbrandhorst) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2875](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2875)
- protoc-gen-openapiv2: RPC visibility setting transitively applied to
messages by [@&#8203;erademacher](https://togithub.com/erademacher) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2880](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2880)
- protoc-gen-openapiv2: Use json_name when generating required field
names by [@&#8203;patrick246](https://togithub.com/patrick246) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2885](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2885)
- fix: support service tags in OpenAPI config file
([#&#8203;2817](https://togithub.com/grpc-ecosystem/grpc-gateway/issues/2817))
by [@&#8203;y-takuya](https://togithub.com/y-takuya) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2858](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2858)
- feat: add option to disable rendering of service tags by
[@&#8203;kkolur](https://togithub.com/kkolur) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2928](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2928)
- fix: required properties of message type are required in OpenAPI by
[@&#8203;gostajonasson](https://togithub.com/gostajonasson) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2904](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2904)
- feat: add option to add description to tags by
[@&#8203;same-id](https://togithub.com/same-id) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2939](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2939)
- add extensions for Tag object by
[@&#8203;kkolur](https://togithub.com/kkolur) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2950](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2950)
- Make registry load packages deterministically by
[@&#8203;gonzaloserrano](https://togithub.com/gonzaloserrano) in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2945](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2945)

#### New Contributors

- [@&#8203;erademacher](https://togithub.com/erademacher) made their
first contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2880](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2880)
- [@&#8203;patrick246](https://togithub.com/patrick246) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2885](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2885)
- [@&#8203;y-takuya](https://togithub.com/y-takuya) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2858](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2858)
- [@&#8203;kkolur](https://togithub.com/kkolur) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2928](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2928)
- [@&#8203;gostajonasson](https://togithub.com/gostajonasson) made their
first contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2904](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2904)
- [@&#8203;same-id](https://togithub.com/same-id) made their first
contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2939](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2939)
- [@&#8203;gonzaloserrano](https://togithub.com/gonzaloserrano) made
their first contribution in
[https://github.com/grpc-ecosystem/grpc-gateway/pull/2945](https://togithub.com/grpc-ecosystem/grpc-gateway/pull/2945)

**Full Changelog**:
grpc-ecosystem/grpc-gateway@v2.11.3...v2.12.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:eyJjcmVhdGVkSW5WZXIiOiIzMi4yNDEuOSIsInVwZGF0ZWRJblZlciI6IjM0LjEyLjAifQ==-->

Co-authored-by: Rex P <[email protected]>
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.

Required properties of message types get optional in OpenAPI
4 participants