From 7136c7f56c97eb95811e34b57182cb6c989b56f4 Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 28 Apr 2026 15:00:20 -0700 Subject: [PATCH 1/2] RecipeMarketplace.Category.merge: copy unmatched source children --- .../marketplace/RecipeMarketplace.java | 8 +- .../RecipeMarketplaceMergeTest.java | 92 +++++++++++++++++++ 2 files changed, 96 insertions(+), 4 deletions(-) create mode 100644 rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java diff --git a/rewrite-core/src/main/java/org/openrewrite/marketplace/RecipeMarketplace.java b/rewrite-core/src/main/java/org/openrewrite/marketplace/RecipeMarketplace.java index 16966dfb6a6..f63c78dc81e 100644 --- a/rewrite-core/src/main/java/org/openrewrite/marketplace/RecipeMarketplace.java +++ b/rewrite-core/src/main/java/org/openrewrite/marketplace/RecipeMarketplace.java @@ -84,11 +84,11 @@ public void merge(Category category) { break; } } - if (existingSubCategory != null) { - existingSubCategory.merge(subCategory); - } else { - categories.add(subCategory); + if (existingSubCategory == null) { + existingSubCategory = new Category(subCategory.getDisplayName(), subCategory.getDescription()); + categories.add(existingSubCategory); } + existingSubCategory.merge(subCategory); } } diff --git a/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java b/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java new file mode 100644 index 00000000000..fa70b37dc1c --- /dev/null +++ b/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java @@ -0,0 +1,92 @@ +/* + * Copyright 2025 the original author or authors. + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.openrewrite.marketplace; + +import org.intellij.lang.annotations.Language; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +class RecipeMarketplaceMergeTest { + + @Test + void mergeAllocatesFreshCategoriesOnTarget() { + // The receiver must own every Category instance it holds. If `merge` adopts + // the source's child by reference, a subsequent merge that name-matches + // into the receiver recurses into and mutates the original source. + @Language("csv") String csv = """ + name,category1,ecosystem,packageName + org.example.A,Testing,maven,org.example:test + """; + RecipeMarketplace source = new RecipeMarketplaceReader().fromCsv(csv); + RecipeMarketplace target = new RecipeMarketplace(); + + target.getRoot().merge(source.getRoot()); + + RecipeMarketplace.Category sourceTesting = findCategory(source.getRoot(), "Testing"); + RecipeMarketplace.Category targetTesting = findCategory(target.getRoot(), "Testing"); + assertThat(targetTesting).isNotSameAs(sourceTesting); + } + + @Test + void mergeIntoFreshTargetDoesNotMutateSource() { + // Layering universal + org into a fresh result must leave both inputs + // untouched — otherwise long-lived (cached) sources accumulate recipes + // from every previous merge they were a layer of. + @Language("csv") String universalCsv = """ + name,category1,ecosystem,packageName + org.example.universalA,Testing,maven,org.example:test + org.example.universalB,Testing,maven,org.example:test + """; + @Language("csv") String orgCsv = """ + name,category1,ecosystem,packageName + org.example.orgC,Testing,maven,org.example:test + """; + RecipeMarketplace universal = new RecipeMarketplaceReader().fromCsv(universalCsv); + RecipeMarketplace org = new RecipeMarketplaceReader().fromCsv(orgCsv); + + var universalNamesBefore = universal.getAllRecipes().stream() + .map(RecipeListing::getName).sorted().toList(); + var orgNamesBefore = org.getAllRecipes().stream() + .map(RecipeListing::getName).sorted().toList(); + + RecipeMarketplace result = new RecipeMarketplace(); + result.getRoot().merge(universal.getRoot()); + result.getRoot().merge(org.getRoot()); + + assertThat(universal.getAllRecipes().stream().map(RecipeListing::getName).sorted().toList()) + .as("universal must not gain recipes from later merges") + .isEqualTo(universalNamesBefore); + assertThat(org.getAllRecipes().stream().map(RecipeListing::getName).sorted().toList()) + .as("org must not gain recipes from earlier merges") + .isEqualTo(orgNamesBefore); + assertThat(result.getAllRecipes()).extracting(RecipeListing::getName) + .containsExactlyInAnyOrder( + "org.example.universalA", + "org.example.universalB", + "org.example.orgC"); + } + + private static RecipeMarketplace.Category findCategory( + RecipeMarketplace.Category parent, String displayName) { + for (RecipeMarketplace.Category c : parent.getCategories()) { + if (c.getDisplayName().equalsIgnoreCase(displayName)) { + return c; + } + } + throw new AssertionError("Category not found: " + displayName); + } +} From 10275bc6d55e5ae82728007523a358324fbb760b Mon Sep 17 00:00:00 2001 From: Nate Date: Tue, 28 Apr 2026 15:03:27 -0700 Subject: [PATCH 2/2] Bump copyright year on new test file --- .../org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java b/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java index fa70b37dc1c..4f6656cca53 100644 --- a/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java +++ b/rewrite-core/src/test/java/org/openrewrite/marketplace/RecipeMarketplaceMergeTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2025 the original author or authors. + * Copyright 2026 the original author or authors. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License.