Skip to content

MergeYaml creates duplicate properties in yaml with several documents (separated by "---") #3539

@luisrov

Description

@luisrov

I am using

  • OpenRewrite v2.0.1
  • Maven/Gradle plugin v5.2.2
  • rewrite-module v8.1.2

I`m using the Maven plugin on my multi-module project.

<plugin>
	<groupId>org.openrewrite.maven</groupId>
	<artifactId>rewrite-maven-plugin</artifactId>
	<version>5.2.2</version>
	<configuration>
		<activeRecipes>
			<recipe>myrecipe</recipe><!-- my recipe invoke MergeYaml -->
		</activeRecipes>
	</configuration>
</plugin>

The org.openrewrite.yaml.MergeYaml recipe creates duplicate properties in yaml files containing multiple documents separated with "---".

Below a unit tests to test the bug:

package com.mycompany.app.yaml

import com.mycompany.app.utils.SourcesTest
import org.junit.jupiter.api.Test
import org.openrewrite.Recipe
import org.openrewrite.yaml.MergeYaml
import org.openrewrite.yaml.YamlParser
import org.openrewrite.yaml.tree.Yaml

class BugMergeYamlTest : SourcesTest<Yaml.Documents> {

    override val parser: YamlParser
        get() = YamlParser()

    override val recipe: Recipe
        get() = MergeYaml("$", """
            app:
              core:
                key2: value02
        """.trimIndent(), false, null)

    @Test
    fun replacePropTest() = assertChanged(
            before = """
            app:
              app.key: app
            ---
            com:
              key1: value1
              key2: value2
              key3: value3
            app:
              core:
                key1: value01
        """.trimIndent(),
            after = """
            app:
              app.key: app
            ---
            com:
              key1: value1
              key2: value2
              key3: value3
            app:
              core:
                key1: value01
                key2: value02
        """
    )
}

What did you expect to see?

app:
  app.key: app
---
com:
  key1: value1
  key2: value2
  key3: value3
app:
  core:
    key1: value01
    key2: value02

What did you see instead?

app:
  app.key: app
  core:
    key2: value02
---
com:
  key1: value1
  key2: value2
  key3: value3
app:
  core:
    key1: value01
    key2: value02

Shouldn't the recipe have an attribute to indicate which document should be modified? Previously it had the fileMatcher attribute, but in this case it would not work either, since both documents are in the same file.

Thank you and greetings.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions