OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.8k stars 6.58k forks source link

[BUG][NodeJS-Express-Server ReferenceRequestBodies are not parsed by the controller #6678

Open RalphBragg opened 4 years ago

RalphBragg commented 4 years ago

Bug Report Checklist

Description

The NodeJS Express Server generator controller.js does not process reference request bodies. Is there are a way to make it do so?

openapi-generator version

5

OpenAPI declaration file content or url
   post:
      operationId: organisationsOrganisationIdAuthorisationserversPOST
      parameters:
      - description: The organisation ID
        explode: false
        in: path
        name: OrganisationId
        required: true
        schema:
          $ref: '#/components/schemas/OrganisationId'
        style: simple
      requestBody:
        $ref: '#/components/requestBodies/AuthorisationServerRequest'
      responses:
        "201":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/AuthorisationServer'
          description: Authorisation server response
        "400":
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/BadRequest'
          description: Bad Request
        "401":
          description: Unauthorized
        "404":
          description: Not found
        "406":
          description: Not Acceptable
        "500":
          description: Internal Server Error
        "502":
          description: Bad Gateway
      security:
      - authorizer: []
      summary: Create an Authorisation Server for the given organisation
      tags:
      - Authorisation Servers
      x-eov-operation-handler: controllers/AuthorisationServersController
Command line used for generation

OPENAPI_GENERATOR_VERSION=5.0.0-SNAPSHOT openapi-generator-cli generate -i /TrustFramework.yaml -o ./output -g nodejs-express-server

Steps to reproduce
  1. Generate the server.
  2. Test that GETs function correctly.
  3. Post's fail to process throwing an undefined exception in Controller.js collectRequestParams() const { content } = request.openapi.schema.requestBody; as content is undefined whent the requestbody is a reference.
Related issues/PRs
Suggest a fix

Can reference request bodies be handled

auto-labeler[bot] commented 4 years ago

👍 Thanks for opening this issue! 🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

wing328 commented 4 years ago

cc @YishTish

jwmcleod commented 4 years ago

I too have encountered this problem (with @openapitools/openapi-generator-cli@1.0.15-5.0.0-beta). Here is a minimal spec with endpoints for the case of having the body directly defined in the path definition (which works as expected) and using a body definition in /components/requestBodies (which fails).

openapi: "3.0.0"
info:
  description: "Demonstrate a problem with openapi-generator generate -i openapi.yaml -g nodejs-express-server"
  version: "0.0.0"
  title: "Test $ref to requestBodies element"
tags:
- name: "test"
  description: "description"

paths:
  /works:
    post:
      description: "Working version - defines content body directly"
      summary: "working version"
      operationId: "add_works"
      tags:
      - "test"
      requestBody:
        description: "directly-defined body"
        required: true
        content:
          application/json:
            schema:
              type: "object"
              properties:
                data:
                  type: "string"
      responses:
        "200":
          description: "Success"

  /fails:
    post:
      description: "Failing version - defines content via reference to components/requestBodies/Body"
      summary: "failing version"
      operationId: "add_fails"
      tags:
      - "test"
      requestBody:
        $ref: "#/components/requestBodies/Body"
      responses:
        "200":
          description: "Success"

components:
  requestBodies:
    Body:
      required: true
      content:
        application/json:
          schema:
            type: "object"
            properties:
              data:
                description: "data"
                type: "string"

Here's a makefile to build and run the server and show the problem:

index.js: openapi.yaml
    openapi-generator generate -i openapi.yaml -g nodejs-express-server

PHONY: server
server: index.js

PHONY: run
run: index.js
    npm start

PHONY: test
test: index.js
    @echo Test working version
    @curl -X POST http://localhost:3000/works -H "accept: */*" -H "Content-Type: application/json" -d '{"data":"The data"}'
    @echo
    @echo Test failing version
    @curl -X POST http://localhost:3000/fails -H "accept: */*" -H "Content-Type: application/json" -d '{"data":"The data"}'

Use:

make server
make run

And then, from another terminal session: make test

Results (under Cygwin):

$ make test
Test working version
{}
Test failing version
Cannot read property 'application/json' of undefined
RalphBragg commented 3 years ago

Hi - was this ever addressed?

YishTish commented 3 years ago

I see the problem. Don't remember if I handled it back in the day. Will check later and update.

On Thu, May 13, 2021 at 8:33 AM Ralph Bragg @.***> wrote:

Hi - was this ever addressed?

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/OpenAPITools/openapi-generator/issues/6678#issuecomment-840313691, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAGOOLMERXQZEDFIUBUEBHTTNNQCDANCNFSM4N6QI6WQ .

--

Yishai Landau

C: 972-54-979-2080 T: 972-8-6333-960 E: @.***