diff --git a/src/Client/HangarAPIClient.php b/src/Client/HangarAPIClient.php index 5c95e9c..d2d7a0d 100644 --- a/src/Client/HangarAPIClient.php +++ b/src/Client/HangarAPIClient.php @@ -116,6 +116,14 @@ public static function getDefaultUserAgent(): string public function setConfiguration(Configuration $configuration): static { $this->configuration = $configuration; + $this->configuration->setBooleanFormatForQueryString(Configuration::BOOLEAN_FORMAT_STRING); + + // The ObjectSerializer reads the boolean query string format from the default configuration + // instead of the one used for the request, so it has to be set there as well. + // Without this, boolean query parameters are sent as 1/0. + Configuration::getDefaultConfiguration() + ->setBooleanFormatForQueryString(Configuration::BOOLEAN_FORMAT_STRING); + $this->projects = new ProjectsApi($this->httpClient, $this->configuration); $this->versions = new VersionsApi($this->httpClient, $this->configuration); $this->users = new UsersApi($this->httpClient, $this->configuration); diff --git a/tests/Unit/Client/HangarAPIClientTest.php b/tests/Unit/Client/HangarAPIClientTest.php index 8c51a0c..9cd8ccc 100644 --- a/tests/Unit/Client/HangarAPIClientTest.php +++ b/tests/Unit/Client/HangarAPIClientTest.php @@ -16,11 +16,16 @@ use Aternos\HangarApi\Client\Project; use Aternos\HangarApi\Client\User; use Aternos\HangarApi\Client\Version; +use Aternos\HangarApi\Configuration; use Aternos\HangarApi\Model\Category; use Aternos\HangarApi\Model\Platform; use Aternos\HangarApi\Model\ProjectMember; use Aternos\HangarApi\Model\Tag; use Composer\InstalledVersions; +use GuzzleHttp\Client; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Middleware; use GuzzleHttp\Psr7\Response; class HangarAPIClientTest extends ClientTestCase @@ -88,7 +93,7 @@ public function testGetProjects(): void $projects = $client->getProjects(); - $this->assertGetRequest("/projects", ["prioritizeExactMatch" => "1", "limit" => "25", "offset" => "0"]); + $this->assertGetRequest("/projects", ["prioritizeExactMatch" => "true", "limit" => "25", "offset" => "0"]); foreach (["sort", "category", "platform", "owner", "query", "license", "version", "tag"] as $parameter) { $this->assertArrayNotHasKey($parameter, $this->getQuery()); } @@ -97,6 +102,20 @@ public function testGetProjects(): void $this->assertSame("WirelessRedstoneTrigger", $projects[0]->getSlug()); } + /** + * @throws ApiException + */ + public function testBooleanQueryParametersAreSentAsStringWithCustomConfiguration(): void + { + $stack = HandlerStack::create(new MockHandler([new Response(200, [], file_get_contents(__DIR__ . "/Fixtures/getProjects.json"))])); + $stack->push(Middleware::history($this->requests)); + $client = new HangarAPIClient(new Configuration(), httpClient: new Client(['handler' => $stack])); + + $client->getProjects(new ProjectSearchOptions()->setPrioritizeExactMatch(false)); + + $this->assertSame("false", $this->getQuery()["prioritizeExactMatch"]); + } + /** * @throws ApiException */ @@ -120,7 +139,7 @@ public function testGetProjectsWithOptions(): void $client->getProjects($options); $this->assertGetRequest("/projects", [ - "prioritizeExactMatch" => "0", + "prioritizeExactMatch" => "false", "sort" => "-stars", "category" => "chat", "platform" => "PAPER", @@ -214,7 +233,7 @@ public function testGetProjectVersions(): void $versions = $client->getProjectVersions("mclogs", new VersionSearchOptions("mclogs")); $this->assertGetRequest("/projects/mclogs/versions", [ - "includeHiddenChannels" => "1", + "includeHiddenChannels" => "true", "limit" => "25", "offset" => "0", ]); @@ -240,7 +259,7 @@ public function testGetProjectVersionsWithOptions(): void $client->getProjectVersions("mclogs", $options); $this->assertGetRequest("/projects/mclogs/versions", [ - "includeHiddenChannels" => "0", + "includeHiddenChannels" => "false", "channel" => "Release", "platform" => "PAPER", "limit" => "2",