From 2983ba303545320c7f5a8c8d54290b257aa60e57 Mon Sep 17 00:00:00 2001 From: Edgaras Levinson Date: Mon, 19 Jan 2026 15:55:45 +0200 Subject: [PATCH] fix: preserve zero fraction during response json encoding Fix zero fraction floats (1.0) being incorectly encoded as ints (1). --- src/Controller/GraphController.php | 4 +++- .../App/config/preserveFloats/config.yml | 15 +++++++++++++++ .../preserveFloats/mapping/RootQuery.types.yml | 7 +++++++ .../Functional/Controller/GraphControllerTest.php | 10 ++++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 tests/Functional/App/config/preserveFloats/config.yml create mode 100644 tests/Functional/App/config/preserveFloats/mapping/RootQuery.types.yml diff --git a/src/Controller/GraphController.php b/src/Controller/GraphController.php index f1985ab6b..8e3583e71 100644 --- a/src/Controller/GraphController.php +++ b/src/Controller/GraphController.php @@ -63,7 +63,9 @@ private function createResponse(Request $request, ?string $schemaName, bool $bat return new JsonResponse('', 405); } $payload = $this->processQuery($request, $schemaName, $batched); - $response = new JsonResponse($payload, 200); + $response = new JsonResponse('', 200); + $response->setEncodingOptions(JsonResponse::DEFAULT_ENCODING_OPTIONS|JSON_PRESERVE_ZERO_FRACTION); + $response->setData($payload); } $this->addCORSHeadersIfNeeded($response, $request); diff --git a/tests/Functional/App/config/preserveFloats/config.yml b/tests/Functional/App/config/preserveFloats/config.yml new file mode 100644 index 000000000..0fc6e5b19 --- /dev/null +++ b/tests/Functional/App/config/preserveFloats/config.yml @@ -0,0 +1,15 @@ +imports: + - { resource: ../config.yml } + - { resource: ../exception/services.yml } + +overblog_graphql: + definitions: + class_namespace: "Overblog\\GraphQLBundle\\PreserveFloats\\__DEFINITIONS__" + schema: + query: RootQuery + mutation: RootQuery + mappings: + types: + - + type: yaml + dir: "%kernel.project_dir%/config/preserveFloats/mapping" diff --git a/tests/Functional/App/config/preserveFloats/mapping/RootQuery.types.yml b/tests/Functional/App/config/preserveFloats/mapping/RootQuery.types.yml new file mode 100644 index 000000000..621d9a10d --- /dev/null +++ b/tests/Functional/App/config/preserveFloats/mapping/RootQuery.types.yml @@ -0,0 +1,7 @@ +RootQuery: + type: object + config: + fields: + float: + type: Float + resolve: '1.0' diff --git a/tests/Functional/Controller/GraphControllerTest.php b/tests/Functional/Controller/GraphControllerTest.php index b302a095d..eedbce3c2 100644 --- a/tests/Functional/Controller/GraphControllerTest.php +++ b/tests/Functional/Controller/GraphControllerTest.php @@ -164,6 +164,16 @@ public function testEndpointActionWithInvalidVariables(): void $client->request('GET', '/', ['query' => $query, 'variables' => '"firstFriends": 2}']); } + public function testEndpointActionPreservesFloats(): void + { + $client = static::createClient(['test_case' => 'preserveFloats']); + $this->disableCatchExceptions($client); + + $client->request('GET', '/', ['query' => 'query { float }'], [], ['CONTENT_TYPE' => 'application/graphql;charset=utf8', 'HTTP_Origin' => 'http://example.com']); + $result = $client->getResponse()->getContent(); + $this->assertSame(['data' => ['float' => 1.0]], json_decode($result, true), $result); + } + public function testMultipleEndpointActionWithUnknownSchemaName(): void { $this->expectException(NotFoundHttpException::class);