Skip to content

Fix AddCommentToXmlTag for self-closing and empty tags#7779

Merged
timtebeek merged 4 commits into
mainfrom
Jenson3210/issue-7777
May 26, 2026
Merged

Fix AddCommentToXmlTag for self-closing and empty tags#7779
timtebeek merged 4 commits into
mainfrom
Jenson3210/issue-7777

Conversation

@Jenson3210
Copy link
Copy Markdown
Contributor

Summary

  • Resolves AddCommentToXmlTag doesn't handle self-closing or empty tags #7777: AddCommentToXmlTag silently skipped self-closing tags (<foo/>) and threw IndexOutOfBoundsException on empty tags (<foo></foo>).
  • Visitor now derives the comment prefix from the existing first child (or "" when content is null/empty) and always routes through Xml.Tag#withContent, which already materializes a Closing tag when transitioning a self-closing tag to one with content.
  • Also fixes a latent bug where the previous implementation mixed tag.getContent() (pre-recursion) with t.withContent(...), dropping nested transformations.

Test plan

  • New test: comment is added to a self-closing tag (<foo bar="baz"/><foo bar="baz"><!-- hello --></foo>).
  • New test: comment is added to an empty tag (<foo></foo><foo><!-- hello --></foo>) with no exception.
  • New test: idempotency — recipe does not duplicate an existing matching comment.
  • Existing addCommentToDependencyBlock test still passes.
  • ./gradlew :rewrite-xml:test --tests "org.openrewrite.xml.AddCommentToXmlTagTest" → BUILD SUCCESSFUL.

The recipe previously silently skipped self-closing tags and threw
IndexOutOfBoundsException on empty tags. Route through withContent
consistently so the Closing tag is materialized when needed.
@github-project-automation github-project-automation Bot moved this from In Progress to Ready to Review in OpenRewrite May 26, 2026
Comment on lines +90 to +91
"<root><foo bar=\"baz\"/></root>",
"<root><foo bar=\"baz\"><!-- hello --></foo></root>"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I wonder if we should prepend the comment to the self closing tag instead of adding a new close tag to add the comment within the tag. What are your thoughts on that?

My motivation would be a more minimal diff, with a clear line introduced before the existing line in a well formatted XML document.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I also preferred that, however, that is a bigger change as there is already expectations of comments being added inside the tags.
Would you want to have different behavior when tag is empty vs non-empty?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I'd be fine with slightly different behavior if that leads to less surprises for the self closing case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

implemented this change here.

Previously the recipe always inserted the comment inside the matched
tag, which forced self-closing tags like <foo bar="baz"/> to be opened
up into <foo bar="baz"><!-- ... --></foo>. Reviewer feedback prefers
keeping the tag self-closing and emitting the comment on the line just
before it, which yields a smaller, more idiomatic diff.

Empty tags (<foo></foo>) and tags with content keep the existing
in-tag behavior.
Comment thread rewrite-xml/src/main/java/org/openrewrite/xml/AddCommentToXmlTag.java Outdated
@timtebeek timtebeek merged commit 7875ad3 into main May 26, 2026
@timtebeek timtebeek deleted the Jenson3210/issue-7777 branch May 26, 2026 11:13
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

AddCommentToXmlTag doesn't handle self-closing or empty tags

3 participants