oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Support for pretty printing JSON

Open Supericy opened this issue 1 year ago • 3 comments

It doesn't look like this is supported, it would be nice to have it as an option.

Currently, the generated code for JSON encoding a response looks like so:

return json.NewEncoder(w).Encode(response)

It could easily be pretty printed by doing something like:

e := json.NewEncoder(w)
e.SetIndent("", "  ")
return e.Encode(response)

However, it doesn't look like there is any way to configure oapi-codegen to do this. I could of course add middleware to unmarshal/re-marshal the response, but that seems like unnecessary work.

Apologies if this is a duplicate, I didn't see any similar requests.

Supericy avatar Aug 27 '24 02:08 Supericy

This would be something we'd want to make opt in via an output-options configuration.

It's something we'd be willing to review a PR for - but is probably low priority for us, as it's not something that's commonly done, and although useful for human consumers, results in slightly higher network traffic cost, so isn't as useful for API-API traffic

jamietanna avatar Aug 28 '24 14:08 jamietanna

For anyone else reading this: https://www.jvt.me/posts/2024/09/16/api-pretty/ has a bit more info on why this wouldn't be recommended, but as mentioned, we'd still likely take a contribution for it

jamietanna avatar Sep 16 '24 09:09 jamietanna

Thanks for the replies Jamie! I'm mildly interesting in adding it as an option so I may put a PR in the future. I appreciate the blog post too!

Supericy avatar Sep 16 '24 12:09 Supericy