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: Replace deprecated protobuf methods. #2764

Merged
merged 6 commits into from May 15, 2024
Merged

Conversation

blakeli0
Copy link
Collaborator

@blakeli0 blakeli0 commented May 11, 2024

isSynthetic() is deprecated and will be removed in protobuf-java 4.26.0, replace it so that we can prepare for protobuf-java upgrade.

Per official doc of protobuf, an optional field is implemented as a "synthetic" oneof field internally. Also see b/266950618#comment123 for suggested replacements of isSynthetic().

@blakeli0 blakeli0 requested a review from a team as a code owner May 11, 2024 06:01
@product-auto-label product-auto-label bot added the size: xs Pull request size is extra small. label May 11, 2024
@blakeli0 blakeli0 requested review from suztomo and lqiu96 May 14, 2024 21:17
Copy link

sonarcloud bot commented May 14, 2024

Copy link

sonarcloud bot commented May 14, 2024

Quality Gate Passed Quality Gate passed for 'java_showcase_integration_tests'

Issues
0 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
No data about Duplication

See analysis details on SonarCloud

Comment on lines 1068 to +1069
fieldDescriptor.getContainingOneof() != null
&& fieldDescriptor.getContainingOneof().isSynthetic())
&& fieldDescriptor.getRealContainingOneof() == null)
Copy link
Contributor

@lqiu96 lqiu96 May 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can this just be fieldDescriptor.getRealContainingOneof() == null)? The implementation looks like it's already checking for fieldDescriptor.getContaintingOneof() != null: https://github.com/protocolbuffers/protobuf/blob/dde03553c92867184ff5d351b7f087c052f39459/java/core/src/main/java/com/google/protobuf/Descriptors.java#L1435-L1438

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a fieldDescriptor does not contain any oneof(real or synthetic) field, fieldDescriptor.getRealContainingOneof() == null would return true, but fieldDescriptor.getContainingOneof() != null && fieldDescriptor.getRealContainingOneof() == null would return false.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. It needs to check that it's a oneOf before checking for the optional implementation or not.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. That being said, based on the field name isProto3Optional, I'm not sure the current logic is what it intended to be. I feel like fieldDescriptor.isOptional() may make more sense but it would introduce changes to the generated code. So I decided to go with the safest approach for now, and we can improve it in the future if we see issues.

Copy link
Contributor

@lqiu96 lqiu96 May 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like fieldDescriptor.isOptional() may make more sense but it would introduce changes to the generated code.

Hmm, the more I look at this, the more I'm confusing myself. My assumption is that this logic is for code written with pre protobuf v3.15 compatibility in mind (i.e. people writing their own optional workaround -- writing their own oneof with a single field).

Looking at the implementation of isOptional makes it seems like it should work except for the optional workaround case (i.e. writing their own oneof with a single field).

I'm not entirely sure if protoc would have something like this marked as synthetic. This is where the user explicitly writes their optional workaround:

  oneof _foo {
    int32 foo = 1;
  }

I know that under the hood that an optional int32 foo is converted to it above, but I'm not sure the descriptor marks things correctly if the user explicitly writes it out. I'm guessing this is the case since they had backwards compatibility in mind and I'll probably need to test this myself whenever I get some time.

I think the method probably should be:

// Renamed to IsOptional and checks for the optional label and the optional workaround
// to keep the existing generator behavior the same. If protobuf decides to remove the
// under the hood conversion of optional -> oneof, then `isOptional()` should suffice.
.setIsOptional( 
            fieldDescriptor.isOptional() ||
            (fieldDescriptor.getContainingOneof() != null
                && fieldDescriptor.getRealContainingOneof() == null))

but the existing implementation should work and this can be something we look again in the future.

@blakeli0 blakeli0 merged commit 986c090 into main May 15, 2024
33 checks passed
@blakeli0 blakeli0 deleted the fix-deprecated-proto-methods branch May 15, 2024 02:28
JoeWang1127 added a commit that referenced this pull request May 16, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>2.40.1</summary>

##
[2.40.1](v2.40.0...v2.40.1)
(2024-05-15)


### Bug Fixes

