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

fix: numeric enum keys are always prefixed with KEY_ #114

Merged
merged 1 commit into from
Sep 24, 2020

Conversation

vilyus
Copy link
Contributor

@vilyus vilyus commented Sep 21, 2020

relates to #65

Problem: spec definition contained the following schema:

{
  "ReportSchema": {
    "properties": {
      "errorCode": {
        "description": "Error code",
        "enum": [
          "10",
          "20",
          "30",
          "40"
        ],
        "example": "10",
        "maxLength": 3,
        "type": "string"
      }
    }
  }
}

The output was not a valid TS:

export enum ReportSchemaErrorCode {
  '10' = '10',
  '20' = '20',
  '30' = '30',
  '40' = '40'
}

Enum keys in TS cannot be numeric.

The PR#65 fixed one of the 4 places with the enum generation. This PR fixes the other three places.

Output with this PR:

export enum ReportSchemaErrorCode {
  'KEY_10' = '10',
  'KEY_20' = '20',
  'KEY_30' = '30',
  'KEY_40' = '40'
}

@Manweill
Copy link
Owner

The numeric enums always generate type not enum.

@vilyus
Copy link
Contributor Author

vilyus commented Sep 22, 2020

By numeric I mean "enum": ["10", "20", "30"], "type": "string" where enum values happen to start with digits.

Currently rendering the following schema with the master branch produces invalid TS:

{
  "openapi": "3.0.0",
  "info": {
    "title": "Sample",
    "description": "Foobar",
    "version": "0.1"
  },
  "paths": {},
  "components": {
    "schemas": {
      "ReportSchema": {
        "properties": {
          "errorCode": {
            "description": "Error code",
            "enum": [
              "10",
              "20",
              "30",
              "40"
            ],
            "example": "10",
            "maxLength": 3,
            "type": "string"
          }
        }
      }
    }
  }
}

Generated output:

// ... snip
export enum EnumReportSchemaErrorCode {
  '10' = '10',
  '20' = '20',
  '30' = '30',
  '40' = '40'
}

Unfortunately tsc considers such an enum to contain errors: An enum member cannot have a numeric name.

@Manweill
Copy link
Owner

@vilyus in your spec,why your type is not number/int32

@vilyus
Copy link
Contributor Author

vilyus commented Sep 22, 2020

I have a legacy spec that I cannot change. The other failing example would be (I can imagine a scheme like that in a real project, and it cannot be changed to number/int32):

"enum": ["10", "20", "unset"], "type": "string"

@Manweill
Copy link
Owner

I got it.

@Manweill Manweill merged commit 7220bb0 into Manweill:master Sep 24, 2020
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

Successfully merging this pull request may close these issues.

None yet

2 participants