Skip to content

Conversation

@maelick
Copy link
Contributor

@maelick maelick commented Mar 17, 2025

I experienced an issue similar to #660 when passing a parameter struct to stdlib slog (using a JSONHandler).

From what I was able to reproduce, the error occurs when calling encoding/json.Marshal on missing optional parameters that the Opt* struct is used in response types. The problem does not seem to occur if there are no response objects including the optional type (because the Encode/Decode methods are not generated on the Opt* types).

For me the error occurred when using slog with a JSONHandler inside an ogen handler:

logger := slog.New(slog.NewJSONHandler(&strings.Builder{}, &slog.HandlerOptions{}))
[...]
logger.Info("test", "params", params)

Which gives this type of log output:

{"time":"2025-03-17T16:13:56.22672301+01:00","level":"INFO","msg":"test","params":"!ERROR:json: error calling MarshalJSON for type api.OptInt: unexpected end of JSON input"}

This PR includes:

  • test to reproduce the problem
    • both with encoding/json and slog
    • test can be run without the fix with maelick/ogen@e772bdb
    • the problem specifically occurs when the optional struct is used both as a param and as part of a return type
    • I had to add an endpoint that also returns each value type (see maelick/ogen@14db889), otherwise the Encode/Decode methods that use go-faster/jx are not generated
  • a (partial) fix for JSON Marshal error on generated empty OptString struct #660
    • based on Go 1.24 omitzero tag
    • change limited to parameter structs (i.e. does not affect schema structs)
    • add default JSON tag to generated parameters in Generator:
      • omitempty for slices, maps and non-optional nullable fields
      • omitempty and omitzero for optional nullable fields
      • nothing for other fields

Notes:

  • I didn't change the minimum Go version in go.mod
    • not sure whether the new tag is backward compatible and whether Go 1.23 would just ignore it or not
    • edit: tests are failing in the pipeline, I assume because Go 1.23 is used to execute them
  • I only partially squashed my branch to keep a working commit with the test but without the fix
    • if needed I can re-squash to have one commit with the changes and one with the generated files
    • edit: did more squashing

@maelick maelick force-pushed the optional-params-omitzero-squashed branch 3 times, most recently from 4b1ce7c to 250c197 Compare March 18, 2025 14:51
@slangens1
Copy link

has everyone just given up on this by now?

@ernado
Copy link
Member

ernado commented Sep 18, 2025

Hey @maelick, very sorry for late responce.
Can you please fix conflicts so I can merge this?

@maelick maelick force-pushed the optional-params-omitzero-squashed branch from 250c197 to e10e57b Compare October 2, 2025 15:07
@maelick
Copy link
Contributor Author

maelick commented Oct 2, 2025

@ernado I rebased on main, but the template changes in #1446 were incompatible and non-trivial to solve.
I kept the template from #1446, but then the new tests to reproduce the bug were failing (due to missing JSON tags).

I fixed them by removing the Parameter.GoStructTag method I had added and moved the omitempty/omitzero tags as default JSON tags inside Generator (see b718d5b).

Update parameter integration test with:
* new /optionalParameters endpoint in test spec
* add empty handler for new endpoint
* add test for logging and marshaling params
Generated parameter structs have field tags omitempty and/or omitzero annotations:
* omitempty for slices, maps and non-optional nullable fields
* omitempty and omitzero for optional nullable fields
* nothing for other fields
@maelick maelick force-pushed the optional-params-omitzero-squashed branch from fd3bf54 to db90e0f Compare October 3, 2025 06:17
@maelick
Copy link
Contributor Author

maelick commented Oct 3, 2025

Fixed the linting issue with imports ordering and the missing regenerated example.

@ernado ernado merged commit bf1a3ed into ogen-go:main Oct 3, 2025
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants