Skip to content

fix: only use emit_default for proto messages - #75

Merged
viacheslav-rostovtsev merged 2 commits into
googleapis:mainfrom
viacheslav-rostovtsev:dev/virost/fix-emit-defaults-json3
Sep 23, 2026
Merged

viacheslav-rostovtsev merged 2 commits into
googleapis:mainfrom
viacheslav-rostovtsev:dev/virost/fix-emit-defaults-json3

Conversation

@viacheslav-rostovtsev

@viacheslav-rostovtsev viacheslav-rostovtsev commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

internal ref: b/565440846

Also pin toys in the Gemfile since we're pinning it in the CI

GrpcTranscoder#construct_body_query_params passed `emit_defaults: true`
to `to_json` on whatever the `body:` template resolved to. That keyword
belongs to Google::Protobuf::MessageExts#to_json; when the body template
points at a scalar, enum or repeated field the call instead dispatches to
the json gem's Object#to_json, which has never accepted it.

json 2.x silently discarded unknown keywords, so this was invisible. json
3.x validates them and raises ArgumentError, breaking transcoding at
runtime for any RPC with a scalar body binding. The gemspec does not
constrain json, so nothing prevents resolving 3.x.

Guard the keyword on the receiver actually being a protobuf message.
Behaviour is unchanged on json 2.x, where the keyword was always a no-op
on the non-message branch. The sibling call site that serialises the whole
request is safe and untouched: there the receiver is always a message.

Bug: b/565440846
CI already pins toys to the 0.22.x series (googleapis#74), but nothing enforced it
for anyone running `toys ci` locally, so a stale or newer toys silently
produced different results than CI.

Declaring toys as a bundle dependency makes the pin self-enforcing:
Toys::Utils::Gems#check_gemfile_compatibility evaluates the Gemfile and
raises IncompatibleToysError when the running toys falls outside the
declared requirement, instead of failing later in a confusing way.

Note this does not make toys honour Gemfile.lock. Toys unconditionally
writes a temporary Gemfile that strips every already-loaded gem from the
dependency list and re-pins it to the exact version the toys process
loaded, so `toys test` still exercises the newest installed versions of
gems toys itself loads. Use `bundle exec` when lock fidelity matters.

Resolution is otherwise unaffected: the lockfile gains only toys and
toys-core, with no other version changes.
@viacheslav-rostovtsev
viacheslav-rostovtsev requested a review from a team as a code owner September 23, 2026 20:57

@torreypayne torreypayne left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: test? (might be existing coverage over these types, not sure)

@viacheslav-rostovtsev

Copy link
Copy Markdown
Member Author

nit: test? (might be existing coverage over these types, not sure)

existing tests cover this and fail if you allow json3

@viacheslav-rostovtsev
viacheslav-rostovtsev merged commit c80dbce into googleapis:main Sep 23, 2026
16 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.

2 participants