Package org.mockserver.grpc
Class GrpcExampleSynthesizer
java.lang.Object
org.mockserver.grpc.GrpcExampleSynthesizer
Synthesizes schema-valid example protobuf messages from a loaded protobuf
Descriptors.Descriptor. This is the gRPC analogue of the GraphQL
GraphQLResponseSynthesizer and the OpenAPI ExampleBuilder: given a
message type from a descriptor loaded via PUT /mockserver/grpc/descriptors, it
walks the message's fields and emits a deterministic, type-correct placeholder value for
every field — no hand-authored response body required.
The synthesizer builds a real DynamicMessage rather than hand-rolling JSON, so
the result is guaranteed to be a valid instance of the message type and round-trips
cleanly through GrpcJsonMessageConverter (well-known types such as
google.protobuf.Timestamp are rendered by the standard protobuf-JSON printer when
the type registry knows them).
Synthesis rules:
- scalars produce deterministic placeholders by Java/proto type
(string
"string", numbers0/0.0, booltrue, bytes empty) - enums produce their first declared value (index 0)
- nested messages recurse into a synthesized sub-message
- repeated fields produce a single synthesized element
- map fields produce a single synthesized entry
- well-known message types (
Timestamp,Duration, the scalar wrappers,Struct/Value/Empty) get sensible canonical values - recursion is bounded; a message type already on the current path is left at its proto default to avoid infinite recursion on self-referential schemas
Instances are stateless and thread-safe.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionsynthesizeJson(com.google.protobuf.Descriptors.Descriptor messageDescriptor, GrpcJsonMessageConverter converter) Synthesize a schema-valid example message for the supplied message descriptor and render it as protobuf-JSON via the supplied converter.com.google.protobuf.DynamicMessagesynthesizeMessage(com.google.protobuf.Descriptors.Descriptor messageDescriptor) Synthesize a schema-valid exampleDynamicMessagefor the supplied message descriptor.
-
Constructor Details
-
GrpcExampleSynthesizer
public GrpcExampleSynthesizer()
-
-
Method Details
-
synthesizeJson
public String synthesizeJson(com.google.protobuf.Descriptors.Descriptor messageDescriptor, GrpcJsonMessageConverter converter) Synthesize a schema-valid example message for the supplied message descriptor and render it as protobuf-JSON via the supplied converter.- Parameters:
messageDescriptor- the message type to synthesize (e.g. a method's output type)converter- the converter used to render the message as protobuf-JSON- Returns:
- a protobuf-JSON string that is a valid instance of
messageDescriptor - Throws:
GrpcException- if synthesis or rendering fails
-
synthesizeMessage
public com.google.protobuf.DynamicMessage synthesizeMessage(com.google.protobuf.Descriptors.Descriptor messageDescriptor) Synthesize a schema-valid exampleDynamicMessagefor the supplied message descriptor.
-