grpc-ecosystem / grpc-gateway

gRPC to JSON proxy generator following the gRPC HTTP spec

Home Page:https://grpc-ecosystem.github.io/grpc-gateway/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

omit-enum-default-value Generates Invalid Spec for Enums with a Single Value

jeromefroe opened this issue Β· comments

πŸ› Bug Report

When the omit-enum-default-value option is enabled it will set the enum field for an enum with just just a single value to null which is invalid against swagger specification 2.0.

To Reproduce

I ran into this issue with a protobuf file that imports google/protobuf/struct.proto. The struct.proto file defines a NullValue type that has just a single value, NULL_VALUE and the generated Swagger definition for the field is invalid. The generated Swagger spec for the following example file will produce the invalid spec:

syntax = "proto3";

package example;

option go_package = "example";

import "google/protobuf/struct.proto";
import "google/api/annotations.proto";

message GetExampleRequest {
  google.protobuf.Struct struct_value = 1;
}

message GetExampleResponse {}

service ExampleService {
  rpc GetExample(GetExampleRequest) returns (GetExampleResponse) {
    option (google.api.http) = {
      get: "/example"
    };
  };
}

The generated spec for the NullValue type, with the invalid enum field, is:

"protobufNullValue": {
  "type": "string",
  "enum": null,
  "description": "`NullValue` is a singleton enumeration to represent the null value for the\n`Value` type union.\n\n The JSON representation for `NullValue` is JSON `null`."
}

Expected behavior

To avoid generating an invalid spec the generator could omit the enum field or for types with just a single value. From what I can tell this was the behavior of the generator prior to #3167 but the refactoring in that commit changed the behavior because the Enum field was changed to an interface and when the omit-enum-default-value option removes the single value the field is a nil slice but not a nil interface since it is typed and therefore the omitempty option field does not omit it.

Alternatively, the omit-enum-default-value option could skip enum types with just a single (this is admittedly an edge case) or there could be a per-type option to configure omit-enum-default-value (I ran into this issue because for all other enums types in the proto file I'm using have more than a single value and for those the behavior of omit-enum-default-value is exactly what I need).

Actual Behavior

The Swagger spec generated is invalid because the enum field is set to nil.

Your Environment

I'm using grpc-gateway on Linux and MacOS with version v2.19.1 though as noted above I think this issue was introduced in 2.16.0 with #3167.

Hi! πŸ‘‹ I opened #4180 as a starting point for addressing this issue.