← 输入框|github.com|刷新
Skip to content

Introduce ValidateMessage to enable alternative protobuf runtimes - #480

Open
andrewparmet wants to merge 9 commits into
bufbuild:mainfrom
andrewparmet:pv-message-reflector-pure
Open

andrewparmet wants to merge 9 commits into
bufbuild:mainfrom
andrewparmet:pv-message-reflector-pure

Conversation

@andrewparmet

@andrewparmet andrewparmet commented Jun 14, 2026 •

Copy link
Copy Markdown
Contributor

Adds a runtime-independent ValidateMessage interface so alternative protobuf runtimes, including protokt, can validate their messages without converting the root message to com.google.protobuf.Message.

Changes

  • Expose field access, presence, message emptiness, oneof selection, and a CEL representation through ValidateMessage and Value.
  • Preserve existing Validator implementations with a default validate(ValidateMessage) overload. The built-in validator supports both entry points.
  • Validate nested messages, repeated fields, maps, oneofs, and well-known types through the runtime-independent API.
  • Support map-backed native rules and integer enum values. Apply IGNORE_IF_ZERO_VALUE to empty messages across runtimes while preserving explicit presence and unknown fields.
  • Document raw and JVM representations for every field kind. Field and element values support jvmValue(Object.class) for violation details, including message values. Evaluator paths and descriptor guards keep internal root and rule-metadata wrappers out of JVM conversion.
  • Check collection presence and protobuf oneofs directly, and reuse nested protobuf message adapters.

Related work

Testing

  • 323 tests pass, including 17 external-runtime cases in a separate package that use only the public API and eight message/custom-rule regressions.
  • All 2,872 upstream conformance cases pass with native rules enabled and disabled.
  • Protokt integration passes 2,840 conformance cases in each mode, with 32 existing expected skips and no failures. Four adapter contract tests also pass.
  • Formatting and Javadoc checks pass.

Quiet-run timing benchmarks with native rules disabled (JDK 21, Linux x86_64), relative to the mean of two main controls:

Benchmark Time change
Map -7.9%
Wrapper -0.6%
Repeated rule -0.9%
Enum rules +2.2%

The controls differed by at most 3.3%. CPU samples recorded no steal time or swapping. Map allocations fell from about 2,162 to 1,888 bytes per validation. Benchmark results and load samples.

A focused repeated-rule check, with six JVM forks per version in balanced order, measured -0.1% time and +1.6% mean allocation. Both versions exhibited the same three allocation levels, with the PR adding 16 bytes at each level; the larger differences between individual forks also occurred on unchanged main.

@andrewparmet
andrewparmet marked this pull request as ready for review June 14, 2026 15:22
pkwarren pushed a commit that referenced this pull request Jun 15, 2026
Moves protovalidate-java's CEL setup from the deprecated standard
runtime to the planner runtime.

## Notable callouts

- Wire the compiler and runtime by hand rather than through
`plannerCelBuilder()`. `CelRuntimeImpl` directs callers to subset the
standard library via `setStandardFunctions` rather than
`setStandardEnvironmentEnabled`, so the runtime drops stdlib `matches`
that way while the checker continues to use
`setStandardEnvironmentEnabled(false)`. This keeps `CustomOverload`'s
caching `matches` replacement (cel-java#1038).
- Set
`CelOptions.current().enableHeterogeneousNumericComparisons(true)`,
which `CelRuntimeImpl` requires.
- Update `cel-java` to 0.13.1 preparing to support alternative Protobuf
runtimes.

## Related work:

- cel-java: 0.13.1 carries the CelValue-path fixes this work relies on
(fixed32/64 treated as unsigned; FieldMask navigable as a message,
cel-expr/cel-java#1074).
- protovalidate-java: #480 introduces `MessageReflector` to support
alternative Protobuf runtimes, which builds on this runtime change
(supersedes #202).
- protokt: open-toast/protokt#402 is the downstream consumer that
validates protokt messages through `MessageReflector` on this planner
runtime.

## Testing

`./gradlew :conformance:conformance` passes.
@jonbodner-buf

Copy link
Copy Markdown
Contributor

@andrewparmet can you update the branch from main?

@andrewparmet

Copy link
Copy Markdown
Contributor Author

@jonbodner-buf done!

@jonbodner-buf

Copy link
Copy Markdown
Contributor

My biggest concern is there are no tests to validate these changes. Yes, the existing tests still pass, but nothing in the existing tests calls public ValidationResult validate(MessageReflector message) throws ValidationException .

The exception thrown by build.buf.protovalidate.MessageValue#jvmValue and build.buf.protovalidate.MessageReflectorValue#jvmValue also concern me, want to make sure that there is no code path where that could be triggered, especially for MessageReflectorValue, since it will be used with external types that implement MessageReflector.

andrewparmet and others added 2 commits June 17, 2026 23:15
Rename the MessageReflector interface to ValidateMessage, and accordingly
ProtobufMessageReflector -> ProtobufValidateMessage and MessageReflectorValue ->
ValidateMessageValue (with its `reflector` field/locals renamed to `message`).

Also give the jvmValue UnsupportedOperationException on the message-backed Value
types a descriptive message: MessageValue notes it carries the rule message and
should be read via messageValue()/celValue(); the ValidateMessage-backed value
notes that a getField() implementation returned a message value for a field
evaluated as a scalar.
Add ValidateMessageTest, which validates through a map-backed ValidateMessage
(no com.google.protobuf.Message): scalar / nested / repeated / map /
repeated-message / map-message violations and their field paths, a valid
message, the well-known Timestamp type, bytes, enum, unsigned (uint32), the
Int32Value wrapper, a required oneof, the clear error when a message field is
not backed by a ValidateMessage, and that jvmValue is unsupported on the
message-backed Value types.

Co-authored-by: Jon Bodner <jbodner@buf.build>
@andrewparmet andrewparmet changed the title Introduce MessageReflector to enable alternative protobuf runtimes Introduce ValidateMessage to enable alternative protobuf runtimes Jun 18, 2026
@andrewparmet

Copy link
Copy Markdown
Contributor Author

Pushed up some tests - thanks for the starter patch from your investigation! The existing Validator tests do indirectly call validate(ValidateMessage message) via the Message overload, which hands off directly to it.

Exercise successful and failing boolean and string rules through an alternative runtime with native rules enabled and disabled. Document that field and element values must support jvmValue(Object.class), including nested messages.
Keep existing Validator implementations compatible through a default overload. Accept map-backed fields and integer enums, and check message emptiness through ValidateMessage while preserving explicit presence and unknown fields.

Document raw and JVM value types and add public-API parity tests in a separate package. Use direct presence and oneof lookups, cache message adapters lazily, and skip zero-value adaptation unless the ignore rule requires it.
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