From cac6245e9786a35a24392392f8e4335c94879654 Mon Sep 17 00:00:00 2001 From: SergkeiM Date: Tue, 28 Jul 2026 15:01:38 +0300 Subject: [PATCH] Allow optional 'rules' parameter for ruleset creation The Cloudflare API permits creating rulesets without providing the 'rules' array, or with an empty one. Previously, the client library incorrectly marked 'rules' as a required field, preventing successful creation in these valid scenarios. This change corrects the client's validation and adds tests to confirm the new behavior. --- src/Endpoints/Accounts/Rulesets.php | 2 +- src/Endpoints/Zones/Rulesets.php | 2 +- .../Tests/Endpoints/Accounts/RulesetsTest.php | 35 +++++++++++++++++++ test/Tests/Endpoints/Zones/RulesetsTest.php | 35 +++++++++++++++++++ 4 files changed, 72 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/Accounts/Rulesets.php b/src/Endpoints/Accounts/Rulesets.php index 6964661..788230f 100644 --- a/src/Endpoints/Accounts/Rulesets.php +++ b/src/Endpoints/Accounts/Rulesets.php @@ -35,7 +35,7 @@ public function get(string $accountId): ResponseInterface public function create(string $accountId, array|Ruleset $values): ResponseInterface { if (is_array($values)) { - $this->requiredParams(['name', 'kind', 'phase', 'rules'], $values); + $this->requiredParams(['name', 'kind', 'phase'], $values); } else { $values = $values->toArray(); } diff --git a/src/Endpoints/Zones/Rulesets.php b/src/Endpoints/Zones/Rulesets.php index 972067a..b48cf17 100644 --- a/src/Endpoints/Zones/Rulesets.php +++ b/src/Endpoints/Zones/Rulesets.php @@ -35,7 +35,7 @@ public function get(string $zoneId): ResponseInterface public function create(string $zoneId, array|Ruleset $values): ResponseInterface { if (is_array($values)) { - $this->requiredParams(['name', 'kind', 'phase', 'rules'], $values); + $this->requiredParams(['name', 'kind', 'phase'], $values); } else { $values = $values->toArray(); } diff --git a/test/Tests/Endpoints/Accounts/RulesetsTest.php b/test/Tests/Endpoints/Accounts/RulesetsTest.php index db573f9..2d13a77 100644 --- a/test/Tests/Endpoints/Accounts/RulesetsTest.php +++ b/test/Tests/Endpoints/Accounts/RulesetsTest.php @@ -46,6 +46,41 @@ public function shouldCreateWithArray() $this->assertSame('/client/v4/accounts/account_id/rulesets', $this->lastRequest()->getUri()->getPath()); } + #[Test] + public function shouldCreateWithEmptyRulesArray() + { + $client = $this->mockClient([ + new Response(200, [], json_encode(['success' => true, 'result' => ['id' => 'ruleset_id']])), + ]); + + $response = $client->accounts()->rulesets()->create('account_id', [ + 'name' => 'my ruleset', + 'kind' => 'root', + 'phase' => 'http_request_firewall_custom', + 'rules' => [], + ]); + + $this->assertTrue($response->successful()); + $this->assertSame('POST', $this->lastRequest()->getMethod()); + } + + #[Test] + public function shouldCreateWithoutRulesKey() + { + $client = $this->mockClient([ + new Response(200, [], json_encode(['success' => true, 'result' => ['id' => 'ruleset_id']])), + ]); + + $response = $client->accounts()->rulesets()->create('account_id', [ + 'name' => 'my ruleset', + 'kind' => 'root', + 'phase' => 'http_request_firewall_custom', + ]); + + $this->assertTrue($response->successful()); + $this->assertSame('POST', $this->lastRequest()->getMethod()); + } + #[Test] public function shouldCreateWithRulesetObject() { diff --git a/test/Tests/Endpoints/Zones/RulesetsTest.php b/test/Tests/Endpoints/Zones/RulesetsTest.php index 2dc31a1..a4333f7 100644 --- a/test/Tests/Endpoints/Zones/RulesetsTest.php +++ b/test/Tests/Endpoints/Zones/RulesetsTest.php @@ -46,6 +46,41 @@ public function shouldCreateWithArray() $this->assertSame('/client/v4/zones/zone_id/rulesets', $this->lastRequest()->getUri()->getPath()); } + #[Test] + public function shouldCreateWithEmptyRulesArray() + { + $client = $this->mockClient([ + new Response(200, [], json_encode(['success' => true, 'result' => ['id' => 'ruleset_id']])), + ]); + + $response = $client->zones()->rulesets()->create('zone_id', [ + 'name' => 'my ruleset', + 'kind' => 'zone', + 'phase' => 'http_request_firewall_custom', + 'rules' => [], + ]); + + $this->assertTrue($response->successful()); + $this->assertSame('POST', $this->lastRequest()->getMethod()); + } + + #[Test] + public function shouldCreateWithoutRulesKey() + { + $client = $this->mockClient([ + new Response(200, [], json_encode(['success' => true, 'result' => ['id' => 'ruleset_id']])), + ]); + + $response = $client->zones()->rulesets()->create('zone_id', [ + 'name' => 'my ruleset', + 'kind' => 'zone', + 'phase' => 'http_request_firewall_custom', + ]); + + $this->assertTrue($response->successful()); + $this->assertSame('POST', $this->lastRequest()->getMethod()); + } + #[Test] public function shouldCreateWithRulesetObject() {