Skip to content

Commit

Permalink
MergeYaml should avoid adding query with no result as key at end of f…
Browse files Browse the repository at this point in the history
…ile (#4297)

* avoid adding non-existent query as key

in case the jsonpath is a query which yelds no result,
avoid merging the yaml at end of file with the query as a key

* Fix StackOverflowError in case of circular import deps (#4284)

* Fix StackOverflowError in case of circular import deps

Check if import dependency was already resolved before

* Format test, remove unnecessary paths and add issue link

* Replace Streams with for loop as per convention

To limit object allocations

* Swap argument order for consistency

* Verify merged managed dependency version

---------

Co-authored-by: Daniil Zhyliaiev <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>

* Give EffectiveManagedDependencies a unique display name

* Fix model updating to properly work with transitive dependencies.

* Fix test expectation to not break when new versions of flyway-core are released

* Deterministic ordering of new constraints

* Further enhancements to UpdateGradleWrapper's ability to operate in contexts where services.gradle.org is unavailable

* Also enforce that version numbers are literal, rather than dynamic selectors, when services.gradle.org cannot be reached

* Fix typo in comment and apply formatter

---------

Co-authored-by: Stef <[email protected]>
Co-authored-by: Daniil Zhyliaiev <[email protected]>
Co-authored-by: Daniil Zhyliaiev <[email protected]>
Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: Sam Snyder <[email protected]>
  • Loading branch information
6 people authored Jul 2, 2024
1 parent 29182e4 commit 897f701
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 5 deletions.
14 changes: 11 additions & 3 deletions rewrite-yaml/src/main/java/org/openrewrite/yaml/MergeYaml.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,10 @@ public Yaml.Document visitDocument(Yaml.Document document, ExecutionContext ctx)
ctx, getCursor()));
}
Yaml.Document d = super.visitDocument(document, ctx);
if(d == document && !getCursor().getMessage(FOUND_MATCHING_ELEMENT, false)) {
if (d == document && !getCursor().getMessage(FOUND_MATCHING_ELEMENT, false)) {
// No matching element already exists, attempt to construct one
String valueKey = maybeKeyFromJsonPath(key);
if(valueKey == null) {
if (valueKey == null) {
return d;
}
// If there is no space between the colon and the value it will not be interpreted as a mapping
Expand Down Expand Up @@ -137,7 +137,15 @@ public String indent(String text) {

@Nullable
private String maybeKeyFromJsonPath(String jsonPath) {
if(!jsonPath.startsWith("$.")) {
if (!jsonPath.startsWith("$.")) {
return null;
}
// if the key contains a jsonpath filter we cannot infer a valid key
if (jsonPath.matches(".*\\[\\s?\\?\\s?\\(\\s?@\\..*\\)\\s?].*")) {
return null;
}
// remove keys that contain wildcard or deep search
if (jsonPath.matches(".*\\*.*") || jsonPath.matches(".*\\.\\..*")) {
return null;
}
return jsonPath.substring(2);
Expand Down
118 changes: 116 additions & 2 deletions rewrite-yaml/src/test/java/org/openrewrite/yaml/MergeYamlTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.openrewrite.yaml;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.Issue;
Expand Down Expand Up @@ -326,7 +325,100 @@ void insertInSequenceEntries() {
}

@Test
@Disabled
void insertInSequenceEntriesWithWildcard() {
rewriteRun(
spec -> spec.recipe(new MergeYaml(
"$.*.containers",
"imagePullPolicy: Always",
true,
null
)),
yaml(
"""
kind: Pod
spec:
containers:
- name: <container name>
""",
"""
kind: Pod
spec:
containers:
- name: <container name>
imagePullPolicy: Always
"""
)
);
}

@Test
void noInsertInSequenceEntriesWithWildcard() {
rewriteRun(
spec -> spec.recipe(new MergeYaml(
"$.*.unknown",
"imagePullPolicy: Always",
true,
null
)),
yaml(
"""
kind: Pod
spec:
containers:
- name: <container name>
"""
)
);
}

@Test
void insertInSequenceEntriesWithDeepSearch() {
rewriteRun(
spec -> spec.recipe(new MergeYaml(
"$..containers",
"imagePullPolicy: Always",
true,
null
)),
yaml(
"""
kind: Pod
spec:
containers:
- name: <container name>
""",
"""
kind: Pod
spec:
containers:
- name: <container name>
imagePullPolicy: Always
"""
)
);
}

@Test
void noInsertInSequenceEntriesWithDeepSearch() {
rewriteRun(
spec -> spec.recipe(new MergeYaml(
"$..unknown",
"imagePullPolicy: Always",
true,
null
)),
yaml(
"""
kind: Pod
spec:
containers:
- name: <container name>
"""
)
);
}

@Test
void insertInSequenceEntriesMatchingPredicate() {
rewriteRun(
spec -> spec.recipe(new MergeYaml(
Expand Down Expand Up @@ -356,6 +448,28 @@ void insertInSequenceEntriesMatchingPredicate() {
);
}

@Test
void noChangeInSequenceEntriesNotMatchingPredicate() {
rewriteRun(
spec -> spec.recipe(new MergeYaml(
"$.spec.containers[?(@.name == 'pod-x')]",
//language=yaml
"imagePullPolicy: Always",
true,
null
)),
yaml(
"""
kind: Pod
spec:
containers:
- name: pod-0
- name: pod-1
"""
)
);
}

@Test
void insertBlockInSequenceEntriesWithExistingBlock() {
rewriteRun(
Expand Down

0 comments on commit 897f701

Please sign in to comment.