Skip to content

Commit 3cb9923

Browse files
authored
json_encode with preserve zero fraction flag (#60)
* json_encode with preserve zero fraction flag * remove leftovers
1 parent 2a00b0b commit 3cb9923

8 files changed

+75
-7
lines changed

src/Generator/SchemaGenerator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public function generate(): array
8989
$namespace = $schema['namespace'] . '.' . $schema['name'];
9090
}
9191

92-
$schemas[$namespace] = json_encode($schema);
92+
$schemas[$namespace] = json_encode($schema, JSON_PRESERVE_ZERO_FRACTION);
9393
}
9494

9595
return $schemas;

src/Merger/SchemaMerger.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ public function exportSchema(
192192
}
193193

194194
/** @var string $fileContents */
195-
$fileContents = json_encode($rootSchemaDefinition);
195+
$fileContents = json_encode($rootSchemaDefinition, JSON_PRESERVE_ZERO_FRACTION);
196196

197197
file_put_contents($this->getOutputDirectory() . '/' . $schemaFilename, $fileContents);
198198
}

src/Optimizer/FieldOrderOptimizer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@ public function optimize(SchemaTemplateInterface $schemaTemplate): SchemaTemplat
1919

2020
$data = $this->processSchema($data);
2121

22-
return $schemaTemplate->withSchemaDefinition(json_encode($data, JSON_THROW_ON_ERROR));
22+
return $schemaTemplate->withSchemaDefinition(
23+
json_encode($data, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION)
24+
);
2325
}
2426

2527
/**

src/Optimizer/FullNameOptimizer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ public function optimize(SchemaTemplateInterface $schemaTemplate): SchemaTemplat
2020
$currentNamespace = $data['namespace'] ?? '';
2121
$data = $this->processSchema($currentNamespace, $data, true);
2222

23-
return $schemaTemplate->withSchemaDefinition(json_encode($data, JSON_THROW_ON_ERROR));
23+
return $schemaTemplate->withSchemaDefinition(
24+
json_encode($data, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION)
25+
);
2426
}
2527

2628
/**

src/Optimizer/PrimitiveSchemaOptimizer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ public function optimize(SchemaTemplateInterface $schemaTemplate): SchemaTemplat
2323

2424
$data = $this->processSchema($data);
2525

26-
return $schemaTemplate->withSchemaDefinition(json_encode($data, JSON_THROW_ON_ERROR));
26+
return $schemaTemplate->withSchemaDefinition(
27+
json_encode($data, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION)
28+
);
2729
}
2830

2931
/**

tests/Unit/Generator/SchemaGeneratorTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,48 @@ public function testGenerate()
109109
self::assertCount(2, $result);
110110
}
111111

112+
public function testGeneratePreservesZeroFraction()
113+
{
114+
$expectedResult = [
115+
'name.space.TestClass' => json_encode([
116+
'type' => 'record',
117+
'name' => 'TestClass',
118+
'namespace' => 'name.space',
119+
'fields' => [
120+
[
121+
'name' => 'name',
122+
'type' => 'double',
123+
'default' => 0.0,
124+
'doc' => 'test',
125+
'logicalType' => 'test'
126+
]
127+
]
128+
], JSON_PRESERVE_ZERO_FRACTION)
129+
];
130+
131+
$property = $this->getMockForAbstractClass(PhpClassPropertyInterface::class);
132+
$property->expects(self::exactly(1))->method('getPropertyType')->willReturn('double');
133+
$property->expects(self::exactly(1))->method('getPropertyName')->willReturn('name');
134+
$property->expects(self::exactly(2))->method('getPropertyDefault')->willReturn(0.0);
135+
$property->expects(self::exactly(3))->method('getPropertyDoc')->willReturn('test');
136+
$property->expects(self::exactly(2))->method('getPropertyLogicalType')->willReturn('test');
137+
138+
139+
$class = $this->getMockForAbstractClass(PhpClassInterface::class);
140+
$class->expects(self::once())->method('getClassName')->willReturn('TestClass');
141+
$class->expects(self::exactly(2))->method('getClassNamespace')->willReturn('name\\space');
142+
$class->expects(self::once())->method('getClassProperties')->willReturn([$property]);
143+
144+
$registry = $this->getMockForAbstractClass(ClassRegistryInterface::class);
145+
$registry->expects(self::once())->method('getClasses')->willReturn([$class]);
146+
147+
$generator = new SchemaGenerator();
148+
$generator->setClassRegistry($registry);
149+
$result = $generator->generate();
150+
self::assertEquals($expectedResult, $result);
151+
self::assertCount(1, $result);
152+
}
153+
112154
public function testExportSchemas()
113155
{
114156
$schemas = [

tests/Unit/Optimizer/FieldOrderOptimizerTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function testOptimize(): void
4747
},
4848
"default": []
4949
},
50+
{
51+
"name": "price",
52+
"type": "float",
53+
"default": 99.0
54+
},
5055
{
5156
"name": "foreword",
5257
"type": "array",
@@ -143,6 +148,11 @@ public function testOptimize(): void
143148
},
144149
"default": []
145150
},
151+
{
152+
"name": "price",
153+
"type": "float",
154+
"default": 99.0
155+
},
146156
{
147157
"name": "foreword",
148158
"type": "array",
@@ -202,7 +212,7 @@ public function testOptimize(): void
202212
]
203213
}
204214
]
205-
}'));
215+
}'),JSON_PRESERVE_ZERO_FRACTION);
206216

207217
$schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
208218
$schemaTemplate

tests/Unit/Optimizer/FullNameOptimizerTest.php

+11-1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ public function testOptimize(): void
4747
},
4848
"default": []
4949
},
50+
{
51+
"name": "price",
52+
"type": "float",
53+
"default": 99.0
54+
},
5055
{
5156
"name": "foreword",
5257
"type": "array",
@@ -121,6 +126,11 @@ public function testOptimize(): void
121126
},
122127
"default": []
123128
},
129+
{
130+
"name": "price",
131+
"type": "float",
132+
"default": 99.0
133+
},
124134
{
125135
"name": "foreword",
126136
"type": "array",
@@ -159,7 +169,7 @@ public function testOptimize(): void
159169
},
160170
{ "name": "backSide", "type": "com.example.other.Cover"}
161171
]
162-
}'));
172+
}'), JSON_PRESERVE_ZERO_FRACTION);
163173

164174

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

0 commit comments

Comments
 (0)