* [common-protos] An existing method `UpdateVehicleLocation` is
([7f96074](7f96074))
* [common-protos] An existing method `UpdateVehicleLocation` is removed
from service `VehicleService`
([#2751](#2751))
([7f96074](7f96074))
* [iam] An existing method `UpdateVehicleLocation` is removed from
([4a1ae7b](4a1ae7b))
* [iam] An existing method `UpdateVehicleLocation` is removed from
service `VehicleService`
([#2752](#2752))
([4a1ae7b](4a1ae7b))
* do not populate repo level change while removing library
([#2740](#2740))
([43e62b9](43e62b9))
* only append `.api.grpc` suffix to group id if the artifact id starts
with `proto-` or `grpc-`
([#2731](#2731))
([8e87b2e](8e87b2e))
* opentelemetry-bom to be in third-party-dependencies BOM
([#2736](#2736))
([4ecc89b](4ecc89b))
* prepare to generate grafeas
([#2761](#2761))
([1114f18](1114f18))
* Replace deprecated protobuf methods.
([#2764](#2764))
([986c090](986c090))


### Dependencies

* update dependency black to v24.4.2
([#2660](#2660))
([1cbb681](1cbb681))
* update dependency com.fasterxml.jackson:jackson-bom to v2.17.1
([#2732](#2732))
([891b01d](891b01d))
* update dependency com.google.cloud:grpc-gcp to v1.6.0
([#2767](#2767))
([a39aa07](a39aa07))
* update dependency com.google.errorprone:error_prone_annotations to
v2.27.1
([#2708](#2708))
([4d7d246](4d7d246))
* update dependency com.google.errorprone:error_prone_annotations to
v2.27.1
([#2709](#2709))
([4e31d7d](4e31d7d))
* update dependency com.google.oauth-client:google-oauth-client-bom to
v1.36.0
([#2768](#2768))
([22b7398](22b7398))
* update dependency commons-codec:commons-codec to v1.17.0
([#2710](#2710))
([b87356c](b87356c))
* update dependency jinja2 to v3.1.4 [security]
([#2742](#2742))
([d67eaf8](d67eaf8))
* update dependency lxml to v5.2.2
([#2766](#2766))
([df7e211](df7e211))
* update dependency markupsafe to v2.1.5
([#2657](#2657))
([805baf8](805baf8))
* update dependency net.bytebuddy:byte-buddy to v1.14.15
([#2753](#2753))
([a472620](a472620))
* update dependency platformdirs to v4.2.1
([#2662](#2662))
([dbdcc91](dbdcc91))
* update googleapis/java-cloud-bom digest to db4265f
([#2755](#2755))
([908db6f](908db6f))
* update googleapis/java-cloud-bom digest to f3c611a
([#2700](#2700))
([d254e9b](d254e9b))
* update opentelemetry-java monorepo to v1.38.0
([#2769](#2769))
([0a5c7c4](0a5c7c4))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Joe Wang <106995533+JoeWang1127@users.noreply.github.com>
lqiu96 pushed a commit that referenced this pull request May 16, 2024
`isSynthetic()` is deprecated and will be
[removed](protocolbuffers/protobuf@1aeacd4#diff-2228551d02c6661809ca7103db9512eef4c2d01f35556d42316543d92a89edefL2846-L2847)
in protobuf-java 4.26.0, replace it so that we can prepare for
protobuf-java upgrade.

Per [official
doc](https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md)
of protobuf, an optional field is implemented as a "synthetic" oneof
field internally. Also see b/266950618#comment123 for suggested
replacements of `isSynthetic()`.
lqiu96 pushed a commit that referenced this pull request May 16, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>2.40.1</summary>

##
[2.40.1](v2.40.0...v2.40.1)
(2024-05-15)


### Bug Fixes

* [common-protos] An existing method `UpdateVehicleLocation` is
([7f96074](7f96074))
* [common-protos] An existing method `UpdateVehicleLocation` is removed
from service `VehicleService`
([#2751](#2751))
([7f96074](7f96074))
* [iam] An existing method `UpdateVehicleLocation` is removed from
([4a1ae7b](4a1ae7b))
* [iam] An existing method `UpdateVehicleLocation` is removed from
service `VehicleService`
([#2752](#2752))
([4a1ae7b](4a1ae7b))
* do not populate repo level change while removing library
([#2740](#2740))
([43e62b9](43e62b9))
* only append `.api.grpc` suffix to group id if the artifact id starts
with `proto-` or `grpc-`
([#2731](#2731))
([8e87b2e](8e87b2e))
* opentelemetry-bom to be in third-party-dependencies BOM
([#2736](#2736))
([4ecc89b](4ecc89b))
* prepare to generate grafeas
([#2761](#2761))
([1114f18](1114f18))
* Replace deprecated protobuf methods.
([#2764](#2764))
([986c090](986c090))


### Dependencies

* update dependency black to v24.4.2
([#2660](#2660))
([1cbb681](1cbb681))
* update dependency com.fasterxml.jackson:jackson-bom to v2.17.1
([#2732](#2732))
([891b01d](891b01d))
* update dependency com.google.cloud:grpc-gcp to v1.6.0
([#2767](#2767))
([a39aa07](a39aa07))
* update dependency com.google.errorprone:error_prone_annotations to
v2.27.1
([#2708](#2708))
([4d7d246](4d7d246))
* update dependency com.google.errorprone:error_prone_annotations to
v2.27.1
([#2709](#2709))
([4e31d7d](4e31d7d))
* update dependency com.google.oauth-client:google-oauth-client-bom to
v1.36.0
([#2768](#2768))
([22b7398](22b7398))
* update dependency commons-codec:commons-codec to v1.17.0
([#2710](#2710))
([b87356c](b87356c))
* update dependency jinja2 to v3.1.4 [security]
([#2742](#2742))
([d67eaf8](d67eaf8))
* update dependency lxml to v5.2.2
([#2766](#2766))
([df7e211](df7e211))
* update dependency markupsafe to v2.1.5
([#2657](#2657))
([805baf8](805baf8))
* update dependency net.bytebuddy:byte-buddy to v1.14.15
([#2753](#2753))
([a472620](a472620))
* update dependency platformdirs to v4.2.1
([#2662](#2662))
([dbdcc91](dbdcc91))
* update googleapis/java-cloud-bom digest to db4265f
([#2755](#2755))
([908db6f](908db6f))
* update googleapis/java-cloud-bom digest to f3c611a
([#2700](#2700))
([d254e9b](d254e9b))
* update opentelemetry-java monorepo to v1.38.0
([#2769](#2769))
([0a5c7c4](0a5c7c4))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Joe Wang <106995533+JoeWang1127@users.noreply.github.com>
lqiu96 pushed a commit that referenced this pull request May 22, 2024
`isSynthetic()` is deprecated and will be
[removed](protocolbuffers/protobuf@1aeacd4#diff-2228551d02c6661809ca7103db9512eef4c2d01f35556d42316543d92a89edefL2846-L2847)
in protobuf-java 4.26.0, replace it so that we can prepare for
protobuf-java upgrade.

Per [official
doc](https://github.com/protocolbuffers/protobuf/blob/main/docs/implementing_proto3_presence.md)
of protobuf, an optional field is implemented as a "synthetic" oneof
field internally. Also see b/266950618#comment123 for suggested
replacements of `isSynthetic()`.
lqiu96 pushed a commit that referenced this pull request May 22, 2024
🤖 I have created a release *beep* *boop*
---


<details><summary>2.40.1</summary>

##
[2.40.1](v2.40.0...v2.40.1)
(2024-05-15)


### Bug Fixes

* [common-protos] An existing method `UpdateVehicleLocation` is
([7f96074](7f96074))
* [common-protos] An existing method `UpdateVehicleLocation` is removed
from service `VehicleService`
([#2751](#2751))
([7f96074](7f96074))
* [iam] An existing method `UpdateVehicleLocation` is removed from
([4a1ae7b](4a1ae7b))
* [iam] An existing method `UpdateVehicleLocation` is removed from
service `VehicleService`
([#2752](#2752))
([4a1ae7b](4a1ae7b))
* do not populate repo level change while removing library
([#2740](#2740))
([43e62b9](43e62b9))
* only append `.api.grpc` suffix to group id if the artifact id starts
with `proto-` or `grpc-`
([#2731](#2731))
([8e87b2e](8e87b2e))
* opentelemetry-bom to be in third-party-dependencies BOM
([#2736](#2736))
([4ecc89b](4ecc89b))
* prepare to generate grafeas
([#2761](#2761))
([1114f18](1114f18))
* Replace deprecated protobuf methods.
([#2764](#2764))
([986c090](986c090))


### Dependencies

* update dependency black to v24.4.2
([#2660](#2660))
([1cbb681](1cbb681))
* update dependency com.fasterxml.jackson:jackson-bom to v2.17.1
([#2732](#2732))
([891b01d](891b01d))
* update dependency com.google.cloud:grpc-gcp to v1.6.0
([#2767](#2767))
([a39aa07](a39aa07))
* update dependency com.google.errorprone:error_prone_annotations to
v2.27.1
([#2708](#2708))
([4d7d246](4d7d246))
* update dependency com.google.errorprone:error_prone_annotations to
v2.27.1
([#2709](#2709))
([4e31d7d](4e31d7d))
* update dependency com.google.oauth-client:google-oauth-client-bom to
v1.36.0
([#2768](#2768))
([22b7398](22b7398))
* update dependency commons-codec:commons-codec to v1.17.0
([#2710](#2710))
([b87356c](b87356c))
* update dependency jinja2 to v3.1.4 [security]
([#2742](#2742))
([d67eaf8](d67eaf8))
* update dependency lxml to v5.2.2
([#2766](#2766))
([df7e211](df7e211))
* update dependency markupsafe to v2.1.5
([#2657](#2657))
([805baf8](805baf8))
* update dependency net.bytebuddy:byte-buddy to v1.14.15
([#2753](#2753))
([a472620](a472620))
* update dependency platformdirs to v4.2.1
([#2662](#2662))
([dbdcc91](dbdcc91))
* update googleapis/java-cloud-bom digest to db4265f
([#2755](#2755))
([908db6f](908db6f))
* update googleapis/java-cloud-bom digest to f3c611a
([#2700](#2700))
([d254e9b](d254e9b))
* update opentelemetry-java monorepo to v1.38.0
([#2769](#2769))
([0a5c7c4](0a5c7c4))
</details>

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Co-authored-by: Joe Wang <106995533+JoeWang1127@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size: xs Pull request size is extra small.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants