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

[Feature] Support enumNames-like setting to generate enum with custom names #96

Closed
songzhi opened this issue Jan 23, 2020 · 7 comments
Closed
Assignees

Comments

@songzhi
Copy link

songzhi commented Jan 23, 2020

There's basically tow options.The first is use another field likeenumNames:string[] in OpenApiSchema to generate enums with custom names.
Schema like this:

"state": {
    "title": "State",
    "enum": [
        0,
        1,
        2
    ],
    "type": "integer",
    "default": 0,
    "enumNames": ["initial", "working", "finished"]
}

Generated code:

enum state {
  INITIAL = 0,
  WORKING = 1,
  FINISHED = 2
}

But enumNames is not a valid openapi schema field.
Another option is expanding the getEnumFromDescription.

@ferdikoomen
Copy link
Owner

there is a nice thread here OAI/OpenAPI-Specification#681 some client support the x-enum-varnames. The x- props are extension props in the V3 spec. That might be a semi-supported way to do it.

@ferdikoomen
Copy link
Owner

The code is not hard, I will have a look soon!

@ferdikoomen ferdikoomen self-assigned this Feb 11, 2020
@ferdikoomen
Copy link
Owner

@songzhi would the following SPEC work for you?

Example with x-enum-varnames and x-enum-descriptions

{
    "EnumWithStrings": {
        "description": "This is a simple enum with strings",
        "enum": [
            0,
            1,
            2
        ],
        "x-enum-varnames": [
            "Success",
            "Warning"
            "Error"
        ],
        "x-enum-descriptions": [
            "Used when the status of something is successful",
            "Used when the status of something has a warning"
            "Used when the status of something has an error"
        ]
    }
}

@songzhi
Copy link
Author

songzhi commented Feb 14, 2020

@ferdikoomen It's works for me. Thanks for this.

@ferdikoomen
Copy link
Owner

Alright, i will try to implement this in the upcoming days. Keep you posted

@ferdikoomen
Copy link
Owner

I pushed some changes to the master branch, did not publish a new version yet. Feel free to have a look, i will do some final testing tomorrow.

@ferdikoomen
Copy link
Owner

ferdikoomen commented Feb 22, 2020

New version has been pushed (0.1.16) with support for enums

@songzhi songzhi closed this as completed Feb 22, 2020
lzurbriggen pushed a commit to jshmrtn/openapi-typescript-codegen that referenced this issue Mar 20, 2024
fix(config): set lint to false by default
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

2 participants