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(NestJS): use correct typing for default headers #10616

Merged
merged 3 commits into from
Oct 21, 2021

Conversation

bilalshaikh42
Copy link
Contributor

@bilalshaikh42 bilalshaikh42 commented Oct 17, 2021

closes #10615

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.
  • File the PR against the correct branch: master (5.3.0), 6.0.x
  • 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 @taxpon @sebastianhaas @kenisteward @Vrolijkx @macjohnny @topce @akehir @petejohansonxo @amakhrov

Copy link
Contributor

@amakhrov amakhrov left a 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 good.

I noticed that later in the service class there is a bug related to defaultHeaders:

let headers = this.defaultHeaders;
...
if ({{paramName}}) {
  headers['{{baseName}}'] = {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']);
}     

This mutates defaultHeaders, affecting all future calls (even to different endpoints).

I assume you rely on the typescript-nestjs generator in your project, so you might want to address this issue as well (doesn't have to be in this PR though :) )

@bilalshaikh42
Copy link
Contributor Author

bilalshaikh42 commented Oct 17, 2021

Thanks for your contribution! The fix looks good.

I noticed that later in the service class there is a bug related to defaultHeaders:

let headers = this.defaultHeaders;
...
if ({{paramName}}) {
  headers['{{baseName}}'] = {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']);
}     

This mutates defaultHeaders, affecting all future calls (even to different endpoints).

I assume you rely on the typescript-nestjs generator in your project, so you might want to address this issue as well (doesn't have to be in this PR though :) )

Sure, I would be interested in getting that as well. I have a few other changes to be made as well, to address updates in NestJS v8, but am not sure how to maintain backwards compatibility. Is there a set procedure for doing so?

As an example, one specific change I am thinking of is changing the import of HttpService from @nestjs/common to @nestjs/axios but that would break support for earlier versions.

@amakhrov
Copy link
Contributor

As for breaking changes - if there is a good reason for that (like in your case, where httpservice in nestjs/common is officially deprecated), you still create a PR, and it can target the next major release of openapi-generator.

Alternatively, you can add a config option to the generator, e.g. nestjsVersion. So that if it's 8 or below the generator produces the code as it is now, but if it's 9 or higher, you generate imports from nestjs/axios. This approach is used in typescript-angular generator, allowing to seamlessly support apps that are stuck on older framework versions for whatever reason

@macjohnny
Copy link
Member

@bilalshaikh42 thanks for your contribution. can you please update the samples?

@bilalshaikh42
Copy link
Contributor Author

@macjohnny
I am running into the following error when running the command ./mvnw clean package. I am not that familiar with the java ecosystem, so not sure what could be doing wrong

with proper logic to escape unsafe characters
[ERROR] Tests run: 15, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 1.227 s <<< FAILURE! - in org.openapitools.codegen.DefaultGeneratorTest
[ERROR] org.openapitools.codegen.DefaultGeneratorTest.supportCustomTemplateEngine  Time elapsed: 0.302 s  <<< FAILURE!
java.lang.RuntimeException: Could not generate api file for 'Ping'
        at org.openapitools.codegen.DefaultGeneratorTest.supportCustomTemplateEngine(DefaultGeneratorTest.java:311)
Caused by: com.github.jknack.handlebars.HandlebarsException:
api.hbs:48:83: java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.util.HashMap$Node[] java.util.HashMap.table accessible: module java.base does not "opens java.util" to unnamed module @76329302
    api.hbs:48:83
        at org.openapitools.codegen.DefaultGeneratorTest.supportCustomTemplateEngine(DefaultGeneratorTest.java:311)
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field transient java.util.HashMap$Node[] java.util.HashMap.table accessible: module java.base does not "opens java.util" to unnamed module @76329302
        at org.openapitools.codegen.DefaultGeneratorTest.supportCustomTemplateEngine(DefaultGeneratorTest.java:311)

[main] INFO com.tngtech.archunit.core.PluginLoader - Detected Java version 16.0.2
[INFO] Tests run: 2, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 4.074 s - in org.openapitools.codegen.ArchUnitRulesTest
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR]   DefaultGeneratorTest.supportCustomTemplateEngine:311 » Runtime Could not gener...
[ERROR]   StringHelpersTest.startsWithSectionalTest:47->evaluate:28 » Handlebars inline@...
[ERROR]   StringHelpersTest.startsWithTest:61->evaluate:28 » Handlebars inline@43bd77be:...
[ERROR]   StringHelpersTest.startsWithYesOverrideTest:81->evaluate:28 » Handlebars inlin...
[INFO]
[ERROR] Tests run: 1550, Failures: 4, Errors: 0, Skipped: 0

@bilalshaikh42
Copy link
Contributor Author

Though it seems the next few steps worked fine, so I pushed those. Let me know if there is anything else I need to do!

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][NestJS] client uses incorrect type for default headers, leading to compilation errors
3 participants