Skip to content
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

feat: support requesting numeric enum rest encoding #395

Merged
merged 7 commits into from
Jul 21, 2022
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
7 changes: 7 additions & 0 deletions src/RequestBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ public function build($path, Message $message, array $headers = [])
);
}

$numericEnums = isset($this->restConfig['numericEnums']) && $this->restConfig['numericEnums'];
$methodConfig = $this->restConfig['interfaces'][$interface][$method] + [
'placeholders' => [],
'body' => null,
Expand All @@ -107,6 +108,12 @@ public function build($path, Message $message, array $headers = [])
// We found a valid uriTemplate - now build and return the Request

list($body, $queryParams) = $this->constructBodyAndQueryParameters($message, $config);

// Request enum fields will be encoded as numbers rather than strings (in the response).
if ($numericEnums) {
$queryParams['$alt'] = "json;enum-encoding=int";
}

$uri = $this->buildUri($pathTemplate, $queryParams);

return new Request(
Expand Down
12 changes: 12 additions & 0 deletions tests/Tests/Unit/RequestBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public function set_up()
'www.example.com',
__DIR__ . '/testdata/test_service_rest_client_config.php'
);
$this->numericEnumsBuilder = new RequestBuilder(
'www.example.com',
__DIR__ . '/testdata/test_numeric_enums_rest_client_config.php'
);
}

public function testMethodWithUrlPlaceholder()
Expand Down Expand Up @@ -378,6 +382,14 @@ public function testMethodWithOneOfInQueryString()
$this->assertSame('some-value', $query['field1']);
}

public function testMethodWithNumericEnumsQueryParam()
{
$request = $this->numericEnumsBuilder->build(self::SERVICE_NAME . '/MethodWithNumericEnumsQueryParam', new MockRequestBody());
$query = Query::parse($request->getUri()->getQuery());

$this->assertEquals('json;enum-encoding=int', $query['$alt']);
}

public function testThrowsExceptionWithNonMatchingFormat()
{
$message = new MockRequestBody();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

return [
'numericEnums' => true,
'interfaces' => [
'test.interface.v1.api' => [
'MethodWithNumericEnumsQueryParam' => [
'method' => 'get',
'uriTemplate' => '/v1/fixedurl',
'queryParams' => [
'name',
'number'
]
]
],
],
];