@@ -73,8 +73,8 @@ public function serialize(Message $message): string
73
73
// @phpstan-ignore-next-line
74
74
return match ($ this ->contentType ) {
75
75
ContentTypes::PROTOBUF => $ message ->serializeToString (),
76
- ContentTypes::JSON => self ::postProcessJsonEnumValues ($ message, $ message -> serializeToJsonString () ),
77
- ContentTypes::NDJSON => self ::postProcessJsonEnumValues ($ message, $ message -> serializeToJsonString () ) . "\n" ,
76
+ ContentTypes::JSON => self ::serializeToJsonString ($ message ),
77
+ ContentTypes::NDJSON => self ::serializeToJsonString ($ message ) . "\n" ,
78
78
};
79
79
}
80
80
@@ -92,16 +92,21 @@ public function hydrate(Message $message, string $payload): void
92
92
}
93
93
94
94
/**
95
- * Workaround until protobuf exposes `FormatEnumsAsIntegers` option.
96
- *
97
95
* [JSON Protobuf Encoding](https://opentelemetry.io/docs/specs/otlp/#json-protobuf-encoding):
98
96
* > Values of enum fields MUST be encoded as integer values.
99
97
*
100
98
* @see https://github.com/open-telemetry/opentelemetry-php/issues/978
101
99
* @see https://github.com/protocolbuffers/protobuf/pull/12707
102
100
*/
103
- private static function postProcessJsonEnumValues (Message $ message, string $ payload ): string
101
+ private static function serializeToJsonString (Message $ message ): string
104
102
{
103
+ // @phan-suppress-next-line PhanUndeclaredClassReference
104
+ if (\class_exists (\Google \Protobuf \PrintOptions::class)) {
105
+ /** @psalm-suppress TooManyArguments @phan-suppress-next-line PhanParamTooManyInternal,PhanUndeclaredClassConstant @phpstan-ignore arguments.count */
106
+ return $ message ->serializeToJsonString (\Google \Protobuf \PrintOptions::ALWAYS_PRINT_ENUMS_AS_INTS );
107
+ }
108
+
109
+ $ payload = $ message ->serializeToJsonString ();
105
110
$ pool = DescriptorPool::getGeneratedPool ();
106
111
$ desc = $ pool ->getDescriptorByClassName ($ message ::class);
107
112
if (!$ desc instanceof Descriptor) {
0 commit comments