Add kotlin deprecation to messageOrNull extension properties in the generated code when the corresponding message is deprecated.

Additionally adds tests to ensure deprecated fields are properly annotated as such.

Fixes https://github.com/protocolbuffers/protobuf/issues/17084.

PiperOrigin-RevId: 653731276
pull/17412/head
Deanna Garcia 7 months ago committed by Copybara-Service
parent a5aa1ba986
commit 501fb26927
  1. 1
      java/kotlin/BUILD.bazel
  2. 13
      java/kotlin/src/test/kotlin/com/google/protobuf/Proto2Test.kt
  3. 5
      src/google/protobuf/compiler/java/full/message.cc
  4. 1
      src/google/protobuf/compiler/java/full/message_field.cc
  5. 5
      src/google/protobuf/compiler/java/lite/message.cc
  6. 4
      src/google/protobuf/compiler/java/lite/message_field.cc

@ -276,6 +276,7 @@ kt_jvm_library(
"//java/core:test_util",
"@maven//:com_google_truth_truth",
"@maven//:junit_junit",
"@rules_kotlin//kotlin/compiler:kotlin-reflect",
],
)

@ -26,12 +26,14 @@ import protobuf_unittest.UnittestProto.TestAllTypes.NestedEnum
import protobuf_unittest.UnittestProto.TestEmptyMessage
import protobuf_unittest.UnittestProto.TestEmptyMessageWithExtensions
import protobuf_unittest.copy
import protobuf_unittest.deprecatedMessageOrNull
import protobuf_unittest.foreignMessage
import protobuf_unittest.optionalGroupExtension
import protobuf_unittest.optionalNestedMessageOrNull
import protobuf_unittest.repeatedGroupExtension
import protobuf_unittest.testAllExtensions
import protobuf_unittest.testAllTypes
import protobuf_unittest.testDeprecatedFields
import protobuf_unittest.testEmptyMessage
import protobuf_unittest.testEmptyMessageWithExtensions
import protobuf_unittest.testEnumMap
@ -959,4 +961,15 @@ class Proto2Test {
assertThat(someNestedMessage.optionalNestedMessageOrNull)
.isEqualTo(TestAllTypesKt.nestedMessage { bb = 118 })
}
@Test
fun testDeprecated() {
val testInstance =
protobuf_unittest.UnittestProto.TestDeprecatedFields.getDefaultInstance()
assertThat(testInstance::deprecatedMessageOrNull.annotations.any { it is Deprecated }).isTrue()
val unused = testDeprecatedFields {
assertThat(::deprecatedMessage.annotations.any { it is Deprecated }).isTrue()
}
}
}

@ -1330,6 +1330,11 @@ void ImmutableMessageGenerator::GenerateKotlinOrNull(
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
if (field->has_presence() && GetJavaType(field) == JAVATYPE_MESSAGE) {
if (field->options().deprecated()) {
printer->Print(
"@kotlin.Deprecated(message = \"Field $name$ is deprecated\")\n",
"name", context_->GetFieldGeneratorInfo(field)->name);
}
printer->Print(
"public val $full_classname$OrBuilder.$camelcase_name$OrNull: "
"$full_name$?\n"

@ -409,6 +409,7 @@ void ImmutableMessageFieldGenerator::GenerateKotlinOrNull(io::Printer* printer)
if (descriptor_->has_presence() &&
descriptor_->real_containing_oneof() == nullptr) {
printer->Print(variables_,
"$kt_deprecation$\n"
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");
}

@ -854,6 +854,11 @@ void ImmutableMessageLiteGenerator::GenerateKotlinOrNull(
for (int i = 0; i < descriptor_->field_count(); i++) {
const FieldDescriptor* field = descriptor_->field(i);
if (field->has_presence() && GetJavaType(field) == JAVATYPE_MESSAGE) {
if (field->options().deprecated()) {
printer->Print(
"@kotlin.Deprecated(message = \"Field $name$ is deprecated\")\n",
"name", context_->GetFieldGeneratorInfo(field)->name);
}
printer->Print(
"public val $full_classname$OrBuilder.$camelcase_name$OrNull: "
"$full_name$?\n"

@ -309,10 +309,12 @@ void ImmutableMessageFieldLiteGenerator::GenerateKotlinDslMembers(
GenerateKotlinOrNull(printer);
}
void ImmutableMessageFieldLiteGenerator::GenerateKotlinOrNull(io::Printer* printer) const {
void ImmutableMessageFieldLiteGenerator::GenerateKotlinOrNull(
io::Printer* printer) const {
if (descriptor_->has_presence() &&
descriptor_->real_containing_oneof() == nullptr) {
printer->Print(variables_,
"$kt_deprecation$\n"
"public val $classname$Kt.Dsl.$name$OrNull: $kt_type$?\n"
" get() = $kt_dsl_builder$.$name$OrNull\n");
}

Loading…
Cancel
Save