grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec
https://grpc-ecosystem.github.io/grpc-gateway/
BSD 3-Clause "New" or "Revised" License
18.05k stars 2.23k forks source link

Document about how to mark an arrray field as required in an extra openapi config file? #3806

Open mousimin opened 9 months ago

mousimin commented 9 months ago

📚 Documentation

I have a simple proto file:

message GetTokenRequest {
  string hostname = 1 [(buf.validate.field).required = true, (buf.validate.field).string.hostname = true ];
  repeated string buckets = 2 [(buf.validate.field).repeated.min_items = 1];;
}

And I want to add an extra openapi file to help to generate the swagger file:

openapiOptions:
  field:
    - field: api.user.GetTokenRequest.hostname
      option:
        required: ["hostname"]
    - field: api.user.GetTokenRequest.buckets
      option:
        required: ["buckets"]

After run "buf generate proto" command, I found the hostname field was marked as required, but the buckets field was not, do we have a document which describes how to config an array field as required? The generated swagger file:

"parameters": [
  {
    "name": "hostname",
    "description": "hostname is the name of which you want to acquire join token",
    "in": "query",
    "required": true,
    "type": "string"
  },
  {
    "name": "buckets",
    "description": "bucket is the name for the cos-agent instance using this token sneds telemetry data to",
    "in": "query",
    "required": false,
    "type": "array",
    "items": {
      "type": "string",
      "required": [
        "buckets"
      ]
    },
    "collectionFormat": "multi"
  }
]
johanbrandhorst commented 9 months ago

Hm, I don't think so. There might be a bug here, but I'd appreciate if you could dig around a bit to find out. Perhaps this could be a doc contribution or a bug fix, or both :).

rgaskill commented 6 days ago

For anyone else that lands here, one way to work around this is to specify a field behavior. updated example from above:

message GetTokenRequest {
  string hostname = 1 [(buf.validate.field).required = true, (buf.validate.field).string.hostname = true ];
  repeated string buckets = 2  [(google.api.field_behavior) = REQUIRED];
}