This CL adds support for the Edition 2026 custom JSON enum name feature (pb.enumvalue.json) in PHP Protobuf.
Note that we don't need extension caching similar to C++ or Java because our PHP code generator collects custom JSON names at protoc compile time and passes them to DescriptorPool::internalAddGeneratedFile(), populating EnumValueDescriptor and EnumDescriptor during descriptor registration.
Tested with PHP unit tests across pure-PHP and C-extension modes.
PiperOrigin-RevId: 963411589
This limit has no public api to override to a different limit: it is only ever set to the 'default' of 32 MB but has actually just not applied at all in many cases.
If we 'fix the bug' by making it so this limit does apply, it would break any users who may be successfully parsing data arbitrarily larger than 32 MB with no issue today due to hitting a case where limit is consistently not applied. By contrast, removing this doesn't formally break any preexisting users, and realigns the behavior to match PHP-upb as well as the default behavior in all of our other supported runtimes.
Note that gRPC-PHP will also already enforce a 4mb limit by default before passing the data to Protobuf, so the only people who could be exposed to this topic either way would need to be either intentionally bumping up the gRPC limit or not using gRPC at all.
In general the need to have a limit within CodedInputStream is very weak in this case compared to other languages: since PHP only parses off of a string and not a stream, it trivial for application code to apply a limit with an if before the parse starts.
PiperOrigin-RevId: 950914431
## What
Adds an optional `$recursion_limit` parameter to `Message::mergeFromString()` and `Message::serializeToString()` in the PHP runtime:
```
$msg->mergeFromString($data, $recursion_limit = 100);
$msg->serializeToString($recursion_limit = 100);
```
## Why
PHP hardcoded a nesting depth of 100 with no way to raise it, while Ruby/Java/C++/C# already expose this. Deeply nested messages (e.g. PostgreSQL parse trees via pg_query) hit "Max nesting exceeded" with no recourse. The underlying upb library already supports it. Fixes#27840.
## How
- C extension (message.c): threads the limit through upb via upb_DecodeOptions_MaxDepth / upb_EncodeOptions_MaxDepth on both decode and encode; validates the range (1–65535).
- Pure PHP: passes the limit into CodedInputStream so the decoder honors a raised depth. The pure-PHP encoder has no depth guard, so the argument is accepted on serializeToString for API parity but is enforced only by the C extension.
Defaults to 100, so existing behavior is unchanged.
Closes#27874
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/27874 from norberttech:php-ext-recursion-limit c84eb87584
PiperOrigin-RevId: 933223447
This PR addresses issue #18808 regarding locale-dependent JSON serialization in PHP.
### Problem
When the system environment is set to a locale that uses a comma as a decimal separator (like `de_DE`), `float` and `double` values were being serialized into JSON as `3,14` instead of `3.14`. This produced invalid JSON that could not be parsed by other systems.
### Solution
- Updated php/src/Google/Protobuf/Internal/GPBJsonWire.php to use `sprintf("%.*h", ...)` for formatting values. This ensures that the decimal separator is always a dot (`.`), making the output locale-independent.
- Added a new test case testJsonEncodeFloatLocaleIndependent in php/tests/EncodeDecodeTest.php. This test explicitly sets the locale to `de_DE` to verify that the fix works correctly and produces valid JSON.
### Verification
- [x] Added new regression test.
- [x] Verified that all PHP tests pass locally: `vendor/bin/phpunit php/tests/EncodeDecodeTest.php`
Fixes#18808Closes#25329
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/25329 from khdkkhdd:18808-php-float-fix 5de303962e
PiperOrigin-RevId: 911348844
Unfortunately, because of inherent design choices in the ProtoJSON format, Field Masks of fields which contain an underscore followed by an uppercase letter or number cannot be successfully round tripped through the ProtoJSON representation.
The behavior today is that PHP-upb errors on this case but doesn't explain to the user why it has the check that it does. Pure-PHP produces JSON that will just not round-trip parse back.
This change improves the error message in the upb case, and adds a log-warning in the the pure-PHP case with a similar message.
Fixes https://github.com/protocolbuffers/protobuf/issues/25786
PiperOrigin-RevId: 908697453
This is a reopening of https://github.com/protocolbuffers/protobuf/issues/6035, where it was requested that someone contribute a PR. I chose the option name `EMIT_DEFAULTS` to match the ruby client and existing upd option. I tried to be relatively comprehensive with the tests, but let me know if there's anything stylistically or logically you want changed.
Locally the tests run happily:
```
php % git rev-parse HEAD
7cb3b2d9f60bdffce69c01143ca15c46d2eaa900
php % php -dextension=ext/google/protobuf/modules/protobuf.so -d error_reporting="E_ALL & ~E_DEPRECATED" vendor/bin/phpunit --bootstrap tests/force_c_ext.php tests/EncodeDecodeTest.php
PHPUnit 8.5.26 #StandWithUkraine
............................................................... 63 / 147 ( 42%)
............................................................... 126 / 147 ( 85%)
..................... 147 / 147 (100%)
Time: 49 ms, Memory: 6.00 MB
OK (147 tests, 1150 assertions)
```
Closes#23985
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/23985 from SpencerMalone:php-default-value-option 6d8bc74056
PiperOrigin-RevId: 821657589
The php extension already had an implementation for encoding enums as integers, but it was not exposed, so add a bitmask param to `Message::serializeToJsonString()` to enable it as well as `preserve_proto_field_names`.
Converted the existing boolean parameter to preferentially be an integer (with boolean handling for BC), which accepts a bitmask of `Google\Protobuf\PrintOptions::*`, eg `PrintOptions::ALWAYS_PRINT_ENUMS_AS_INTS | PrintOptions::PRESERVE_PROTO_FIELD_NAMES`
`PrintOptions` class name and constant names were chosen to align with protobuf-cpp's implementation.
Implemented `preserve_proto_fieldnames` in the native version, to match the existing implementation in the extension (with tests).
Closes#12707
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12707 from brettmc:php-json-enum-as-int 515a0839b7
PiperOrigin-RevId: 725806573
Protobuf php lib encodes 123_000_000 nano like this: 2000-01-01T00:00:00.**123**Z but then it gets decoded into 123 nanoseconds instead of 123_000_000.
There were issue opened some time ago that also describes this behaviour #4335Closes#12396
COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12396 from kindratmakc:bugfix/inconsistent-timestamp-json-encode-decode df47c96567
PiperOrigin-RevId: 603118615
* Updated PHP to the new version of upb.
This is a large change, as the upb API surface has been
renamed to follow Google style more closely.
* Fixed utf8_range.
* Updated Ruby for new utf8_range.
* Picked up new upb for PHP, with spelling fixes.
* Fixed the 32-bit build.
* [PHP] Fixed $msg->setMessage(null) to properly clear the message.
Fixes: https://github.com/protocolbuffers/protobuf/issues/8457
* Changed pure PHP to throw TypeError, and added a test for null.
* Added more tests and fixed null in setter for oneof.
* Fixed a bunch of incorrect arginfo and a few incorrect error messages.
* Passes mem check test with no leaks!
* WIP.
* Fix build warning that was causing Bazel build to fail.
* Added compatibility code for PHP <8.0.
* Added test_valgrind target and made tests Valgrind-clean.
* Updated Valgrind test to fail if memory leaks are detected.
* Removed intermediate shell script so commands are easier to cut, paste, and modify.
* Passing all Valgrind tests!
* Hoist addref into ObjCache_Get().
* Removed special case of map descriptors by keying object map on upb_msgdef.
* Removed all remaining RETURN_ZVAL() macros.
* Removed all explicit reference add/del operations.
* Added REFCOUNTING.md to Makefile.am.
* Port for php8
* Port php c extension for php8
* Update composer.json
* Drop php7.0 support
* Update phpunit for php7.1 in c extension test
* Add back support for php7.0
* Add badge for php8 continuous build
* Only ported c extension to php8.
* Didn't fixed the issue of throwing warnings for missing arginfo in bundled files.
* Tests not fixed, because syntax of phpunit (<7 vs >9.3) are not compatible.
* In next release, needs to drop php5 and php7.0 support (in order to use phpunit > 7)
2020-08-11 19:30:46 -07:00
Renamed from php/tests/encode_decode_test.php (Browse further)