danielgtaylor / danielgtaylor/huma

Autopatch PATCH request response body frequently has incorrect content encoding

Open
#753 1 comment 0 reactions 0 assignees View on GitHub
question
Dominant language
Go
Stars
4.4k
Forks
285
Avg merge
40m
Merged PRs (30d)
1

Description

I use Huma with the echo router via humaecho. I have echo `gzip` middleware enabled by default. This seems to not play well with autopatch, as follows: often, autopatch PATCH response unexpectedly comes through as gzipped when a plain text JSON is expected. This behavior was observed with a range of HTTP clients and a range of autopatched endpoints. The operation itself succeeds, returns 200, and performs the data changes expected, but the clients (expecting a JSON) barf on the binary response. A representative example from our API tests:

```
const response = await fetch('/test-endpoint', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ key: 'value' }),
});

const rawText = await response.clone().text();
console.log('Raw response:', rawText); // supposed to be well-formed JSON but get binary gzipped data
```

Indeed, in autopatch.go, if you print the value of `putWriter.Header().Get("Content-Encoding")` you get `gzip`.

A way to work around it is to specify the `"Accept-Encoding": "identity"` header on the client side, however it seems that this is something that should not require additional headers. Because I don't understand the logic behind why the body comes through as gzipped in the case of autopatch and not with regular client-side GETs and PUTs, I am not sure what the correct fix is, but it would be nice to put it in place, or at the very least document that using a gzip middleware with autopatch requires the workaround above.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in autopatch.go and reproduce the PATCH request with the echo gzip middleware enabled, comparing its response with regular GET and PUT requests. Check the issue's fetch example and the Content-Encoding header behavior; done means autopatched PATCH responses are valid JSON for normal clients without requiring Accept-Encoding: identity, with regression coverage or documentation if the workaround is intentional.

Written by the indexing model from the issue text.

Assessment

Tech stack
go
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.