Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid enum var names are generated by multi-byte characters #893

Closed
osjupiter opened this issue Aug 24, 2018 · 5 comments
Closed

Invalid enum var names are generated by multi-byte characters #893

osjupiter opened this issue Aug 24, 2018 · 5 comments

Comments

@osjupiter
Copy link
Contributor

osjupiter commented Aug 24, 2018

Description

Hi. I have been using openapi-generator with definitions which contains multi-byte characters.
When I wrote enum definitions which names contain only non-word characters(RegExp \W), invalid code generated.
(All of enum names become "_".)

Are double-byte characters not allowed for enum names in OAS ?
If it is valid, I hope these characters will be supported.

generated code

java

@JsonAdapter(PetType.Adapter.class)
public enum PetType {
  _("猫"), // cat   --compile error
  _("犬"); // dog

go

type PetType string
// List of PetType
const (
	 PetType = "猫" // compile error
	 PetType = "犬"
)
openapi-generator version

3.2.3-SNAPSHOT

OpenAPI declaration file content or url
swagger: "2.0"
info:
  title: "example"
  version: "1.0"
paths:
  /pet:
    post:
      parameters:
        - in: body
          name: data
          schema:
            type: object
            properties:
              animal:
                $ref: "#/definitions/PetType"
      responses:
        "200":
          description: ok
definitions:
  PetType:
    type: string
    enum:
      - 
      - 
    x-enum-names:
      - Cat
      - Dog
Command line used for generation
java -jar openapi-generator-cli-3.2.3-20180824.092221-7.jar generate  -i sample.yaml -g java -o dest
Steps to reproduce

just generated

Related issues/PRs

I could not find it.

Suggest a fix/enhancement

I always create a customized config class and add x-enum-names extension by overriding postProcessModelsEnum and updateCodegenPropertyEnum like this.

I think it is not good to positively use multi-byte characters as an api parameter.
So it is enough to be able to use it by extension.

Thanks.

@wing328
Copy link
Member

wing328 commented Aug 24, 2018

Are double-byte characters not allowed for enum names in OAS ?

Double-byte characters are allowed in enum names in OAS v2, v3.

I always create a customized config class and add x-enum-names extension by overriding postProcessModelsEnum and updateCodegenPropertyEnum like this.

@osjupiter thanks for the details in reporting the issue. I agree with you the existing solution we'd in place won't meet your requirement and I like your idea of using extensions to control the naming of the enum parameter name.

Would you have time to contribute a PR to fix the Go or Java client generator as a starting point?

(It's late night in Japan so let's resume the conversation over the weekend or on Monday. Have a nice weekend!)

@osjupiter
Copy link
Contributor Author

Thanks for your agreement!
I'll send a PR.

@osjupiter
Copy link
Contributor Author

I send PR!
I changed the extension name to "x-enum-varnames".

With this extension, users will be able to control names of enum property for more diverse purposes.

exmaple

      EnumProp:
        type: string
        enum:
          - "1"
          - "2"
        x-enum-varnames:
          - ONE
          - TWO

gererates

  public enum EnumPropEnum {
    ONE("1"),  // without extension : _1
    
    TWO("2"); // without extension : _2

Note that this feature affects output of all languages.

@jmini
Copy link
Member

jmini commented Aug 30, 2018

PR #917 is merged

@akhileshnair08
Copy link

akhileshnair08 commented Aug 4, 2020

ABCEnum:
enum:
-{0,"aum"}
-{1,"gautham"}
-{2,"Buddha"}
x-enum-varnames:
- AUM
- GAUTHAM
- BUDDHA
type: object
properties:
num_value:
type: integer
format: int32
str_value:
type: string

Why is this not working???
getting below error:
unexpected error in Open-API generation (org.openapitools:openapi-generator-maven-plugin:4.1.3:generate:default:generate-sources)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants