OpenAPITools / OpenAPITools/openapi-generator

[nodejs-express-server] Remove the new Promise antipattern

Open
#6,169 0 comments 3 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.