Skip to content

Commit 2dd71b1

Browse files
authored
RecipeListing withers now preserve metadata map (#6849)
The Lombok-generated @with methods created new instances via the all-args constructor, which left the metadata map empty. Replace them with hand-written withMarketplace/withBundle that copy metadata into the new instance.
1 parent 6533e0a commit 2dd71b1

2 files changed

Lines changed: 54 additions & 2 deletions

File tree

rewrite-core/src/main/java/org/openrewrite/marketplace/RecipeListing.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ public class RecipeListing implements Comparable<RecipeListing> {
3333
/**
3434
* The marketplace that this listing belongs to.
3535
*/
36-
@With
3736
private final @Nullable RecipeMarketplace marketplace;
3837

3938
private final @EqualsAndHashCode.Include String name;
@@ -61,9 +60,28 @@ public class RecipeListing implements Comparable<RecipeListing> {
6160

6261
private final Map<String, Object> metadata = new LinkedHashMap<>();
6362

64-
@With(AccessLevel.PACKAGE)
6563
private final RecipeBundle bundle;
6664

65+
public RecipeListing withMarketplace(@Nullable RecipeMarketplace marketplace) {
66+
if (this.marketplace == marketplace) {
67+
return this;
68+
}
69+
RecipeListing copy = new RecipeListing(marketplace, name, displayName, description,
70+
estimatedEffortPerOccurrence, options, dataTables, recipeCount, bundle);
71+
copy.metadata.putAll(this.metadata);
72+
return copy;
73+
}
74+
75+
RecipeListing withBundle(RecipeBundle bundle) {
76+
if (this.bundle == bundle) {
77+
return this;
78+
}
79+
RecipeListing copy = new RecipeListing(marketplace, name, displayName, description,
80+
estimatedEffortPerOccurrence, options, dataTables, recipeCount, bundle);
81+
copy.metadata.putAll(this.metadata);
82+
return copy;
83+
}
84+
6785
private RecipeBundleReader resolve(Collection<RecipeBundleResolver> resolvers) {
6886
for (RecipeBundleResolver resolver : resolvers) {
6987
if (resolver.getEcosystem().equals(bundle.getPackageEcosystem())) {

rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceReaderTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,40 @@ private void setVersionRecursive(RecipeMarketplace.Category category, String pac
402402
}
403403
}
404404

405+
@Test
406+
void mergePreservesMetadata() {
407+
// Source marketplace has a recipe with metadata in "Java" category
408+
RecipeMarketplace source = new RecipeMarketplaceReader().fromCsv("""
409+
name,displayName,description,category,ecosystem,packageName,embedding
410+
org.example.TestRecipe,Test Recipe,A test recipe,Java,maven,org.example:test,abc123
411+
""");
412+
413+
// Verify metadata was read
414+
RecipeListing sourceRecipe = source.getAllRecipes().iterator().next();
415+
assertThat(sourceRecipe.getMetadata().get("embedding")).isEqualTo("abc123");
416+
417+
// Target marketplace also has a recipe in "Java" (overlapping category)
418+
RecipeMarketplace target = new RecipeMarketplaceReader().fromCsv("""
419+
name,displayName,description,category,ecosystem,packageName,embedding
420+
org.example.OtherRecipe,Other Recipe,Another recipe,Java,maven,org.example:other,xyz789
421+
""");
422+
423+
// Merge source into target — both have "Java" category so this exercises
424+
// the recursive merge path where withMarketplace() is called
425+
target.getRoot().merge(source.getRoot());
426+
427+
// Verify both recipes exist and metadata is preserved
428+
RecipeListing mergedTest = target.findRecipe("org.example.TestRecipe");
429+
assertThat(mergedTest).isNotNull();
430+
assertThat(mergedTest.getMetadata().get("embedding"))
431+
.as("Metadata should be preserved through merge when categories overlap")
432+
.isEqualTo("abc123");
433+
434+
RecipeListing mergedOther = target.findRecipe("org.example.OtherRecipe");
435+
assertThat(mergedOther).isNotNull();
436+
assertThat(mergedOther.getMetadata().get("embedding")).isEqualTo("xyz789");
437+
}
438+
405439
private static RecipeMarketplace.Category findCategory(RecipeMarketplace.Category category, String name) {
406440
return category.getCategories().stream()
407441
.filter(c -> c.getDisplayName().equals(name))

0 commit comments

Comments
 (0)