Skip to content
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

ChangeType leads to botched imports for nested fields #4292

Merged
merged 7 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1850,11 +1850,13 @@ public Test test() {

@Issue("https://github.com/openrewrite/rewrite-jackson/pull/6")
@Test
void changeTypeOfStaticImportOfNestedEnumValue() {
void changeTypeOfStaticImportOfNestedEnumValueUnused() {
rewriteRun(
recipeSpec -> recipeSpec.recipe(new ChangeType(
"org.codehaus.jackson.map.SerializationConfig$Feature",
"com.fasterxml.jackson.databind.SerializationFeature", true)),
recipeSpec -> recipeSpec.recipes(
new ChangeType(
"org.codehaus.jackson.map.SerializationConfig$Feature",
"com.fasterxml.jackson.databind.SerializationFeature", true)
),
java(
"""
package org.codehaus.jackson.map;
Expand All @@ -1868,37 +1870,77 @@ public static enum Feature {
),
java(
"""
package com.fasterxml.jackson.databind;
public enum SerializationFeature {
WRAP_ROOT_VALUE
import static org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE;

class A {
}
""",
"""
import static com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE;

class A {
}
"""
)
);
}

@Issue("https://github.com/openrewrite/rewrite-jackson/pull/6")
@Test
void changeTypeOfStaticImportOfNestedEnumValueUsed() {
rewriteRun(
recipeSpec -> recipeSpec.recipes(
new ChangeType(
"org.codehaus.jackson.map.ObjectMapper",
"com.fasterxml.jackson.databind.ObjectMapper", true),
new ChangeType(
"org.codehaus.jackson.map.SerializationConfig$Feature",
"com.fasterxml.jackson.databind.SerializationFeature", true)
),
java(
"""
package org.codehaus.jackson.map;
public class SerializationConfig {
public static enum Feature {
WRAP_ROOT_VALUE
}
}
""",
SourceSpec::skip
),
java(
"""
package org.codehaus.jackson.map;
public class ObjectMapper {
public ObjectMapper configure(SerializationConfig.Feature f, boolean state) {
return this;
}
}
""",
SourceSpec::skip
),
java(
"""
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;

import static org.codehaus.jackson.map.SerializationConfig.Feature.WRAP_ROOT_VALUE;

class A {
void test() {
configure(WRAP_ROOT_VALUE, true);
}
void configure(Feature f, boolean b) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(WRAP_ROOT_VALUE, true);
}
}
""",
"""
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

import static com.fasterxml.jackson.databind.SerializationFeature.WRAP_ROOT_VALUE;

class A {
void test() {
configure(WRAP_ROOT_VALUE, true);
}
void configure(SerializationFeature f, boolean b) {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(WRAP_ROOT_VALUE, true);
}
}
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ private void addImport(JavaType.FullyQualified owningClass) {
}

if (sf != null) {
sf = sf.withImports(ListUtils.map(sf.getImports(), i -> visitAndCast(i, ctx, super::visitImport)));
sf = sf.withImports(ListUtils.map(sf.getImports(), originalImport -> {
J.Import modifiedImport = visitAndCast(originalImport, ctx, super::visitImport);
return modifiedImport; // FIXME Mangles import
}));
timtebeek marked this conversation as resolved.
Show resolved Hide resolved
}

j = sf;
Expand Down
Loading