From 3cb99235dfa5a335245a520fc21fbdf5824e41a6 Mon Sep 17 00:00:00 2001 From: Andrei Pop Date: Wed, 23 Nov 2022 20:32:52 +0200 Subject: [PATCH] json_encode with preserve zero fraction flag (#60) * json_encode with preserve zero fraction flag * remove leftovers --- src/Generator/SchemaGenerator.php | 2 +- src/Merger/SchemaMerger.php | 2 +- src/Optimizer/FieldOrderOptimizer.php | 4 +- src/Optimizer/FullNameOptimizer.php | 4 +- src/Optimizer/PrimitiveSchemaOptimizer.php | 4 +- tests/Unit/Generator/SchemaGeneratorTest.php | 42 +++++++++++++++++++ .../Optimizer/FieldOrderOptimizerTest.php | 12 +++++- .../Unit/Optimizer/FullNameOptimizerTest.php | 12 +++++- 8 files changed, 75 insertions(+), 7 deletions(-) diff --git a/src/Generator/SchemaGenerator.php b/src/Generator/SchemaGenerator.php index 9e99bc6..a183ca9 100644 --- a/src/Generator/SchemaGenerator.php +++ b/src/Generator/SchemaGenerator.php @@ -89,7 +89,7 @@ public function generate(): array $namespace = $schema['namespace'] . '.' . $schema['name']; } - $schemas[$namespace] = json_encode($schema); + $schemas[$namespace] = json_encode($schema, JSON_PRESERVE_ZERO_FRACTION); } return $schemas; diff --git a/src/Merger/SchemaMerger.php b/src/Merger/SchemaMerger.php index 70a20f7..f9a9abb 100644 --- a/src/Merger/SchemaMerger.php +++ b/src/Merger/SchemaMerger.php @@ -192,7 +192,7 @@ public function exportSchema( } /** @var string $fileContents */ - $fileContents = json_encode($rootSchemaDefinition); + $fileContents = json_encode($rootSchemaDefinition, JSON_PRESERVE_ZERO_FRACTION); file_put_contents($this->getOutputDirectory() . '/' . $schemaFilename, $fileContents); } diff --git a/src/Optimizer/FieldOrderOptimizer.php b/src/Optimizer/FieldOrderOptimizer.php index 8e6815b..4aac197 100644 --- a/src/Optimizer/FieldOrderOptimizer.php +++ b/src/Optimizer/FieldOrderOptimizer.php @@ -19,7 +19,9 @@ public function optimize(SchemaTemplateInterface $schemaTemplate): SchemaTemplat $data = $this->processSchema($data); - return $schemaTemplate->withSchemaDefinition(json_encode($data, JSON_THROW_ON_ERROR)); + return $schemaTemplate->withSchemaDefinition( + json_encode($data, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION) + ); } /** diff --git a/src/Optimizer/FullNameOptimizer.php b/src/Optimizer/FullNameOptimizer.php index 3f79667..642a8dc 100644 --- a/src/Optimizer/FullNameOptimizer.php +++ b/src/Optimizer/FullNameOptimizer.php @@ -20,7 +20,9 @@ public function optimize(SchemaTemplateInterface $schemaTemplate): SchemaTemplat $currentNamespace = $data['namespace'] ?? ''; $data = $this->processSchema($currentNamespace, $data, true); - return $schemaTemplate->withSchemaDefinition(json_encode($data, JSON_THROW_ON_ERROR)); + return $schemaTemplate->withSchemaDefinition( + json_encode($data, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION) + ); } /** diff --git a/src/Optimizer/PrimitiveSchemaOptimizer.php b/src/Optimizer/PrimitiveSchemaOptimizer.php index f2e2164..143e655 100644 --- a/src/Optimizer/PrimitiveSchemaOptimizer.php +++ b/src/Optimizer/PrimitiveSchemaOptimizer.php @@ -23,7 +23,9 @@ public function optimize(SchemaTemplateInterface $schemaTemplate): SchemaTemplat $data = $this->processSchema($data); - return $schemaTemplate->withSchemaDefinition(json_encode($data, JSON_THROW_ON_ERROR)); + return $schemaTemplate->withSchemaDefinition( + json_encode($data, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION) + ); } /** diff --git a/tests/Unit/Generator/SchemaGeneratorTest.php b/tests/Unit/Generator/SchemaGeneratorTest.php index 0a798e6..bd4199a 100644 --- a/tests/Unit/Generator/SchemaGeneratorTest.php +++ b/tests/Unit/Generator/SchemaGeneratorTest.php @@ -109,6 +109,48 @@ public function testGenerate() self::assertCount(2, $result); } + public function testGeneratePreservesZeroFraction() + { + $expectedResult = [ + 'name.space.TestClass' => json_encode([ + 'type' => 'record', + 'name' => 'TestClass', + 'namespace' => 'name.space', + 'fields' => [ + [ + 'name' => 'name', + 'type' => 'double', + 'default' => 0.0, + 'doc' => 'test', + 'logicalType' => 'test' + ] + ] + ], JSON_PRESERVE_ZERO_FRACTION) + ]; + + $property = $this->getMockForAbstractClass(PhpClassPropertyInterface::class); + $property->expects(self::exactly(1))->method('getPropertyType')->willReturn('double'); + $property->expects(self::exactly(1))->method('getPropertyName')->willReturn('name'); + $property->expects(self::exactly(2))->method('getPropertyDefault')->willReturn(0.0); + $property->expects(self::exactly(3))->method('getPropertyDoc')->willReturn('test'); + $property->expects(self::exactly(2))->method('getPropertyLogicalType')->willReturn('test'); + + + $class = $this->getMockForAbstractClass(PhpClassInterface::class); + $class->expects(self::once())->method('getClassName')->willReturn('TestClass'); + $class->expects(self::exactly(2))->method('getClassNamespace')->willReturn('name\\space'); + $class->expects(self::once())->method('getClassProperties')->willReturn([$property]); + + $registry = $this->getMockForAbstractClass(ClassRegistryInterface::class); + $registry->expects(self::once())->method('getClasses')->willReturn([$class]); + + $generator = new SchemaGenerator(); + $generator->setClassRegistry($registry); + $result = $generator->generate(); + self::assertEquals($expectedResult, $result); + self::assertCount(1, $result); + } + public function testExportSchemas() { $schemas = [ diff --git a/tests/Unit/Optimizer/FieldOrderOptimizerTest.php b/tests/Unit/Optimizer/FieldOrderOptimizerTest.php index e3c7884..7be0902 100644 --- a/tests/Unit/Optimizer/FieldOrderOptimizerTest.php +++ b/tests/Unit/Optimizer/FieldOrderOptimizerTest.php @@ -47,6 +47,11 @@ public function testOptimize(): void }, "default": [] }, + { + "name": "price", + "type": "float", + "default": 99.0 + }, { "name": "foreword", "type": "array", @@ -143,6 +148,11 @@ public function testOptimize(): void }, "default": [] }, + { + "name": "price", + "type": "float", + "default": 99.0 + }, { "name": "foreword", "type": "array", @@ -202,7 +212,7 @@ public function testOptimize(): void ] } ] - }')); + }'),JSON_PRESERVE_ZERO_FRACTION); $schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class); $schemaTemplate diff --git a/tests/Unit/Optimizer/FullNameOptimizerTest.php b/tests/Unit/Optimizer/FullNameOptimizerTest.php index 724daa4..69357e4 100644 --- a/tests/Unit/Optimizer/FullNameOptimizerTest.php +++ b/tests/Unit/Optimizer/FullNameOptimizerTest.php @@ -47,6 +47,11 @@ public function testOptimize(): void }, "default": [] }, + { + "name": "price", + "type": "float", + "default": 99.0 + }, { "name": "foreword", "type": "array", @@ -121,6 +126,11 @@ public function testOptimize(): void }, "default": [] }, + { + "name": "price", + "type": "float", + "default": 99.0 + }, { "name": "foreword", "type": "array", @@ -159,7 +169,7 @@ public function testOptimize(): void }, { "name": "backSide", "type": "com.example.other.Cover"} ] - }')); + }'), JSON_PRESERVE_ZERO_FRACTION); $schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);