Skip to content

Commit

Permalink
json_encode with preserve zero fraction flag (#60)
Browse files Browse the repository at this point in the history
* json_encode with preserve zero fraction flag

* remove leftovers
  • Loading branch information
andrei-arobs authored Nov 23, 2022
1 parent 2a00b0b commit 3cb9923
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/Generator/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/Merger/SchemaMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
4 changes: 3 additions & 1 deletion src/Optimizer/FieldOrderOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Optimizer/FullNameOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down
4 changes: 3 additions & 1 deletion src/Optimizer/PrimitiveSchemaOptimizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

/**
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/Generator/SchemaGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down
12 changes: 11 additions & 1 deletion tests/Unit/Optimizer/FieldOrderOptimizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function testOptimize(): void
},
"default": []
},
{
"name": "price",
"type": "float",
"default": 99.0
},
{
"name": "foreword",
"type": "array",
Expand Down Expand Up @@ -143,6 +148,11 @@ public function testOptimize(): void
},
"default": []
},
{
"name": "price",
"type": "float",
"default": 99.0
},
{
"name": "foreword",
"type": "array",
Expand Down Expand Up @@ -202,7 +212,7 @@ public function testOptimize(): void
]
}
]
}'));
}'),JSON_PRESERVE_ZERO_FRACTION);

$schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
$schemaTemplate
Expand Down
12 changes: 11 additions & 1 deletion tests/Unit/Optimizer/FullNameOptimizerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public function testOptimize(): void
},
"default": []
},
{
"name": "price",
"type": "float",
"default": 99.0
},
{
"name": "foreword",
"type": "array",
Expand Down Expand Up @@ -121,6 +126,11 @@ public function testOptimize(): void
},
"default": []
},
{
"name": "price",
"type": "float",
"default": 99.0
},
{
"name": "foreword",
"type": "array",
Expand Down Expand Up @@ -159,7 +169,7 @@ public function testOptimize(): void
},
{ "name": "backSide", "type": "com.example.other.Cover"}
]
}'));
}'), JSON_PRESERVE_ZERO_FRACTION);


$schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
Expand Down

0 comments on commit 3cb9923

Please sign in to comment.