Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: do not throw deprecated warning on field getters for default values #17788

Closed
38 changes: 38 additions & 0 deletions php/tests/GeneratedClassTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,44 @@ public function testDeprecatedInt32Field()
$this->assertSame(4, $deprecationCount);
}

public function testDeprecatedInt32FieldGetterDoesNotThrowWarning()
{
// temporarily change error handler to capture the deprecated errors
$deprecationCount = 0;
set_error_handler(function ($errno, $errstr) use (&$deprecationCount) {
if ($errstr === 'deprecated_optional_int32 is deprecated.') {
$deprecationCount++;
}
}, E_USER_DEPRECATED);

// does not throw warning
$message = new TestMessage();
$message->getDeprecatedOptionalInt32();

restore_error_handler();

$this->assertEquals(0, $deprecationCount);
}

public function testDeprecatedFieldGetterThrowsWarningWithValue()
{
$message = new TestMessage(['deprecated_optional_int32' => 1]);

// temporarily change error handler to capture the deprecated errors
$deprecationCount = 0;
set_error_handler(function ($errno, $errstr) use (&$deprecationCount) {
if ($errstr === 'deprecated_optional_int32 is deprecated.') {
$deprecationCount++;
}
}, E_USER_DEPRECATED);

$message->getDeprecatedOptionalInt32();

restore_error_handler();

$this->assertEquals(1, $deprecationCount);
}

#########################################################
# Test optional int32 field.
#########################################################
Expand Down
21 changes: 17 additions & 4 deletions src/google/protobuf/compiler/php/php_generator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,17 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
? absl::StrCat("@trigger_error('", field->name(),
" is deprecated.', E_USER_DEPRECATED);\n ")
: "";
std::string deprecation_trigger_with_conditional =
(field->options().deprecated())
? absl::StrCat("if ($this->", field->name(),
" !== ",
field->is_repeated() || field->real_containing_oneof()
|| field->has_presence()
? "null"
: DefaultForField(field),
") {\n ", deprecation_trigger,
"}\n ")
: "";

// Emit getter.
if (oneof != nullptr) {
Expand All @@ -584,7 +595,7 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true), "number",
IntToString(field->number()), "deprecation_trigger",
deprecation_trigger);
deprecation_trigger_with_conditional);
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
} else if (field->has_presence() && !field->message_type()) {
printer->Print(
"public function get^camel_name^()\n"
Expand All @@ -594,15 +605,16 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true), "name",
field->name(), "default_value", DefaultForField(field),
"deprecation_trigger", deprecation_trigger);
"deprecation_trigger", deprecation_trigger_with_conditional);
} else {
printer->Print(
"public function get^camel_name^()\n"
"{\n"
" ^deprecation_trigger^return $this->^name^;\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true), "name",
field->name(), "deprecation_trigger", deprecation_trigger);
field->name(), "deprecation_trigger",
deprecation_trigger_with_conditional);
}

// Emit hazzers/clear.
Expand Down Expand Up @@ -642,7 +654,8 @@ void GenerateFieldAccessor(const FieldDescriptor* field, const Options& options,
"$this->readWrapperValue(\"^field_name^\");\n"
"}\n\n",
"camel_name", UnderscoresToCamelCase(field->name(), true), "field_name",
field->name(), "deprecation_trigger", deprecation_trigger);
field->name(), "deprecation_trigger",
deprecation_trigger_with_conditional);
bshaffer marked this conversation as resolved.
Show resolved Hide resolved
}

// Generate setter.
Expand Down
Loading