rewrite icon indicating copy to clipboard operation
rewrite copied to clipboard

ChangePropertyKey does not construct nested YAML structure, inserts flat keys instead

Open vudayani opened this issue 7 months ago • 2 comments

What version of OpenRewrite are you using?

I am using rewrite - v8.47.4

What is the smallest, simplest way to reproduce the problem?

    @Test
    void test() {
        rewriteRun(
                spec -> spec.recipe(new ChangePropertyKey(
                        "management.server.accesslog.prefix",
                        "management.server.jetty.accesslog.prefix",
                        true,
                        null,
                        null
                )),
                yaml(
                        """
                        management:
                            server:
                                accesslog:
                                    prefix: logs
                        """,
                        """
                        management:
                            server:
                                jetty:
                                    accesslog:
                                        prefix: logs
                        """,
                        spec -> spec.path("src/main/resources/application.yml")
                )
        );
    }

We encountered an issue when using org.openrewrite.yaml.ChangePropertyKey to move keys in a .yaml file. The expected behavior is for the recipe to move a YAML block and construct a proper nested structure. However, it results in a flat key instead of creating a properly nested YAML hierarchy.

Input YAML:

management:
  server:
    accesslog:
      prefix: logs

What did you expect to see?

management:
  server:
    jetty:
      accesslog:
        prefix: logs

What did you see instead?

management:
    server:
            jetty.accesslog.prefix: logs

Analysis:

Looking closely at the implementation of org.openrewrite.yaml.ChangePropertyKey, the issue appears to be with the behavior of the InsertSubpropertyVisitor. The subproperty is being treated as a literal scalar key — the InsertSubpropertyVisitor does not split the subproperty key (e.g., jetty.accesslog.prefix) into nested keys based on the dots (.). This results in a flat key rather than a properly nested structure.

Are you interested in contributing a fix to OpenRewrite?

Yes, I would be happy to give it a try with some guidance or feedback from the maintainers.

vudayani avatar May 07 '25 03:05 vudayani

Hi @vudayani ; good to see you here again. Do I understand correct that you landed here from following how rewrite-spring's ChangeSpringPropertyKey uses the ChangePropertyKey defined here? Since your title mentions the first, but the test uses the second, and one uses the other to complicate matters further. 😅 Either way @sambsnyd is our resident expert on Yaml wrangling; let's see if he can chime in here. We're also available to help in our OSS Slack if that helps you there.

timtebeek avatar May 07 '25 10:05 timtebeek

Thanks, and sorry for the confusion! :-) Yes, you're right — I originally encountered the issue while working with ChangeSpringPropertyKey, which internally delegates to ChangePropertyKey. That’s how I ended up tracing the behavior here. I will update the title to clarify it.

vudayani avatar May 07 '25 11:05 vudayani

This issue is stale because it has not had any activity for 60 days. Remove question label or comment or this will be closed in two weeks. Issues may be reopened when there is renewed interest.

github-actions[bot] avatar Jul 28 '25 11:07 github-actions[bot]