[BUG][NodeJS-Express-Server ReferenceRequestBodies are not parsed by the controller
Bug Report Checklist
- [X] Have you provided a full/minimal spec to reproduce the issue?
- [X] Have you validated the input using an OpenAPI validator (example)?
- [5] What's the version of OpenAPI Generator used?
- [X] Have you search for related issues/PRs?
- [X] What's the actual output vs expected output?
- [ ] [Optional] Bounty to sponsor the fix (example)
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
- Generate the server.
- Test that GETs function correctly.
- 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
👍 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.
cc @YishTish
I too have encountered this problem (with @openapitools/[email protected]). 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
Hi - was this ever addressed?
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: @.***
After updating the test above to use openapi-generator-cli v 2.21.04 and changing the rules for the index.js target and the test target to
index.js: openapi.yaml
npx openapi-generator-cli 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:8080/works -H "accept: */*" -H "Content-Type: application/json" -d '{"data":"The data"}'
@echo
@echo Test failing version
@curl -X POST http://localhost:8080/fails -H "accept: */*" -H "Content-Type: application/json" -d '{"data":"The data"}'
PHONY: clean
clean:
rm -rf api
rm -rf controllers
rm -rf node_modules
rm -rf .openapi-generator
rm -rf services
rm -rf uploaded_files
rm -rf utils
rm -f *.log
rm -f *.js
rm -f .eslintrc.json
rm -f .openapi-generator-ignore
rm -f package.json
rm -f package-lock.json
rm -f README.md
this gives what i expect:
$ make test
Test working version
{}
Test failing version
{}
I believe this is now fixed, although I am not going to go to the effort of figuring out when it happened just now.
Update: Makefile and openapi.yaml file now available in a repository for easier testing.