grpc-gateway
grpc-gateway copied to clipboard
openapiv2: Field options are not properly rendered for repeated fields
I need to generate swagger json using proto annotations. generated schema should look like this
"schema": {
"type": "object",
"example": {
"product_id": ["8900099100000113079", "8900099100000082373"]
},
"properties": {
"product_id": {
"type": "array",
"items": {
"type": "string",
"maxLength": 19,
"minLength": 1,
"pattern": "^[0-9]+$"
},
"description": "Only digits are allowed.",
"title": "Provide list of ids"
}
}
}
and the annotation added was something like this
message product {
repeated string product_id=1[(grpc.gateway.protoc_gen_openapiv2.options.openapiv2_field) = {
pattern: "^[0-9]+$"
max_length: 19
min_length: 1
description: "Only digits are allowed."
}];
}
But the problem is above annotation is generating schema like this
"schema": {
"type": "object",
"example": {
"product_id": ["8900099100000113079", "8900099100000082373"]
},
"properties": {
"product_id": {
"type": "array",
"items": {
"type": "string"
},
"description": "Only digits are allowed.",
"title": "Provide list of ids",
"maxLength": 19,
"minLength": 1,
"pattern": "^[0-9]+$"
}
}
}
which is not correct. We could not find the relevant example in a_bit_of_everything.proto.
Please help in adding the annotations.
Hi! I went and looked at the OpenAPIv2 spec and it seems you can specify the field properties both at the top level and inside the items
object: https://swagger.io/specification/v2/#items-object. I think we should probably put it inside the items if the specified field is repeated. Would you be willing to help contribute a fix for this?
The easiest way to get started would be to add a new test to https://github.com/grpc-ecosystem/grpc-gateway/blob/master/protoc-gen-openapiv2/internal/genopenapi/template_test.go that would have the expected behavior. Having had a quick glance at the code, I think you'll need to make the changes here: https://github.com/grpc-ecosystem/grpc-gateway/blob/bfda18d44d7458645d5753e9faf16dc9ccd1db41/protoc-gen-openapiv2/internal/genopenapi/template.go#L398, but I could be wrong.
/assign @johanbrandhorst i am willing to contribute on this kindly assign it to me
Hi @lakshkeswani, please have at it!
We're dealing with similar issue. Our proto file:
import "google/protobuf/field_mask.proto";
message SomeMessage {
google.protobuf.FieldMask update_mask = 10;
}
(Here's the definition of FieldMask:)
// google/protobuf/field_mask.proto
message FieldMask {
// The set of field mask paths.
repeated string paths = 1;
}
is generated into openapiv2 spec as:
"parameters": [
{
"name": "updateMask",
"in": "query",
"type": "string"
}
]
The issue is that FieldMask should be generated into OpenAPI as a "type": "array"
(expected) but it is generated as a "type": "string"
(actual).
This sounds like a separate issue, could you create a separate issue and share your RPC definition too please?
@johanbrandhorst I have created a separate issue #2580 with reproducible example.