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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Required properties of message types get optional in OpenAPI #2837

Closed
OnkelTem opened this issue Aug 5, 2022 · 9 comments 路 Fixed by #2904
Closed

Required properties of message types get optional in OpenAPI #2837

OnkelTem opened this issue Aug 5, 2022 · 9 comments 路 Fixed by #2904

Comments

@OnkelTem
Copy link

OnkelTem commented Aug 5, 2022

馃悰 Bug Report

Message properties marked with [(google.api.field_behavior) = REQUIRED] get ignored when generating OpenAPI spec, if properties reference other messages (schemas).

To Reproduce

Create a proto with the following definition:

message Vendor {
  uint32 id = 1 [(google.api.field_behavior) = REQUIRED];
}
message CreateVendorRequest {
  Vendor vendor = 1 [(google.api.field_behavior) = REQUIRED];
}

And generate anOpenApi spec. It will contain:

    "CreateVendorRequest": {
      "type": "object",
      "properties": {
        "vendor": {
          "$ref": "#/definitions/Vendor"
        }
      }
    },

i.e. the required: [ "vendor" ] part is missed.

Expected behavior

    "CreateVendorRequest": {
      "type": "object",
      "properties": {
        "vendor": {
          "$ref": "#/definitions/Vendor"
        }
      },
      "required": [
        "vendor"
      ]
    },

Actual Behavior

The required part is missed.

Your Environment

Linux
2.11.1

@johanbrandhorst
Copy link
Collaborator

Thanks for your issue. This is only when referencing messages that are imported, is that correct?

@OnkelTem
Copy link
Author

OnkelTem commented Aug 5, 2022

Yep, scalars and "inlined" arrays (not in other schemas) seem to get processed correctly.

@johanbrandhorst
Copy link
Collaborator

Ok, thanks for confirming. Would you be interested in contributing a fix for this? The API fields configuration feature is still a bit new so there will be bugs like this (see also #2829).

@OnkelTem
Copy link
Author

OnkelTem commented Aug 5, 2022

@johanbrandhorst Thanks for the quick replies. I'm not qualified enough for that work, unfortunately, my field is JavaScript. But I'll let my teammates know about such an opportunity :)

UPD. One of my colleagues told me that it could sound like banter, but actually no. I really meant what I said.

@tmccnnll
Copy link

Yep, scalars and "inlined" arrays (not in other schemas) seem to get processed correctly.

I think scalars are actually incorrectly handled but this is covered by issue #2635

@OnkelTem
Copy link
Author

OnkelTem commented Sep 9, 2022

I also noticed that if a property is of an array message type, it gets rendered as required, e.g.:

export interface Pbv1ActionReasonsResponseV1 {
  reasons: Pbv1ActionReason[]
}

but if I remove repeated, then it becomes non-required:

export interface Pbv1ActionReasonsResponseV1 {
  reason?: Pbv1ActionReason
}

@Rajarshi1998
Copy link

I would like to contribute to fix this issue. Thank you!!

@johanbrandhorst
Copy link
Collaborator

Feel free to start work on this. The first step is usually adding a failing test or example.

@gostajonasson
Copy link
Contributor

gostajonasson commented Sep 27, 2022

I created a small and naive PR for this but it works as far as I can see.
Happy for review by a maintainer.
(I'll be filling out the CLA soon.)

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

Successfully merging a pull request may close this issue.

5 participants