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

Response struct without a "Body" field is silently ignored if using Register #481

Open
RangelReale opened this issue Jun 14, 2024 · 2 comments
Labels
question Further information is requested

Comments

@RangelReale
Copy link

If we register an operation where the output struct don't have a "Body" field, the output is completely ignored and nothing is written to the http connection.

This is a surprising behavior, maybe Register should panic if the output structure don't have this field? This will for sure generate nasty bugs in big codebases.

@outofthisworld
Copy link

outofthisworld commented Jun 15, 2024

If we register an operation where the output struct don't have a "Body" field, the output is completely ignored and nothing is written to the http connection.

This is a surprising behavior, maybe Register should panic if the output structure don't have this field? This will for sure generate nasty bugs in big codebases.

Also just to add to this, all of the fields in the struct will automatically become 'headers'. Which you can see in the docs

In addition, returning nil as a response body causes the package to error due to some Serialization issue. Now you could argue you should be instead returning No Content, however I don't necessarily agree because you could be expecting a pointer to something, return it, oops accidently nil and then get a 500.

@danielgtaylor danielgtaylor added the question Further information is requested label Aug 19, 2024
@danielgtaylor
Copy link
Owner

@RangelReale @outofthisworld I've fixed the 500 error when returning nil response structs without error if a body was expected.

As for this issue, the response struct represents the entire response, not just the body. As mentioned the fields become headers and can be used to set HTTP status if necessary as well as a body if it makes sense for that operation. What is the broken behavior you are seeing and what do you expect to see? See also:

Responses can have an optional status code, headers, and/or body. Like inputs, they use standard Go structs which describe the entirety of the response.

https://huma.rocks/features/response-outputs/

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

No branches or pull requests

3 participants