Skip to content

AddOrUpdateAnnotationAttribute respects appendArray with string literals#6598

Merged
jkschneider merged 2 commits intomainfrom
steve/1410-annotation-append-array
Jan 26, 2026
Merged

AddOrUpdateAnnotationAttribute respects appendArray with string literals#6598
jkschneider merged 2 commits intomainfrom
steve/1410-annotation-append-array

Conversation

@steve-aom-elliott
Copy link
Copy Markdown
Contributor

@steve-aom-elliott steve-aom-elliott commented Jan 26, 2026

Summary

  • When appendArray=true and the existing annotation value is a string literal (not already an array), the recipe now converts it to an array and appends the new values
  • Added helper method createNewArrayWithExistingAndNew() to handle the conversion
  • Added test for implicit value attribute case

Problem

AddOrUpdateAnnotationAttribute did not respect the appendArray option when the existing annotation value was a string literal rather than an array. It would replace the value instead of appending.

Example:

  • Input: @Foo(bars = "abc") with appendArray=true and attributeValue="xyz"
  • Expected: @Foo(bars = {"abc", "xyz"})
  • Actual: @Foo(bars = "xyz")

Solution

Added checks in update(J.Assignment ...) and update(J.Literal ...) methods to detect when appendArray=true and the attribute is an array type but the current value is a literal. In these cases, we now convert the literal to an array containing both the existing value and the new values.

Test plan

  • Existing tests pass
  • New test respectsAppendArrayWhenGoingFromImplicitLiteralValueToArray() added
  • Existing tests respectsAppendArrayWhenGoingFromLiteralToArraySingle and respectsAppendArrayWhenGoingFromLiteralToArrayMulti now pass

When appendArray=true and the existing annotation value is a string
literal (not already an array), the recipe now converts it to an array
and appends the new values instead of replacing.

Before: @foo(bars = "abc") + appendArray=true + value="xyz" → @foo(bars = "xyz")
After:  @foo(bars = "abc") + appendArray=true + value="xyz" → @foo(bars = {"abc", "xyz"})

Fixes moderneinc/customer-requests#1410
@github-project-automation github-project-automation Bot moved this to In Progress in OpenRewrite Jan 26, 2026
@steve-aom-elliott steve-aom-elliott moved this from In Progress to Ready to Review in OpenRewrite Jan 26, 2026
@steve-aom-elliott steve-aom-elliott added bug Something isn't working test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail labels Jan 26, 2026
@jkschneider jkschneider merged commit b155248 into main Jan 26, 2026
2 checks passed
@jkschneider jkschneider deleted the steve/1410-annotation-append-array branch January 26, 2026 20:20
@github-project-automation github-project-automation Bot moved this from Ready to Review to Done in OpenRewrite Jan 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working test provided Already replicated with a unit test, using JUnit pioneer's ExpectedToFail

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

2 participants