Skip to content

Commit

Permalink
Restore adding missing prefix in MergeYaml (#4150)
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek authored and app committed Apr 29, 2024
1 parent 640d02c commit 6fa7246
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,9 @@ public Yaml.Mapping.Entry visitMappingEntry(Yaml.Mapping.Entry entry, ExecutionC
Yaml.Block value = (Yaml.Block) new MergeYamlVisitor<>(entry.getValue(), incoming,
Boolean.TRUE.equals(acceptTheirs), objectIdentifyingProperty).visitNonNull(entry.getValue(),
ctx, getCursor());
if (value instanceof Yaml.Scalar && value.getPrefix().isEmpty()) {
value = value.withPrefix(" ");
}
return entry.withValue(value);
}
return super.visitMappingEntry(entry, ctx);
Expand Down
80 changes: 59 additions & 21 deletions rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ void mergeList() {
widget:
list:
- item 1
""",
""",
"""
widget:
list:
Expand Down Expand Up @@ -927,26 +927,64 @@ void addNewEntryToSequence() {
false, "name")),
yaml(
"""
groups:
- name: analysis
jobs:
- analysis
- name: update
jobs:
- update
""",
"""
groups:
- name: analysis
jobs:
- analysis
- name: update
jobs:
- update
- name: newName
jobs:
- newJob
""")
groups:
- name: analysis
jobs:
- analysis
- name: update
jobs:
- update
""",
"""
groups:
- name: analysis
jobs:
- analysis
- name: update
jobs:
- update
- name: newName
jobs:
- newJob
""")
);
}

@Test
// Mimics `org.openrewrite.java.micronaut.UpdateSecurityYamlIfNeeded`
void mergeEmptyStructureFollowedByCopyValue() {
rewriteRun(
spec -> spec.recipes(
new MergeYaml(
"$.spec",
//language=yaml
"""
empty:
initially:
""",
false,
null
),
new CopyValue("$.spec.level1.level2", null, "$.spec.empty.initially", null))
.expectedCyclesThatMakeChanges(2),
yaml(
"""
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
spec:
level1:
level2: true
""",
"""
apiVersion: storage.cnrm.cloud.google.com/v1beta1
kind: StorageBucket
spec:
level1:
level2: true
empty:
initially: true
"""
)
);
}
}

0 comments on commit 6fa7246

Please sign in to comment.