Skip to content

codegen: add <> -> 'Not_Equal' to specialCharReplacements#12801

Merged
wing328 merged 1 commit intoOpenAPITools:masterfrom
Felk:enumvarname_notequal
Jul 11, 2022
Merged

codegen: add <> -> 'Not_Equal' to specialCharReplacements#12801
wing328 merged 1 commit intoOpenAPITools:masterfrom
Felk:enumvarname_notequal

Conversation

@Felk
Copy link
Copy Markdown
Contributor

@Felk Felk commented Jul 8, 2022

<> 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.

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jul 9, 2022

Thanks for the PR but your commit (as shown in the Commits tab) is not linked to your Github account, which means this PR won't count as your contribution in https://github.com/OpenAPITools/openapi-generator/graphs/contributors.

Let me know if you need help fixing it.

Ref: https://github.com/OpenAPITools/openapi-generator/wiki/FAQ#how-can-i-update-commits-that-are-not-linked-to-my-github-account

@Felk Felk force-pushed the enumvarname_notequal branch from 506fd03 to 310f2a3 Compare July 9, 2022 21:50
This 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.
@Felk Felk force-pushed the enumvarname_notequal branch from 310f2a3 to 10f7205 Compare July 9, 2022 21:58
@Felk
Copy link
Copy Markdown
Contributor Author

Felk commented Jul 9, 2022

yeah my bad, I fixed the author metadata

@wing328
Copy link
Copy Markdown
Member

wing328 commented Jul 11, 2022

LGTM. Will update samples after merging.

@wing328 wing328 merged commit 9122b2a into OpenAPITools:master Jul 11, 2022
@wing328
Copy link
Copy Markdown
Member

wing328 commented Jul 11, 2022

Samples updated via 244a459

@Felk
Copy link
Copy Markdown
Contributor Author

Felk commented Jul 11, 2022

nice, thank you

@Felk Felk deleted the enumvarname_notequal branch July 11, 2022 16:17
wing328 pushed a commit that referenced this pull request Jul 10, 2024
* codegen: add == -> 'Double_Equal' to specialCharReplacements

The double equal '==' is a common operator in a few contexts (specific use case for me is haystack operators). Currently if this value appears in an enum its name gets sanitized to empty and generates invalid syntax. Very similar to #12801

* makes java underscore test more flexible

Given the name and purpose of this test, maybe it is better to test that the generated value is not an underscore rather than to test that it _is_ a specific (and possibly arbitrary) substitute value.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants