Skip to content

Commit 6a2f29a

Browse files
authored
feat(BE-3407): add JSON_PRESERVE_ZERO_FRACTION on json_encode (#20)
1 parent 13cede4 commit 6a2f29a

File tree

4 files changed

+46
-17
lines changed

4 files changed

+46
-17
lines changed

src/HttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private function createRequest(
7676
$request = $this->requestFactory->createRequest($method, $url);
7777

7878
if ([] !== $body) {
79-
$jsonData = json_encode($body, JSON_THROW_ON_ERROR);
79+
$jsonData = json_encode($body, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
8080

8181
$dataLength = strlen($jsonData);
8282

src/KafkaSchemaRegistryApiClient.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,9 @@ public function setImportMode(string $mode): bool
260260
*/
261261
private function createRequestBodyFromSchema(string $schema): array
262262
{
263-
return ['schema' => json_encode(json_decode($schema, true, 512, JSON_THROW_ON_ERROR), JSON_THROW_ON_ERROR)];
263+
return ['schema' => json_encode(
264+
json_decode($schema, true, 512, JSON_THROW_ON_ERROR),
265+
JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION
266+
)];
264267
}
265268
}

tests/HttpClientTest.php

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ public function testCreateRequest(): void
3939
$this->assertSame('', $response->getUri()->getQuery());
4040
}
4141

42-
public function testCreateRequestWithBody(): void
42+
/**
43+
* @dataProvider requestBodyDataProvider
44+
**/
45+
public function testCreateRequestWithBody(array $body, string $expectedEncodedBody): void
4346
{
4447
$httpClient = new HttpClient(
4548
new Curl(new Psr17Factory()),
@@ -48,15 +51,11 @@ public function testCreateRequestWithBody(): void
4851
'http://some-url/'
4952
);
5053

51-
$body = ['a' => 'b'];
52-
$jsonEncodedBody = json_encode($body);
53-
5454
/** @var RequestInterface $response */
5555
$response = $this->invokeMethod($httpClient, 'createRequest', ['GET', 'uri', $body]);
5656
$response->getBody()->rewind();
5757

58-
$this->assertSame('9', $response->getHeader('Content-Length')[0]);
59-
$this->assertSame($jsonEncodedBody, $response->getBody()->read(9));
58+
$this->assertSame($expectedEncodedBody, $response->getBody()->getContents());
6059
}
6160

6261
public function testCreateRequestWithQueryString(): void
@@ -164,4 +163,13 @@ public function testCallMethodWithThrownException(): void
164163
$this->expectException(Exception::class);
165164
$httpClient->call('GET', 'uri');
166165
}
166+
167+
public function requestBodyDataProvider(): array
168+
{
169+
return [
170+
[['a' => 'b'], '{"a":"b"}'],
171+
[['a' => 0.0], '{"a":0.0}'],
172+
[['a' => '{"b":0.0}'], '{"a":"{\"b\":0.0}"}'],
173+
];
174+
}
167175
}

tests/KafkaSchemaRegistryApiClientTest.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -123,18 +123,21 @@ public function testGetSchemaById(): void
123123
$api->getSchemaById(1);
124124
}
125125

126-
public function testRegisterNewSchemaVersion(): void
126+
/**
127+
* @dataProvider schemaDataProvider
128+
**/
129+
public function testRegisterNewSchemaVersion(string $testSchema, string $expectedSchema): void
127130
{
128131
$httpClientMock = $this->getHttpClientMock();
129132

130133
$httpClientMock
131134
->expects(self::once())
132135
->method('call')
133-
->with('POST', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME), ['schema' => '[]'])
136+
->with('POST', sprintf('subjects/%s/versions', self::TEST_SUBJECT_NAME), ['schema' => $expectedSchema])
134137
->willReturn([]);
135138

136139
$api = new KafkaSchemaRegistryApiClient($httpClientMock);
137-
$api->registerNewSchemaVersion(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA);
140+
$api->registerNewSchemaVersion(self::TEST_SUBJECT_NAME, $testSchema);
138141
}
139142

140143
public function testCheckSchemaCompatibilityForVersionFalseOnEmptyResponse(): void
@@ -161,7 +164,10 @@ public function testCheckSchemaCompatibilityForVersionFalseOnEmptyResponse(): vo
161164
self::assertFalse($result);
162165
}
163166

164-
public function testCheckSchemaCompatibilityForVersionTrue(): void
167+
/**
168+
* @dataProvider schemaDataProvider
169+
**/
170+
public function testCheckSchemaCompatibilityForVersionTrue(string $testSchema, string $expectedSchema): void
165171
{
166172
$httpClientMock = $this->getHttpClientMock();
167173

@@ -171,14 +177,14 @@ public function testCheckSchemaCompatibilityForVersionTrue(): void
171177
->with(
172178
'POST',
173179
sprintf('compatibility/subjects/%s/versions/%s', self::TEST_SUBJECT_NAME, self::TEST_VERSION),
174-
['schema' => '[]']
180+
['schema' => $expectedSchema]
175181
)
176182
->willReturn(['is_compatible' => true]);
177183

178184
$api = new KafkaSchemaRegistryApiClient($httpClientMock);
179185
$result = $api->checkSchemaCompatibilityForVersion(
180186
self::TEST_SUBJECT_NAME,
181-
self::TEST_SCHEMA,
187+
$testSchema,
182188
self::TEST_VERSION
183189
);
184190

@@ -378,7 +384,10 @@ public function testSetDefaultCompatibilityLeve(): void
378384
self::assertTrue($result);
379385
}
380386

381-
public function testGetVersionForSchema(): void
387+
/**
388+
* @dataProvider schemaDataProvider
389+
**/
390+
public function testGetVersionForSchema(string $testSchema, string $expectedSchema): void
382391
{
383392
$httpClientMock = $this->getHttpClientMock();
384393

@@ -388,12 +397,12 @@ public function testGetVersionForSchema(): void
388397
->with(
389398
'POST',
390399
sprintf('subjects/%s', self::TEST_SUBJECT_NAME),
391-
['schema' => '[]']
400+
['schema' => $expectedSchema]
392401
)
393402
->willReturn(['version' => self::TEST_VERSION]);
394403

395404
$api = new KafkaSchemaRegistryApiClient($httpClientMock);
396-
$result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, self::TEST_SCHEMA);
405+
$result = $api->getVersionForSchema(self::TEST_SUBJECT_NAME, $testSchema);
397406

398407
self::assertSame((string) self::TEST_VERSION, $result);
399408
}
@@ -560,4 +569,13 @@ private function getHttpClientMock(): MockObject
560569
->onlyMethods(['call'])
561570
->getMock();
562571
}
572+
573+
public function schemaDataProvider(): array
574+
{
575+
return [
576+
'empty schema' => ['{}', '[]'],
577+
'schema' => ['{"id": "string"}', '{"id":"string"}'],
578+
'preserves zero fraction' => ['{"double":0.0}', '{"double":0.0}'],
579+
];
580+
}
563581
}

0 commit comments

Comments
 (0)