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

bugfix json_format.py #16382

Closed
wants to merge 1 commit into from
Closed

Conversation

zhangzibao
Copy link
Contributor

In the _RegularMessageToJsonObject method, there is no str type conversion for non-bytes type values in the repeated content. This causes an exception in the MessageToJson method, as the jsons data does not allow for the occurrence of byte array type property values.

BUG EG (contains proto and python code):

------------common.proto------------
message SafetyInfo{//
repeated LoginDevice deviceList = 1;
}

message LoginDevice {
optional string uuid = 1 [default = ""];
optional string deviceName = 2 [default = ""];
optional string deviceType = 3 [default = ""];
required uint32 lastTime = 4;
}

------------test code(python)------------
from google.protobuf.json_format import MessageToJson

import common_pb2 # generate by common.proto

pb = common_pb2.SafetyInfo()
pb_hex = "0a4e0a2039323833663530356533363332396338356638623866343832613561323061651211636d4a38426c307a4d20446576696365731a0f6950686f6e6520694f532031332e3720dc85a9b00628010a410a206233356161326632366236343966313536393466663761336263303434323163120950432dcdacd0cbb4ef1a0a57696e646f777320313020d5d2c6e705280010001800200028003001" pb.ParseFromString(bytes.fromhex(pb_hex))
print(pb)
print(MessageToJson(pb))

In the _RegularMessageToJsonObject method, there is no str type conversion for non-bytes type values in the repeated content. This causes an exception in the MessageToJson method, as the jsons data does not allow for the occurrence of byte array type property values.

bug eg(contains proto and python code):

------common.proto------
message SafetyInfo{//
  repeated LoginDevice deviceList = 1;
}

message LoginDevice {
  optional string uuid = 1 [default = ""];
  optional string deviceName = 2 [default = ""];
  optional string deviceType = 3 [default = ""];
  required uint32 lastTime = 4;
}

------test code(python)------
from google.protobuf.json_format import MessageToJson

import common_pb2

pb = common_pb2.SafetyInfo()
pb_hex = "0a4e0a2039323833663530356533363332396338356638623866343832613561323061651211636d4a38426c307a4d20446576696365731a0f6950686f6e6520694f532031332e3720dc85a9b00628010a410a206233356161326632366236343966313536393466663761336263303434323163120950432dcdacd0cbb4ef1a0a57696e646f777320313020d5d2c6e705280010001800200028003001"
pb.ParseFromString(bytes.fromhex(pb_hex))
print(pb)
print(MessageToJson(pb))
@zhangzibao zhangzibao requested a review from a team as a code owner April 2, 2024 17:37
@zhangzibao zhangzibao requested review from anandolee and removed request for a team April 2, 2024 17:37
@acozzette acozzette added the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Apr 2, 2024
@github-actions github-actions bot removed the 🅰️ safe for tests Mark a commit as safe to run presubmits over label Apr 2, 2024
@copybara-service copybara-service bot closed this in f6bcf9c Apr 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants