mpashkovskiy/express-oas-generator

Not fetching POST request body using typescript

christian-hawk opened this issue · 3 comments

Hello there, thx for this project.

I'm wondering what I'm doing wrong:

server.ts

import routes from '@sp-proxy/frameworks-drivers/main/routes'
import express from 'express'
import { readFileSync } from 'fs'
import https from 'https'
import cfg from '@sp-proxy/frameworks-drivers/main/config/env'
import {
  handleRequests,
  handleResponses,
  HandleResponsesOptions,
  SPEC_OUTPUT_FILE_BEHAVIOR
} from 'express-oas-generator'
const app = express()

const oasGeneratorOptions : HandleResponsesOptions = {
  specOutputFileBehavior: SPEC_OUTPUT_FILE_BEHAVIOR.PRESERVE,
  specOutputPath: './test_spec.json',
  swaggerDocumentOptions: undefined
}
handleResponses(app, oasGeneratorOptions)

app.use(routes)
app.use(express.urlencoded({ extended: false }))
const cert = readFileSync(cfg.tlsCertPath).toString()
const key = readFileSync(cfg.tlsKeyPath).toString()
// handleRequests() tried here too
const server = https.createServer({ key: key, cert: cert }, app)

handleRequests()
server.listen(cfg.port, () => console.log('server started'))

export default app

When I POST to the following endpoint, I don't get the request params, only responses):

...
"/inbound-saml/trust-relation/metadata": {
      "post": {
        "summary": "/inbound-saml/trust-relation/metadata",
        "responses": {
          "201": {
            "description": "Created",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "creation": {
                      "type": "string",
                      "example": "success"
                    }
                  }
                }
              }
            }
          }
        },
        "tags": []
      }
    }
...

Any idea what may be happening? Thanks.

Any luck? Im facing this issue right now

I guess I found the reason.
If u look at the official README.md
there is place:

Parameters and response body not documented!

Express-oas-generator (EOG) adds parameters handler as a very last middleware. If any middleware or 
path in router breaks the chain and doesn't pass execution to next middleware/router then 
very last EOG middleware won't be called. So call next() or next(err) as the very last line in your handler.
 Some docs:

calling next() https://expressjs.com/en/guide/writing-middleware.html
handling errors with next() https://expressjs.com/en/guide/error-handling.html
For more info please read the entire issue report

I tried to put next() right before return res.json(rs); (which originally was ending of route)
After I removed it, now it shows request body.