grpc-gateway icon indicating copy to clipboard operation
grpc-gateway copied to clipboard

Items type for google.protobuf.ListValue

Open skunkie opened this issue 1 year ago • 5 comments

🐛 Bug Report

If we use google.protobuf.ListValue, the generated swagger spec specifies items type as object:

"listvalueField": {
  "type": "array",
  "items": {
    "type": "object"
  }
},

Swagger-ui considers it an error, if we pass an array, e.g. of strings:

propKeysystemTagserrorindex0errorParameter string value must be valid JSON

The generated json spec contains:

"listvalueField": {
  "type": "array",
  "items": {
    "type": "object"
  }
},

To Reproduce

  1. Create a proto file:
syntax = "proto3";

package my_service;
option go_package = "./pb";

import "google/protobuf/empty.proto";
import "google/protobuf/struct.proto";

message UpdateItem {
  message Request {
    google.protobuf.ListValue listvalue_field = 1;
  }
}

service MyService {
  rpc UpdateA(UpdateA.Request) returns (google.protobuf.Empty) {
    option (google.api.http) = {
      patch: "/v1/item"
      body: "*"
    };
}
  1. Generate swagger spec with protoc.

Expected behavior

There should be a choice to specify the type for items.

Actual Behavior

The items type is taken from wktSchemas: https://github.com/grpc-ecosystem/grpc-gateway/blob/v2.16.0/protoc-gen-openapiv2/internal/genopenapi/template.go#L98-L104

Your Environment

OSX Ventura 13.4.1, Go 1.20, [email protected]

skunkie avatar Jul 21 '23 14:07 skunkie

I might be misunderstanding, but the schema for ListValue is based on the defined JSON representation, which states that it is a JSON array. Theoretically, it could be an array of any JSON value, but because we don't know, we choose the most generic one, object. We have to choose something, so what do you suggest? There's no option to specify a specific type (if you want an array of strings, use repeated string value = 1).

johanbrandhorst avatar Jul 23 '23 05:07 johanbrandhorst

@skunkie recently I made it possible to specify format of items

https://github.com/grpc-ecosystem/grpc-gateway/issues/3403

far4599 avatar Jul 23 '23 10:07 far4599

I might be misunderstanding, but the schema for ListValue is based on the defined JSON representation, which states that it is a JSON array. Theoretically, it could be an array of any JSON value, but because we don't know, we choose the most generic one, object. We have to choose something, so what do you suggest? There's no option to specify a specific type (if you want an array of strings, use repeated string value = 1).

ListValue helps distinguish between nil (the JSON key is not passed) and an empty array (the JSON key is passed with an empty content), while repeated string value = 1 always returns nil.

As for the option, we could add it to JSONSchema.

skunkie avatar Jul 24 '23 10:07 skunkie

@skunkie recently I made it possible to specify format of items

#3403

Does it make sense for uuid format only?

skunkie avatar Jul 24 '23 10:07 skunkie

@johanbrandhorst, @far4599 any updates or comments?

skunkie avatar Aug 15 '23 12:08 skunkie