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.24k stars 2.24k forks source link

google.protobuf.Any generated OpenAPI v2 yaml that is potentially incorrect #4724

Open veqryn opened 2 months ago

veqryn commented 2 months ago

🐛 Bug Report

I believe the generated OpenAPI v2 yaml for a google.protobuf.Any is incorrect. This may also occur on other objects that use additionalProperties: {}

To Reproduce

  1. Have a proto file that includes a google.protobuf.Any somewhere
  2. Generate the OpenAPI v2 yaml
  3. Look at the OpenAPI v2 yaml, try generating OpenAPI client sdk's based on it.

Expected behavior

I expect the generated yaml to look like this:

  protobufAny:
    type: object
    properties:
      '@type':
        type: string
    additionalProperties: true

When turned into OpenAPI client sdk for golang, it would become something like this:

type ProtobufAny struct {
    Type *string `json:"@type"`
    AdditionalProperties map[string]any
}

Actual Behavior

  protobufAny:
    type: object
    properties:
      '@type':
        type: string
    additionalProperties: {}

I believe this is wrong, because additionalProperties: {} can either imply a second level map, or an object without fields. It should be additionalProperties: true

When turned into OpenAPI client sdk for golang, it becomes this:

type ProtobufAny struct {
    Type *string `json:"@type"`
    AdditionalProps map[string]struct{}
}

or

type ProtobufAny struct {
    Type *string `json:"@type"`
    AdditionalProperties map[string]map[string]interface{}
}

In both cases, the client fails on trying to unmarshal into this struct.

Your Environment

v2.22.0 Linux and Mac

johanbrandhorst commented 1 month ago

I don't really understand where the additionalProperties comes from in the first place, the any.proto definition doesn't have that field. In any case, if this is breaking you, I'd be happy to review a PR to fix it. Thanks!