Skip to content

Fix optional fields in proto3#16

Merged
mhidalgo-rai merged 5 commits into
rai-opensource:mainfrom
adamwhats:main
Nov 25, 2025
Merged

Fix optional fields in proto3#16
mhidalgo-rai merged 5 commits into
rai-opensource:mainfrom
adamwhats:main

Conversation

@adamwhats
Copy link
Copy Markdown
Contributor

Proposed changes

I think I've found an issue with handling optional fields. To replicate, add an optional field to a message in test.proto:

// A control request for an HVAC system.
message HVACControlRequest {
    double air_flow_rate = 1;
    Temperature temperature_setpoint = 2;
+   optional double humidity_setpoint = 3;
}

After building this gives the following error:

  File "/workspaces/proto2ros/install/proto2ros/lib/python3.12/site-packages/proto2ros/equivalences.py", line 379, in compute_equivalence_for_message
    oneof_type = Type(f"{ros_package_name}/{oneof_type_name}")
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/opt/ros/jazzy/lib/python3.12/site-packages/rosidl_adapter/parser.py", line 278, in __init__
    super(Type, self).__init__(
  File "/opt/ros/jazzy/lib/python3.12/site-packages/rosidl_adapter/parser.py", line 206, in __init__
    raise InvalidResourceName(
rosidl_adapter.parser.InvalidResourceName: 'HVACControlRequestOneOf_humiditySetpoint' is an invalid message name. It should have the pattern '^[A-Z][A-Za-z0-9]*$'

I think this is due to proto3 internally rewriting optional fields as a 'synthetic oneof' with a leading underscore, as documented here. This then causes issues with this line line, as .camelize() doesn't handle double underscores, hence we get oneof_type_name = HVACControlRequestOneOf_humiditySetpoint.

My fix has is a small change to equivalences.py where we only add oneof field descriptors without a leading underscore to oneof_field_sets, then a little bit of handling in the next loop below. I'm very open to feedback on this, there may well be a more elegant solution.

Checklist

  • Lint and unit tests pass locally
  • I have added tests that prove my changes are effective
  • I have added necessary documentation to communicate the changes

Signed-off-by: adamwhats <adamlwatts@msn.com>
Signed-off-by: adamwhats <adamlwatts@msn.com>
@adamwhats
Copy link
Copy Markdown
Contributor Author

Edited to add commit signoffs

Copy link
Copy Markdown
Collaborator

@mhidalgo-rai mhidalgo-rai left a comment

Choose a reason for hiding this comment

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

@adamwhats great catch, and thanks for the contribution 😃

Would you mind adding a test for Python conversion APIs too?

Comment thread proto2ros/proto2ros/equivalences.py
@mhidalgo-rai mhidalgo-rai changed the title Optional fields Fix optional fields in proto3 Nov 13, 2025
Signed-off-by: adamwhats <adamlwatts@msn.com>
Comment thread proto2ros/proto2ros/equivalences.py
Comment thread proto2ros/proto2ros/equivalences.py
Comment thread proto2ros/proto2ros/equivalences.py
…s on humble

Signed-off-by: adamwhats <adamlwatts@msn.com>
mhidalgo-rai
mhidalgo-rai previously approved these changes Nov 24, 2025
Comment thread proto2ros_tests/test/test_proto2ros.cpp Outdated
Signed-off-by: adamwhats <adamlwatts@msn.com>
@mhidalgo-rai mhidalgo-rai merged commit 0cc2471 into rai-opensource:main Nov 25, 2025
6 checks passed
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.

2 participants