OpenAPITools / OpenAPITools/openapi-generator
[nodejs-express-server] Remove the new Promise antipattern
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Description
Hello,
there is a useless instanciation of a new Promise in the service template:
https://github.com/OpenAPITools/openapi-generator/blob/a58441cf68171456c3a76c9f2b1745b21a3eae69/modules/openapi-generator/src/main/resources/nodejs-express-server/service.mustache#L24
Because of its presence in generated code, it spreads some bad practices regarding the usage of promises in Javascript: In this case we would simply need to return from the service, either a simple object or a promise instanciated by a call to a DB..., in wery limited occasion we would need to return a new Promise by ourselves.
Proposed resolution
const {{{operationId}}} = ({{#allParams}}{{#-first}}{ {{/-first}}{{paramName}}{{#hasMore}}, {{/hasMore}}{{#-last}} }{{/-last}}{{/allParams}}) => {
try {
return Service.successResponse({
{{#allParams}}
{{paramName}},
{{/allParams}}
}));
} catch (e) {
throw Service.rejectResponse(
e.message || 'Invalid input',
e.status || 405,
));
}
};
Additional context, some references
https://hackernoon.com/javascript-promises-best-practices-anti-patterns-b32309f65551
https://runnable.com/blog/common-promise-anti-patterns-and-how-to-avoid-them
see "Creating new Promises"
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with modules/openapi-generator/src/main/resources/nodejs-express-server/service.mustache at the referenced operation template and inspect the generated Node.js Express service output. Remove the unnecessary Promise wrapper while preserving the shown response and error handling, then verify the generated service behavior with the relevant generator tests if available.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript
- Domain
- api, backend
- Issue type
- Refactor
- Difficulty
- 1/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 45/100