Commit graph

23 commits

Author SHA1 Message Date
Tony Liao
1b27892b8b Support custom JSON enum names in PHP
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
2026-08-12 06:45:04 -07:00
Protobuf Team Bot
ed612a8b43 Remove total_bytes_limit from pure PHP CodedInputStream.
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
2026-07-20 10:24:15 -07:00
Norbert Orzechowicz
5c5ee3c9c7 [PHP] - Add configurable recursion_limit to mergeFromString / serializeToString (#27874)
## 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
2026-06-16 12:06:04 -07:00
Dong Kim
32d75b6bbc [PHP] Fix locale-dependent JSON float encoding (#18808) (#25329)
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 #18808

Closes #25329

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/25329 from khdkkhdd:18808-php-float-fix 5de303962e
PiperOrigin-RevId: 911348844
2026-05-06 08:23:01 -07:00
Protobuf Team Bot
0488596867 Clarify FieldMask JSON warnings/errors in the situation of fields which aren't round-trip representable in JSON format.
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
2026-05-01 06:18:43 -07:00
Protobuf Team Bot
b9ab5812ce Bump version dependencies on phpunit/phpunit to ">=11.5.0 <12.0.0"
PiperOrigin-RevId: 861973659
2026-01-27 18:34:56 -08:00
Protobuf Team Bot
272f68a6bb php: disable recursive tests for valgrind.
PiperOrigin-RevId: 857306260
2026-01-16 14:35:13 -08:00
Protobuf Team Bot
12d5002cc4 php: Fix that recursion limit is not enforced.
https://github.com/protocolbuffers/protobuf/issues/25067

PiperOrigin-RevId: 856286406
2026-01-14 11:11:41 -08:00
Protobuf Team Bot
8cfe0c86e2 Check that readRaw does not accept negative length value.
Fixes https://github.com/protocolbuffers/protobuf/issues/24159

PiperOrigin-RevId: 855837030
2026-01-13 12:33:40 -08:00
Spencer Malone
6df6c8aace Add option for PHP to emit default values for JSON. (#23985)
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
2025-10-20 08:28:16 -07:00
Brett McBride
40ec76ef67 [PHP] allow encoding enums as integers for json (#12707)
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
2025-02-11 16:10:13 -08:00
Anton Grbin
1c98b5ba47 [7392] fix conformance test in PHP JSON parser (#19376)
# Motivation

This PR fixes failing JSON conformance tests for php with name `IgnoreUnknownEnumStringValue*`.

The JSON parsing spec was discussed in https://github.com/protocolbuffers/protobuf/issues/7392.

Recent similar changes in other languages:
- Python: 86abf35ef5
- Swift: https://github.com/apple/swift-protobuf/pull/1345
- C#: https://github.com/protocolbuffers/protobuf/pull/15758
- C++: https://github.com/protocolbuffers/protobuf/pull/16479

Note: this PR is equivalent to https://github.com/protocolbuffers/protobuf/pull/16743. I had to create a new one since I lost access to noom/protobuf in the meantime (switched companies recently).

Closes #19376

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/19376 from antongrbin:anton--7392--php-newbranch 641a28a2b8
PiperOrigin-RevId: 699989555
2024-11-25 08:28:42 -08:00
Joshua Haberman
5a91d6fe5e [php] Added a unit test that bad UTF-8 is rejected in the parser.
PiperOrigin-RevId: 632274113
2024-05-09 14:42:05 -07:00
kindratmakc
47e92cd689 Fix inconsistent timestamp json encode/decode (#12396)
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 #4335

Closes #12396

COPYBARA_INTEGRATE_REVIEW=https://github.com/protocolbuffers/protobuf/pull/12396 from kindratmakc:bugfix/inconsistent-timestamp-json-encode-decode df47c96567
PiperOrigin-RevId: 603118615
2024-01-31 12:01:31 -08:00
Sandy Zhang
81068e8e8c Internal change
PiperOrigin-RevId: 566426899
2023-09-18 15:17:56 -07:00
zajca
743c675c4f fix empty message serialization for Any 2022-09-16 12:03:10 +02:00
Joshua Haberman
349d74d92e
Updated PHP upb, and utf8_range for Ruby/PHP (#9585)
* 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.
2022-03-05 11:55:57 -08:00
Joshua Haberman
7e95c64dfb
[PHP] Fixed $msg->setMessage(null) to properly clear the message. (#8472)
* [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.
2021-05-14 16:48:45 -07:00
Joshua Haberman
b0d90e3abe
Fixed PHP memory leaks and arginfo errors (#8614)
* 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.
2021-05-13 23:16:49 -07:00
Paul Yang
114efc42d4
Fix php json parsing not throwing error for unknown field. (#8528) 2021-05-04 10:09:56 -07:00
Joshua Haberman
d5079ed7db
Fixed PHP to support field numbers >=2**28. (#8235) 2021-01-27 10:12:59 -08:00
Paul Yang
823f351448
Port for php8 (#8105)
* 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
2020-12-04 13:46:34 -08:00
Paul Yang
d4ca92962c
Port php c extension to php8 (#7793)
* 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)