Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !12801

codegen: add `<>` -> 'Not_Equal' to specialCharReplacements

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/Felk/enumvarname_notequal into master Jul 08, 2022
  • Overview 5
  • Commits 1
  • Pipelines 0
  • Changes 1

Created by: Felk

<> is a fairly common alternative to !=, e.g. in SQL or in DevExtreme filters. Previously, if one of an enum's variant was just '<>', its entire name would be sanitized away, resulting in an empty string as symbol name and therefore a syntax error.


I have an OpenAPI schema that looks something like this:

{
  // ...
  "components" : {
    "schemas" : {
      "FilterOp" : {
        "enum" : [ "=", "<>", ">", ">=", "<", "<=", "contains", "notcontains", "startswith", "endswith" ],
        "type" : "string"
      },
      // ...
    }
  }
}

The OpenAPI-Typescript-Generator currently emits the following code:

/**
 * 
 * @export
 * @enum {string}
 */
export enum FilterOp {
    Equal = '=',
     = '<>',
    GreaterThan = '>',
    GreaterThanOrEqualTo = '>=',
    LessThan = '<',
    LessThanOrEqualTo = '<=',
    Contains = 'contains',
    Notcontains = 'notcontains',
    Startswith = 'startswith',
    Endswith = 'endswith'
}

Which has an empty string for the symbol name of the '<>' enum variant and therefore does not compile. This happens because AbstractTypeScriptClientCodegen#toEnumVarName sanitizes invalid characters using sanitizeName, and <> consists of such invalid characters only, resulting in an empty string.

For a fixed set of strings defined in specialCharReplacements this is solved by providing replacement. I opted for adding a replacement for <> there. With that change the following code is emitted instead (Not_Equal gets sanitized to NotEqual):

/**
 * 
 * @export
 * @enum {string}
 */
export enum FilterOp {
    Equal = '=',
    NotEqual = '<>',
    GreaterThan = '>',
    GreaterThanOrEqualTo = '>=',
    LessThan = '<',
    LessThanOrEqualTo = '<=',
    Contains = 'contains',
    Notcontains = 'notcontains',
    Startswith = 'startswith',
    Endswith = 'endswith'
}

This merge request solely fixes a specific usecase I have. It does not fix this issue in general, which is that sanitizeName in DefaultCodegen may always result in an empty string for some inputs. I do not know how that would be best addressed.

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/Felk/enumvarname_notequal