-
-
Notifications
You must be signed in to change notification settings - Fork 0
fix: coerce empty maps to objects in patch payloads #31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,176 @@ | ||
| <?php | ||
|
|
||
| namespace RenokiCo\PhpK8s\Test; | ||
|
|
||
| use RenokiCo\PhpK8s\Patches\JsonMergePatch; | ||
| use RenokiCo\PhpK8s\Patches\JsonPatch; | ||
|
|
||
| class PatchPayloadTest extends TestCase | ||
| { | ||
| public function test_json_patch_array_values_coerce_nested_empty_maps() | ||
| { | ||
| $payload = $this->cluster->statefulSet()->toJsonPatchPayload([ | ||
| [ | ||
| 'op' => 'replace', | ||
| 'path' => '/spec/template/spec/volumes', | ||
| 'value' => [ | ||
| ['name' => 'wal', 'emptyDir' => []], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString('"emptyDir":{}', $payload); | ||
| $this->assertStringNotContainsString('"emptyDir":[]', $payload); | ||
| } | ||
|
|
||
| public function test_json_patch_object_values_coerce_nested_empty_maps() | ||
| { | ||
| $patch = (new JsonPatch)->replace('/spec/template/spec/volumes', [ | ||
| ['name' => 'wal', 'emptyDir' => []], | ||
| ]); | ||
|
|
||
| $payload = $this->cluster->statefulSet()->toJsonPatchPayload($patch); | ||
|
|
||
| $this->assertStringContainsString('"emptyDir":{}', $payload); | ||
| $this->assertStringNotContainsString('"emptyDir":[]', $payload); | ||
| } | ||
|
|
||
| public function test_json_patch_top_level_empty_list_value_stays_list() | ||
| { | ||
| $payload = $this->cluster->configmap()->toJsonPatchPayload([ | ||
| ['op' => 'replace', 'path' => '/metadata/finalizers', 'value' => []], | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString('"value":[]', $payload); | ||
| } | ||
|
|
||
| public function test_json_patch_values_respect_exempted_list_fields() | ||
| { | ||
| $payload = $this->cluster->persistentVolume()->toJsonPatchPayload([ | ||
| [ | ||
| 'op' => 'replace', | ||
| 'path' => '/spec', | ||
| 'value' => [ | ||
| 'accessModes' => [], | ||
| 'mountOptions' => [], | ||
| 'nodeAffinity' => [], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString('"accessModes":[]', $payload); | ||
| $this->assertStringContainsString('"mountOptions":[]', $payload); | ||
| $this->assertStringContainsString('"nodeAffinity":{}', $payload); | ||
| } | ||
|
|
||
| public function test_json_patch_nested_finalizers_stay_lists_while_maps_coerce() | ||
| { | ||
| $payload = $this->cluster->configmap()->toJsonPatchPayload([ | ||
| [ | ||
| 'op' => 'replace', | ||
| 'path' => '/metadata', | ||
| 'value' => [ | ||
| 'finalizers' => [], | ||
| 'labels' => [], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString('"finalizers":[]', $payload); | ||
| $this->assertStringContainsString('"labels":{}', $payload); | ||
| } | ||
|
|
||
| public function test_json_patch_string_values_containing_empty_array_literal_are_preserved() | ||
| { | ||
| $script = 'foreach (glob("/x/*") ?: [] as $file) { echo $file; }'; | ||
|
|
||
| $payload = $this->cluster->pod()->toJsonPatchPayload([ | ||
| [ | ||
| 'op' => 'replace', | ||
| 'path' => '/spec/containers/0/command', | ||
| 'value' => ['php', '-r', $script], | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertStringNotContainsString('?: {}', $payload); | ||
|
|
||
| $decoded = json_decode($payload, true); | ||
|
|
||
| $this->assertSame($script, $decoded[0]['value'][2]); | ||
| } | ||
|
|
||
| public function test_json_patch_scalar_and_valueless_operations_are_untouched() | ||
| { | ||
| $payload = $this->cluster->deployment()->toJsonPatchPayload([ | ||
| ['op' => 'replace', 'path' => '/spec/replicas', 'value' => 3], | ||
| ['op' => 'remove', 'path' => '/metadata/labels/deprecated'], | ||
| ['op' => 'add', 'path' => '/metadata/labels/app', 'value' => null], | ||
| ]); | ||
|
|
||
| $this->assertSame( | ||
| '[{"op":"replace","path":"\/spec\/replicas","value":3},' | ||
| .'{"op":"remove","path":"\/metadata\/labels\/deprecated"},' | ||
| .'{"op":"add","path":"\/metadata\/labels\/app","value":null}]', | ||
| $payload | ||
| ); | ||
| } | ||
|
|
||
| public function test_empty_json_patch_encodes_as_list() | ||
| { | ||
| $this->assertSame('[]', $this->cluster->configmap()->toJsonPatchPayload([])); | ||
| $this->assertSame('[]', $this->cluster->configmap()->toJsonPatchPayload(new JsonPatch)); | ||
| } | ||
|
|
||
| public function test_json_merge_patch_document_coerces_nested_empty_maps() | ||
| { | ||
| $payload = $this->cluster->statefulSet()->toJsonMergePatchPayload([ | ||
| 'spec' => [ | ||
| 'template' => [ | ||
| 'spec' => [ | ||
| 'volumes' => [ | ||
| ['name' => 'wal', 'emptyDir' => []], | ||
| ], | ||
| ], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| $this->assertStringContainsString('"emptyDir":{}', $payload); | ||
| $this->assertStringNotContainsString('"emptyDir":[]', $payload); | ||
| } | ||
|
|
||
| public function test_json_merge_patch_object_document_coerces_nested_empty_maps() | ||
| { | ||
| $patch = new JsonMergePatch([ | ||
| 'spec' => ['selector' => []], | ||
| ]); | ||
|
|
||
| $payload = $this->cluster->service()->toJsonMergePatchPayload($patch); | ||
|
|
||
| $this->assertStringContainsString('"selector":{}', $payload); | ||
| } | ||
|
|
||
| public function test_json_merge_patch_clearing_finalizers_stays_list() | ||
| { | ||
| $payload = $this->cluster->configmap()->toJsonMergePatchPayload([ | ||
| 'metadata' => ['finalizers' => []], | ||
| ]); | ||
|
|
||
| $this->assertSame('{"metadata":{"finalizers":[]}}', $payload); | ||
| } | ||
|
|
||
| public function test_json_merge_patch_clearing_conditions_stays_list() | ||
| { | ||
| $payload = $this->cluster->deployment()->toJsonMergePatchPayload([ | ||
| 'status' => ['conditions' => []], | ||
| ]); | ||
|
|
||
| $this->assertSame('{"status":{"conditions":[]}}', $payload); | ||
| } | ||
|
|
||
| public function test_empty_json_merge_patch_encodes_as_object() | ||
| { | ||
| $this->assertSame('{}', $this->cluster->configmap()->toJsonMergePatchPayload([])); | ||
| $this->assertSame('{}', $this->cluster->configmap()->toJsonMergePatchPayload(new JsonMergePatch)); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a JSON Patch operation clears a map-valued field such as
/metadata/labelsusingvalue => [], this guard skips coercion and serializes the value as[], causing Kubernetes to reject the patch because the field requires{}.Prompt To Fix With AI