Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inconsistent Handling of Empty JSON Request Body in Hono Validator #2651

Open
ilshm opened this issue May 9, 2024 · 6 comments
Open

Inconsistent Handling of Empty JSON Request Body in Hono Validator #2651

ilshm opened this issue May 9, 2024 · 6 comments
Labels

Comments

@ilshm
Copy link

ilshm commented May 9, 2024

What version of Hono are you using?

4.3.3

What runtime/platform is your app running on?

Bun

What steps can reproduce the bug?

  1. Send a request with an empty JSON body to a route utilizing hono/validator middleware.
  2. Observe the unexpected 400 Bad Request response due to the middleware's exception handling for empty JSON bodies.

What is the expected behavior?

The hono/validator middleware should gracefully handle empty JSON request bodies, considering them valid without triggering exceptions. This behavior is crucial for scenarios where empty JSON bodies are legitimate or expected.

What do you see instead?

The current behavior negatively affects applications where empty JSON request bodies should be permitted. This inconsistency leads to unexpected errors and hinders the module's usability.

Additional information

This issue is critical for applications relying on hono/validator. A swift resolution or workaround would greatly benefit the developer community.

@ilshm ilshm added the bug label May 9, 2024
@monoald
Copy link

monoald commented May 11, 2024

Hello @ilshm, can you send the code to see how did you setup the middleware, and how are you making the request?

I just reproduce the steps to see the bug, but the request is successful, I have no error.

@ilshm
Copy link
Author

ilshm commented May 12, 2024

Hello @ilshm, can you send the code to see how did you setup the middleware, and how are you making the request?

I just reproduce the steps to see the bug, but the request is successful, I have no error.

Hello again, something like this, as I remember:

const route = app.post(
  '/posts',
  zValidator(
    'json',
    z.object({
      title: z.string().optional(),
    })
  ),
  (c) => {
    const title = c.req.valid("json");
    // ... use your validated data
  }
)

Request was made from cURL and Thunder Client.

Hope this helps!

@monoald
Copy link

monoald commented May 12, 2024

I thought you were using the hono/validator middleware, but again I have no error.

This is how I made the request with curl:

curl -X POST "http://localhost:8787/posts"  \
     -H "Content-Type: application/json" \
     -d "{}"

@ilshm
Copy link
Author

ilshm commented May 12, 2024

I thought you were using the hono/validator middleware, but again I have no error.

hono/zod-validator relies on hono/validator

This is how I made the request with curl

I made it without any JSON in body, and without any headers, like so:
curl -X POST "http://localhost:8787/posts"

I have no error

I get Invalid HTTP header: Content-Type=undefined

To circumvent this issue, I've adjusted the middleware (hono/validator) as follows:

validator.ts

      case 'json':
        try {
          value = await c.req.json()
        } catch {}
        break

@monoald
Copy link

monoald commented May 12, 2024

Oh, I didn't know that hono/zod-validator relies on hono/validator

I don't think the behavior should be changed in hono/validator, just send the corresponding Content-Type header and an empty json in the body. those validations are to ensure hono receives the expected values when you set the type in the validation

@Sheharyar566
Copy link

I'm also facing this on node-template and using nodejs. However, I think, it'd be a better idea to keep the behavior as is, but update the message it sends back. For example,
image

in case of empty body, we should send a message like "Expected body, received undefined" or something.. something that makes it a bit clear that the dev should really send body.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants