Skip to content

[TypeScript-Consolidated] Fixes invalid type enum array & incorrect discriminator value if mapping is present #15272

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

Open
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

TiFu
Copy link
Contributor

@TiFu TiFu commented Apr 20, 2023

See #14892

Still WIP - will expand on description once ready.

@wtrocki Code now compiles for the spec that you had posted; Will have CI run the tests against the existing specs in OpenAPI Generator to see whether I broke anything else.

CC Technical Committee
@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    Commit all changed files.
    This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
    These must match the expectations made by your contribution.
    You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./bin/generate-samples.sh bin/configs/java*.
    For Windows users, please run the script in Git BASH.
  • In case you are adding a new generator, run the following additional script :
    ./bin/utils/ensure-up-to-date.sh
    
    Commit all changed files.
  • File the PR against the correct branch: master (6.3.0) (minor release - breaking changes with fallbacks), 7.0.x (breaking changes without fallbacks)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@TiFu TiFu marked this pull request as draft April 20, 2023 18:40
@@ -293,6 +293,40 @@ public String toEnumValue(String value, String datatype) {
}
}

/**
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TODO: Extract to AbstractTSClientCodegen

* In these cases, we then have Array <{ [key: EnumName]: EnumName}> - which is invalid typescript
* To protect agains this we first replace [key: string] with a special/reserved placeholder (i.e. *[key]* )
*/
property.datatypeWithEnum = property.datatypeWithEnum.replace("[key: string]", "*key*")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

*key*

can be constant with PLACEHOLDER name.

#PLACEHOLDER# or something else.

Copy link
Contributor

@wtrocki wtrocki Apr 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually using regular expression with replaceAll might help avoiding placeholder. We always looking to replace last occurence

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it always the last occurrence?

Wondering whether there are more complicated examples where this does not hold - mainly thinking about cases where the same type appears twice (e.g., Tuple<string, string> to Tuple<FancyEnum, FancyEnum>).

In cases like this, our model is anyway broken because we lose type information when generating the initial type.

@wtrocki
Copy link
Contributor

wtrocki commented Apr 20, 2023

Precompiled. It does solve the issue with array.

@TiFu TiFu linked an issue Apr 20, 2023 that may be closed by this pull request
6 tasks
@TiFu TiFu marked this pull request as ready for review April 29, 2023 08:17
@TiFu
Copy link
Contributor Author

TiFu commented Apr 29, 2023

Technical committee: Would appreciate your review/input :)

@TiFu @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov @davidgamero @mkusaka

@bodograumann
Copy link
Contributor

Sounds like maybe a unit test makes sense here!?
That would also help me better understand the issue here.

I would definitely prefer a proper fix when generating the type instead of a workaround with string replacements.

@TiFu
Copy link
Contributor Author

TiFu commented May 28, 2023

Agreed, let me add some unit test.

If I remember well (few weeks ago when I worked on this), I initially tried going for a proper fix but discovered that the function where the initial type is set does not have the full context necessary to set the right type from the start
Let me have another look at this and provide a bit more data-based rationale :)

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.

[BUG][Typescript] Invalid type for the enum array
3 participants