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 4 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 be encoded as numbers rather than strings.
noahdietz marked this conversation as resolved.
Show resolved Hide resolved
if ($numericEnums) {
$queryParams['$alt'] = "json;enum-encoding=int";
}

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

return new Request(
Expand Down
18 changes: 18 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 setUp()
'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,20 @@ public function testMethodWithOneOfInQueryString()
$this->assertSame('some-value', $query['field1']);
}

public function testMethodWithNumericEnumsQueryParam()
{
$message = (new MockRequestBody())
->setName('')
->setNumber(0);
bshaffer marked this conversation as resolved.
Show resolved Hide resolved

$request = $this->numericEnumsBuilder->build(self::SERVICE_NAME . '/MethodWithNumericEnumsQueryParam', $message);
$query = Query::parse($request->getUri()->getQuery());

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

/**
* @expectedException \Google\ApiCore\ValidationException
* @expectedExceptionMessage Could not map bindings for test.interface.v1.api/MethodWithAdditionalBindings to any Uri template.
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'
]
]
],
],
];