kogosoftwarellc / open-api

A Monorepo of various packages to power OpenAPI in node
MIT License
892 stars 235 forks source link

[express-openapi] text/plain content in requestBody gives an error #741

Closed eyzi closed 2 years ago

eyzi commented 3 years ago

Hey. I'm trying to send a plain text PATCH request but express-openapi gives me an error that the body "should be string". Is this a bug or am I doing something wrong?

Here's what I have in the YAML file, a sample request, and the response that I get back. For context, this API request (PATCH /links/{id}) is used to update the link id with the plain text parameter in the body.

api-doc.yml:

...
requestBody:
  description: Plain text parameter
  required: true
  content:
    text/plain:
      schema:
        type: string
...

sample request (sent via Postman):

curl --location --request PATCH 'https://api.example.com/links/google' \
--header 'Content-Type: text/plain' \
--data-raw 'https://google.com'

response:

{
  status: 400,
  errors: [
    {
      errorCode: 'type.openapi.requestValidation',
      message: 'should be string',
      location: 'body'
    }
  ]
}

It works if I do it with application/json instead. Though, I'd like to get it working with text/plain if possible.

Swizec commented 2 years ago

For anyone else who finds this thread, I was missing text/plain support in my Express stack.

This fixed it:

.use(BodyParser.text({ type: 'text/*' }))