Skip to content

Pass literal values to fix ReplaceStringLiteralWithConstant validation#947

Merged
timtebeek merged 2 commits intomainfrom
fix-string-literal-constant-validation
Feb 26, 2026
Merged

Pass literal values to fix ReplaceStringLiteralWithConstant validation#947
timtebeek merged 2 commits intomainfrom
fix-string-literal-constant-validation

Conversation

@timtebeek
Copy link
Member

What's Changed

ReplaceStringLiteralsWithMediaTypeConstants and ReplaceStringLiteralsWithHttpHeadersConstants were passing null for literalValue when constructing ReplaceStringLiteralWithConstant instances. This caused validate() to attempt reflective class loading via Class.forName("org.springframework.http.MediaType") / Class.forName("org.springframework.http.HttpHeaders"). Since spring-web is not a dependency of rewrite-spring, this always fails with ClassNotFoundException, producing validation errors like:

fullyQualifiedConstantName was 'org.springframework.http.MediaType.TEXT_HTML_VALUE'
but it No class for specified name was found.

This affected all constants in both recipes, not just the MediaType ones.

Fix

Provide the actual string literal values directly (e.g., "text/html" for TEXT_HTML_VALUE, "Accept" for ACCEPT), so validation succeeds without needing reflection. Values were verified against Spring Framework 6.2.

Also adds the missing APPLICATION_YAML_VALUE ("application/yaml") constant that exists in Spring Framework 6.x.

Test plan

  • Existing ReplaceLiteralsTest tests pass
  • All org.openrewrite.java.spring.http.* tests pass
  • Compilation succeeds cleanly

…tion

ReplaceStringLiteralsWithMediaTypeConstants and
ReplaceStringLiteralsWithHttpHeadersConstants were passing null for
literalValue, causing ReplaceStringLiteralWithConstant.validate() to
attempt reflective class loading of org.springframework.http.MediaType
and org.springframework.http.HttpHeaders. Since spring-web is not a
dependency of rewrite-spring, Class.forName() always fails with
ClassNotFoundException, producing validation errors like:

  fullyQualifiedConstantName was
  'org.springframework.http.MediaType.TEXT_HTML_VALUE' but it
  No class for specified name was found.

Fix by providing the actual string literal values directly, so
validation no longer needs reflection. Also adds the missing
APPLICATION_YAML_VALUE constant.
@timtebeek timtebeek merged commit da5c278 into main Feb 26, 2026
1 check passed
@timtebeek timtebeek deleted the fix-string-literal-constant-validation branch February 26, 2026 10:13
@github-project-automation github-project-automation bot moved this from In Progress to Done in OpenRewrite Feb 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

1 participant