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

Optional content codec failing on missing request body #3144

Open
anqit opened this issue Sep 18, 2024 · 5 comments · Fixed by #3167
Open

Optional content codec failing on missing request body #3144

anqit opened this issue Sep 18, 2024 · 5 comments · Fixed by #3167
Labels
💎 Bounty bug Something isn't working 💰 Rewarded

Comments

@anqit
Copy link

anqit commented Sep 18, 2024

Describe the bug
I'd like to define an endpoint where the request body is optional. I've passed an optional content codec to the endpoint, but the implemented route is still returning an error indicating not being able to read the body there is no body in the request:

<html><body><h1>Codec Error</h1><p>There was an error en-/decoding the request/response</p><p id="name">MalformedBody</p><p id="message">Malformed request body failed to decode: Unexpected end of input</p></body></html>%

To Reproduce
Steps to reproduce the behaviour:

I've defined an extension method as

    extension [P, I, E, O, M <: EndpointMiddleware](e: Endpoint[P, I, E, O, M])
        def optionalIn[I1: HttpContentCodec](using combiner: Combiner[I, Option[I1]]): Endpoint[P, combiner.Out, E, O, M] =
            e.copy(input = e.input ++ HttpCodec.content[I1].optional)

created and endpoint using that:

val myEndpoint = Endpoint(Method.POST / path / "some-path")
        .optionalIn[OptionalBody]
        // ... other endpoint definition calls

and implemented the route:

        Handler.fromFunctionZIO: (maybeBody: Option[OptionalBody]) =>
                // ... implement business logic

Make a request with no body to that endpoint results in the error. Passing a correctly formed body does not result in the error.

Expected behaviour
Specifying an optional in declaration should allow requests with no body and result in an Option being added to the expected In type and empty option being passed to the handler function if the request contains no body.

Additional context
Add any other context about the problem here.

@anqit anqit added the bug Something isn't working label Sep 18, 2024
@jdegoes
Copy link
Member

jdegoes commented Sep 18, 2024

/bounty $150 for fix and testcase proving it's fixed.

Copy link

algora-pbc bot commented Sep 18, 2024

💎 $150 bounty • ZIO

Steps to solve:

  1. Start working: Comment /attempt #3144 with your implementation plan
  2. Submit work: Create a pull request including /claim #3144 in the PR body to claim the bounty
  3. Receive payment: 100% of the bounty is received 2-5 days post-reward. Make sure you are eligible for payouts

Thank you for contributing to zio/zio-http!

Add a bountyShare on socials

Attempt Started (GMT+0) Solution
🟢 @987Nabil #3167

Copy link

algora-pbc bot commented Sep 24, 2024

💡 @987Nabil submitted a pull request that claims the bounty. You can visit your bounty board to reward.

Copy link

algora-pbc bot commented Sep 27, 2024

🎉🎈 @987Nabil has been awarded $150! 🎈🎊

@guizmaii
Copy link
Member

guizmaii commented Nov 19, 2024

@987Nabil Your fix in #3167 doesn't seem to fix this issue.
I have this issue in my code. I tried to use the latest snapshot but it doesn't fix the error. My tests are still failing

I made a reproducer here: #3214

@guizmaii guizmaii reopened this Nov 19, 2024
guizmaii added a commit that referenced this issue Nov 19, 2024
guizmaii added a commit that referenced this issue Nov 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
💎 Bounty bug Something isn't working 💰 Rewarded
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants