OpenAPITools / OpenAPITools/openapi-generator

[BUG] [nodejs-express-server] Function with object as parameter

Open
#8,140 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: Nodejs
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When I create the server with nodejs express server, in the tagNameServices creates the services properly, but when the requested parameter is an object it generates it in the following way:

const foo = ({ foo }) => new Promise(
  async (resolve, reject) => {
    try {
      resolve(Service.successResponse({
        foo,
      }));
    } catch (e) {
      reject(Service.rejectResponse(
        e.message || 'Invalid input',
        e.status || 405,
      ));
    }
  },
);

The problem is that {tag} is that the parameter is always null. I would like the generator to create a variable without the object brackets.

openapi-generator version

4.3.1

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Issue
  version: 1.0.0
servers:
  - url: http://localhost:4000/
tags:
  - name: tag

paths:
  /tag:
    post:
      operationId: foo
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/foo'
        required: true
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/responsefoo'
          description: foo
      tags:
        - tag
components:
  schemas:
    foo:
      additionalProperties: false
      properties:
        test:
          description: |
            test
          maxLength: 12
          pattern: \s*
          type: string
      type: object
    responsefoo:
      additionalProperties: false
      example:
        msg: Se ha encendido el smartplug
      properties:
        msg:
          description: ok
          maxLength: 2
          minLength: 2
          pattern: '[\s]+'
          type: string
      readOnly: true
      type: object

Generation Details
openapi-generator-cli generate -i {path}/name.yaml -g nodejs-express-server -o {path}
Steps to reproduce

Open a terminal/cmd... and execute the following commnad

openapi-generator-cli generate -i {path}/name.yaml -g nodejs-express-server -o {path}

In Services/TagServices u will find the object as function parameter

Suggest a fix

Do not generate the parameter as an object

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 by running the documented openapi-generator-cli command with the supplied OpenAPI YAML, then inspect the generated Services/TagServices output for the request-body parameter. Compare the generated function signature with the expected non-object parameter behavior; done means the generated nodejs-express-server service handles the object request correctly without the problematic destructuring.

Written by the indexing model from the issue text.

Assessment

Tech stack
express, javascript, nodejs, openapi
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.