Skip to content

codegen: route listed custom bytes and string types through JSON policies - #829

Draft
vanderbr wants to merge 1 commit into
anthropics:mainfrom
reallyme:json-policy-codegen
Draft

vanderbr wants to merge 1 commit into
anthropics:mainfrom
reallyme:json-policy-codegen

Conversation

@vanderbr

@vanderbr vanderbr commented Nov 13, 2026 •

Copy link
Copy Markdown

Custom bytes and string representations can opt into their ProtoBytesJson / ProtoStringJson policies through CodeGenConfig::json_policy_types, buffa_build::Config::json_policy_type(path), and the repeatable plugin option json_policy_type=<rust_type>. Unlisted representations retain their existing JSON behavior.

Part of #822. Depends on #823, #824, #825 and #828; their reviewed changes are included in this branch until they merge. The branch is consolidated into one commit against main.

The generated routing covers singular, optional, repeated and oneof fields, map keys and values, and eager, lazy and owned views. Repeated and map fields use the runtime policy helpers without generating per-run ProtoElemJson implementations for policy representations. Policy types can therefore live in a separate provider crate and be reused by independently generated consumer modules.

Configuration fails closed for unmatched bindings and near-miss type names, including when JSON generation is disabled. Matching uses the configured rules so per-directory generation works. Policy string keys on enum-valued maps remain rejected until the enum map helpers support them. Equivalent path syntax is normalized for policy matching and generated trait deduplication, including whitespace, redundant type-path parentheses, optional raw identifiers and type-path turbofish; Rust aliases are not resolved.

CodeGenWarning::JsonPolicyReflectionBypass { policy_types } is emitted once when generated JSON fields actually use policies and reflection is enabled. It identifies the selected types. Reflective export/import, binary encoding and text formatting do not enforce JSON policies; policy refusal is not a confidentiality boundary. Unused policy configuration does not trigger this warning.

The included parent hardening preserves plugin capabilities in error responses, accepts supported associated type paths, bounds custom decode hooks to their enclosing frames, and keeps map-key policy errors free of rejected input. The guarded-field guide explains policy scope, representation-owned scratch storage, cleanup responsibilities and reflection behavior.

The included decode-hook and runtime-policy implementations record frame context before handwritten decoder overrides and keep caller-buffer base64 errors free of input symbols and offsets. These corrections preserve policy selection, accepted base64, caller storage ownership and generated routing.

Validation:

  • 4,438 workspace tests and doctests pass with all features.
  • Strict workspace/all-target/all-feature Clippy and formatting pass.
  • Regression tests cover frame overrides, fixed base64 diagnostics and the existing generated policy/provider integrations.

The external protobuf conformance suite was not rerun for this follow-up.

Contributed by ReallyMe Technologies Limited.

@github-actions

github-actions Bot commented Nov 13, 2026 •

Copy link
Copy Markdown

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@vanderbr

Copy link
Copy Markdown
Author

recheck

Route selected custom representations through their runtime JSON policies across owned and borrowed field shapes. Include bounded decode-hook and runtime-policy prerequisites with their current frame-context and fixed-diagnostic corrections. Preserve canonical type matching, provider reuse and reflection warnings.
@vanderbr
vanderbr force-pushed the json-policy-codegen branch from f21d03c to ba057b2 Compare November 18, 2026 23:27

This branch has not been deployed

No deployments
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.

1 participant