Skip to content

Add XML schema validation for ros2_control tag and test assets #2466

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

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

sachinkum0009
Copy link
Contributor

@sachinkum0009 sachinkum0009 commented Aug 16, 2025

Pull Request: Add XML schema validation for ros2_control tag and test assets

Description

This pull request introduces XML schema validation for the <ros2_control> tag in URDF files, addressing Issue 2173. The changes include:

  • Added an XML Schema Definition (ros2_control.xsd) to formalize and validate the structure of the <ros2_control> tag.
  • Implemented a GTest-based unit test (validate_xml_schema.cpp) using libxml2 to validate URDF files against the schema.
  • Updated CMakeLists.txt to install the schema and URDF directories, and to build and link the new test.
  • Added a new invalid URDF test file to verify schema validation failure cases.
  • Enhanced the valid test URDF to include more schema features (e.g., params, gpio).
  • Updated package dependencies to include ament_index_cpp for test support.

Motivation

The format of the <ros2_control> tag was previously unclear and often copied from demos or parser code. This PR provides a clear, machine-validated format, making it easier to create and debug hardware interface descriptions.

Checklist

  • Limited scope: This PR only adds schema validation and related tests.
  • Descriptive title and summary provided.
  • New code includes new tests.
  • Local tests pass (colcon test and pre-commit run).
  • Commit messages are clear.

How to Test

Run the following commands to validate the schema and run the tests:

xmllint --noout --schema src/ros2_control/hardware_interface/schema/ros2_control.xsd src/ros2_control/hardware_interface/urdf/test_hardware_components.urdf

colcon test --packages-select hardware_interface --ctest-args -R test_validate_xml_schema --event-handlers console_direct+

Additional Context

See the attached issue and the updated test assets for more details. Please review and let me know if further changes are needed.

Copy link
Member

@saikishor saikishor left a comment

Choose a reason for hiding this comment

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

Thank you so much for such a nice addition👏🏽👏🏽

Isn't the hardware_interface the best place to have the XML schema? As that is where the component parsing and validation happen.

Moreover, the current place is only to store the assets for tests.

What do you think? @bmagyar @christophfroehlich @destogl

@christophfroehlich
Copy link
Contributor

I agree, the tests assets are not the best place.

@sachinkum0009 sachinkum0009 marked this pull request as draft August 16, 2025 17:55
Copy link
Contributor

@christophfroehlich christophfroehlich left a comment

Choose a reason for hiding this comment

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

+1 what @saikishor already said.

how does xsd work actually? is there a way to have different versions? or is this specified only by the url where we put it? I'm thinking of how we handle this in future, when the schema evolves with a new ROS distro for example.

@sachinkum0009
Copy link
Contributor Author

Isn't the hardware_interface the best place to have the XML schema? As that is where the component parsing and validation happen.

Thanks, I will move these XML Schema to the hardware_interface pkg.

how does xsd work actually? is there a way to have different versions? or is this specified only by the url where we put it?

I'm thinking of how we handle this in future, when the schema evolves with a new ROS distro for example.
Yes, we can have different XSD files. Currently, the unit test loads the file from the schema folder and then run the tests. Please let me know if I should follow different approach.

@christophfroehlich
Copy link
Contributor

Yes, we can have different XSD files. Currently, the unit test loads the file from the schema folder and then run the tests. Please let me know if I should follow different approach.

Repository-wise this is fine. But hopefully people will adopt this and have the schema in their xml files. How do they reference to the right version? how does the parser of a specific ros2_control version handle xml files with different xsd specifications? Will we just have a specific xsd file pero ros2_control release hosted on control.ros.org (versioning done by filename), and the parser throws an error if the xml has the wrong version?

This might not be directly related to this specific PR, I'm just wondering how this will work in future.

@sachinkum0009
Copy link
Contributor Author

Repository-wise this is fine. But hopefully people will adopt this and have the schema in their xml files. How do they reference to the right version? how does the parser of a specific ros2_control version handle xml files with different xsd specifications? Will we just have a specific xsd file pero ros2_control release hosted on control.ros.org (versioning done by filename), and the parser throws an error if the xml has the wrong version?

I think its good idea to have specific xsd file per ros2_control hosted on control.ros.org and then parser throws an error if xml has wrong version.

@sachinkum0009
Copy link
Contributor Author

I also wanted to ask where should I add the URDF file to validate the xml schema, for example, should I use the same URDF folder from ros2_control_test_assets, and also which ros2 control pkg is good to add xsd files?
Thanks for your help and guidance.

- urdf files added for validation and unvalidation

- schema xsd file added
@sachinkum0009 sachinkum0009 reopened this Aug 17, 2025
Copy link

codecov bot commented Aug 17, 2025

Codecov Report

❌ Patch coverage is 70.27027% with 11 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.15%. Comparing base (9281169) to head (412f6cc).
⚠️ Report is 2 commits behind head on master.

Files with missing lines Patch % Lines
...rdware_interface/test/test_validate_xml_schema.cpp 70.27% 4 Missing and 7 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2466      +/-   ##
==========================================
- Coverage   89.21%   89.15%   -0.07%     
==========================================
  Files         144      145       +1     
  Lines       16334    16372      +38     
  Branches     1393     1400       +7     
==========================================
+ Hits        14573    14597      +24     
- Misses       1225     1231       +6     
- Partials      536      544       +8     
Flag Coverage Δ
unittests 89.15% <70.27%> (-0.07%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
...rdware_interface/test/test_validate_xml_schema.cpp 70.27% <70.27%> (ø)

... and 1 file with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@christophfroehlich
Copy link
Contributor

christophfroehlich commented Aug 17, 2025

I also wanted to ask where should I add the URDF file to validate the xml schema, for example, should I use the same URDF folder from ros2_control_test_assets, and also which ros2 control pkg is good to add xsd files? Thanks for your help and guidance.

We have all the assets in ros2_control_test_assets, let's put the URDF in there. the xsd is good in the hardware_interface: In this PR, or in a subsequent, we will replace the parse_interfaces_from_xml from component_parser to use the xsd?

Once this is merged, I'll have a look how to publish the xsd for the different versions on control.ros.org, so that we can update all URDFs in our repositories.

sachinkum0009 and others added 4 commits August 18, 2025 11:56
reformatting and sort ascending

Co-authored-by: Christoph Fröhlich <christophfroehlich@users.noreply.github.com>
package path updated to find urdf and xsd file
@sachinkum0009
Copy link
Contributor Author

Thanks for your suggestions. Updated the code. Please LMK, if there are some other suggestions. :)

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.

3 participants