-
-
Notifications
You must be signed in to change notification settings - Fork 6.5k
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
[bug][typescript] Fix node client generator import file paths #7410
[bug][typescript] Fix node client generator import file paths #7410
Conversation
Tagging the most recent contributors to the typescript generator per the contributor list: |
Tagging in a few more TS contributors from the technical commitee to help have a look at this: |
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your contribution! The fix looks reasonable to me (just some minor suggestions).
I'm also wondering if this issue (and the fix) are typescript-node specific, or other typescript generators suffer from the same bug.
@@ -222,8 +223,7 @@ public String toModelImport(String name) { | |||
// Add additional filename information for model imports in the apis | |||
List<Map<String, Object>> imports = (List<Map<String, Object>>) operations.get("imports"); | |||
for (Map<String, Object> im : imports) { | |||
im.put("filename", im.get("import")); | |||
im.put("classname", getModelnameFromModelFilename(im.get("filename").toString())); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks like now the getModelnameFromModelFilename
method is no longer used and can be removed?
Assert.assertEquals(extractedImports.get(0).get("filename"), importName); | ||
} | ||
|
||
private Map<String, Object> postProcessOperationsHelper(String importName) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps just nitpicking - but maybe there could be a better name for this method? Right now it's kind of unclear what its intention is (unless I read the actual method body).
Thanks for the review @amakhrov! Made the suggestions as recommended above.
I'll potentially look at a quick fix for angular next and then attempt to evaluate axios and fetch since those are popular libraries. Another issue for typescript node is: #7440 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
@@ -162,7 +163,7 @@ public String toModelImport(String name) { | |||
return importMapping.get(name); | |||
} | |||
|
|||
return modelPackage() + "/" + camelize(toModelName(name), true); | |||
return DEFAULT_MODEL_IMPORT_DIRECTORY_PREFIX + modelPackage() + "/" + camelize(toModelName(name), true); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mahirk not a blocker on this pr, but wanted to comment since you said you were planning to look at ts implementation in other generators...
since modelPackage is passed by the user, this could result in models//
when passed with trailing slash. It might be best to use StringUtils, something like:
return DEFAULT_MODEL_IMPORT_DIRECTORY_PREFIX +
StringUtils.appendIfMissing(modelPackage(), "/") +
camelize(toModelName(name), true);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
looks good. Just left a small comment if you're looking to do the same in other generators.
was this ever
Did you ever figure out how to get this working with typescript-fetch ? The importMappings still seem to be ignored completely |
Bug and Recap
Issue: Resolves #7385
Method
Below is the map in
operations.get("imports")
based on the import location:Model is created by generator
BYOModel
Therefore the logic was updated to a simpler version, assuming the mapping is created by prior areas.
This resulted in the following import for:
Created Model
BYOModel
Per the above, this fixed the issue of the improper render which led to the bad
classname
i.e. what led to the followingNyPrefixZooStore
The next issue was the extra
../
which is embedded as part of the template. See: https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/typescript-node/api-single.mustache#L8For this purpose, the extra directory listing was remove from the template and added into the
Codegen
itself, where the../
is appended by default ONLY when the model is derived from an auto generated file.After this change here are the mappings in
operations.get("imports")
Model is created by generator
BYOModel
This resulted in the following import for:
Created Model
BYOModel
Testing
Unit
defaultModelImportTest
postProcessOperationsWithModels
Manual
PR checklist
./bin/generate-samples.sh
to update all Petstore samples related to your fix. 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.master