nytimes / openapi2proto

A tool for generating Protobuf v3 schemas and gRPC service definitions from OpenAPI specifications
Apache License 2.0
964 stars 98 forks source link

Enums of any value type (specifically booleans) should be accepted #112

Open aSapien opened 5 years ago

aSapien commented 5 years ago

Reproduction steps:

  1. Download Slack's Swagger API json here:
    curl -O https://api.slack.com/specs/openapi/v2/slack_web.json
  2. Attempt to generate:
    openapi2proto -spec ./slack_web.json
  3. Result: error: failed to transpile: failed to load OpenAPI spec: failed to decode content: failed to unmarshal JSON: json: cannot unmarshal bool into Go struct field .enum of type string

Unfortunately I have little to no experience with Golang, so I'm pretty much clueless :/

Appreciate any help here

jprobinson commented 5 years ago

The root of the error message is the key here:

cannot unmarshal bool into Go struct field .enum of type string

This points to a field named "enum" that has a boolean value instead of an expected string. It appears that the kind folks of Slack decided they needed to declare additional enum values/constants that reattempt to define their own custom "true" and "false" constants as booleans.

        "defs_ok_false": {
            "enum": [
                false
            ],
            "title": "default failure response",
            "type": "boolean"
        },
        "defs_ok_true": {
            "enum": [
                true
            ],
            "title": "default success response",
            "type": "boolean"
        },

This is kinda nutty IMO but it appears to match the OpenAPI 2.0 spec that an enum can be of "any" type.

Right now, openapi2proto expects enums to have strings as values, which is causing the error. I hope you don't mind, but I'll rename this issue to better describe the problem at hand.

aSapien commented 5 years ago

Thanks @jprobinson! Indeed it looks like their API is a mess. I've been trying to generate Slack's entities for quite some time. Unfortunately, openapi-generator also fails to generate data classes

I really hope somebody there is listening and the issue will be addressed