OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.81k stars 6.58k forks source link

[BUG][Protobuf-Schema] Can't convert enum strings #7168

Open ksato9700 opened 4 years ago

ksato9700 commented 4 years ago

Bug Report Checklist

Description

Enum string fields are not converted as I expected. Is there a configuration setting to make it work? It would be nice if you can suggest if if there is, please.

Case 1

input

    PersonData:
      type: object
      properties:
        name:
          type: string
        bloodType:
          type: string
          enum: [A, B, O, AB]

actual output

message PersonData {
  string name = 1;
  enum bloodType {
    A = 0;
    B = 1;
    O = 2;
    AB = 3;
  }
}

expected output

message PersonData {
  string name = 1;
  enum BloodType {
    A = 0;
    B = 1;
    O = 2;
    AB = 3;
  }
  BloodType bloodType = 2;
}

Case2

input

    PersonData2:
      type: object
      properties:
        name:
          type: string
        bloodType:
          $ref: "#/components/schemas/BloodType"

    BloodType:
      type: string
      enum: [A, B, O, AB]

actual output

message BloodType {

}

message PersonData2 {
  string name = 1;
  BloodType bloodType = 2;
}

expected output

enum BloodType {
  A = 0;
  B = 1;
  O = 2;
  AB = 3;
}
message PersonData {
  string name = 1;
  BloodType bloodType = 2;
}
openapi-generator version

openapi-generator-cli:v4.3.

OpenAPI declaration file content or url
openapi: "3.0.0"
info:
  version: 1.0.0
  title: OpenAPI Sample
  license:
    name: MIT
servers:
  - url: http://localhost/
paths:
  /person:
    get:
      summary: Get Person Data
      operationId: getPerson
      responses:
        "200":
          description: Person Data
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/PersonData"
  /person2:
    get:
      summary: Get Person Data (2)
      operationId: getPerson2
      responses:
        "200":
          description: Person Data
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/PersonData2"

components:
  schemas:
    PersonData:
      type: object
      properties:
        name:
          type: string
        bloodType:
          type: string
          enum: [A, B, O, AB]

    PersonData2:
      type: object
      properties:
        name:
          type: string
        bloodType:
          $ref: "#/components/schemas/BloodType"

    BloodType:
      type: string
      enum: [A, B, O, AB]
Generation Details
docker run --rm -v /spec:/local openapitools/openapi-generator-cli:v4.3.1 generate \
        -i /local/sample.yml \
        -g protobuf-schema \
        -o /local/sample
Steps to reproduce

run the above command

Related issues/PRs
Suggest a fix
wing328 commented 4 years ago

Can you try the latest master as I believe there was fixed already?

wing328 commented 4 years ago

Related PR: https://github.com/OpenAPITools/openapi-generator/pull/7073

ksato9700 commented 4 years ago

@wing328 Thank you for the information. I should have checked not only issues but also PRs. Sorry.

Now, I tried with the latest version and the Case 1 works as I expected! Unfortunately, Case2 still doesn't work, generating an empty message definition for the enum type. Are there any other suggestions, please?

wing328 commented 4 years ago

For case 2, please use inline schema as a workaround for the time being.

ksato9700 commented 4 years ago

For case 2, please use inline schema as a workaround for the time being.

Yes, I will. Thank you. At the same time, we occasionally define an enum type as a common schema, so that it can be referred from other schemas. Hopefully, it will be supported in the future.

danieljimenezr commented 2 years ago

Hi, I am having the same issue here and trying the suggestion that @wing328 for case 2 I have the same result: an empty enum. Here's my case

OpenAPI definition

  BusinessesActionsRequest:
      type: object
      properties:
        actions:
          type: array
          items:
            $ref: '#/components/schemas/BusinessesActionEnum'
        name:
          type: string
        classification:
          type: string
        sourceYears:
          $ref: '#/components/schemas/SourceYears'
      description: Describes the input criteria to match a person or a person at an address.

    BusinessesActionEnum:
      type: string
      enum: [ACTIONS_LIST, COMPANY_COUNTY_COURT_JUDGEMENT, BUSINESS_MATCH, DIRECTORSHIPS]
      default: ACTIONS_LIST

Protobuf files generated:

// business_actions_request.proto

syntax = "proto3";

package ***;

import public "models/businesses_action_enum.proto";

message BusinessesActionsRequest {

  repeated BusinessesActionEnum actions = 88061701;

  string name = 3373707;

  string classification = 382350310;

  // Years when the underlying dataset were issued. This could support compliance requirements.
  repeated int32 sourceYears = 90636165;

}
// business_action_enum.proto

syntax = "proto3";

package ***;

message BusinessesActionEnum {

}

We have updated to latest openapi generator version but problem is still there. Any clue of what's happening? Thank you!