Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Client/HangarAPIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
27 changes: 23 additions & 4 deletions tests/Unit/Client/HangarAPIClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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());
}
Expand All @@ -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
*/
Expand All @@ -120,7 +139,7 @@ public function testGetProjectsWithOptions(): void
$client->getProjects($options);

$this->assertGetRequest("/projects", [
"prioritizeExactMatch" => "0",
"prioritizeExactMatch" => "false",
"sort" => "-stars",
"category" => "chat",
"platform" => "PAPER",
Expand Down Expand Up @@ -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",
]);
Expand All @@ -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",
Expand Down
Loading