Skip to content

Commit 6051097

Browse files
authored
fix embedded root schema (#63)
* fix embedded root schema * small fix
1 parent 64776fd commit 6051097

File tree

4 files changed

+115
-6
lines changed

4 files changed

+115
-6
lines changed

example/schemaTemplates/embedded/Collection.avsc renamed to example/schemaTemplates/Collection.avsc

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"type": "record",
33
"namespace": "com.example",
4+
"schema_level": "root",
45
"name": "Collection",
56
"fields": [
67
{ "name": "name", "type": "string" },

src/Merger/SchemaMerger.php

+10-1
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,17 @@ private function replaceSchemaIdWithDefinition(
118118
): string {
119119
$idString = '"' . $schemaId . '"';
120120
$pos = (int) strpos($rootDefinition, $idString);
121+
$embeddedDefinitionWithoutLevel = $this->removeSchemaLevel($embeddedDefinition);
121122

122-
return substr_replace($rootDefinition, $embeddedDefinition, $pos, strlen($idString));
123+
return substr_replace($rootDefinition, $embeddedDefinitionWithoutLevel, $pos, strlen($idString));
124+
}
125+
126+
private function removeSchemaLevel(string $embeddedDefinition): string
127+
{
128+
$arraySchema = json_decode($embeddedDefinition, true);
129+
unset($arraySchema['schema_level']);
130+
131+
return json_encode($arraySchema, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
123132
}
124133

125134
/**

tests/Integration/Registry/SchemaRegistryTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function testGetRootSchemas(): void
6161

6262
$rootSchemas = $registry->getRootSchemas();
6363

64-
self::assertCount(2, $rootSchemas);
64+
self::assertCount(3, $rootSchemas);
6565

6666
foreach ($rootSchemas as $rootSchema) {
6767
self::assertInstanceOf(SchemaTemplateInterface::class, $rootSchema);

tests/Unit/Merger/SchemaMergerTest.php

+103-4
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ public function testGetResolvedSchemaTemplate(): void
9595
{ "name": "items", "type": {"type": "array", "items": "com.example.Page" }, "default": [] }
9696
]
9797
}';
98-
$subschemaDefinition = '{
98+
$subschemaDefinition = json_encode(json_decode('{
9999
"type": "record",
100100
"namespace": "com.example",
101101
"name": "Page",
102102
"fields": [
103103
{ "name": "number", "type": "int" }
104104
]
105-
}';
105+
}'));
106106

107107
$expectedResult = str_replace('"com.example.Page"', $subschemaDefinition, $rootDefinition);
108108

@@ -134,6 +134,105 @@ public function testGetResolvedSchemaTemplate(): void
134134
$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
135135
}
136136

137+
public function testGetResolvedSchemaTemplateWithEmbeddedRoot(): void
138+
{
139+
$rootDefinition = '{
140+
"type": "record",
141+
"namespace": "com.example",
142+
"schema_level": "root",
143+
"name": "Library",
144+
"fields": [
145+
{
146+
"name": "name",
147+
"type": "string"
148+
},
149+
{
150+
"name": "foundingYear",
151+
"type": [
152+
"null",
153+
"int"
154+
],
155+
"default": null
156+
},
157+
{
158+
"name": "type",
159+
"type": [
160+
"null",
161+
{
162+
"name": "type",
163+
"type": "enum",
164+
"symbols": [
165+
"PUBLIC",
166+
"PRIVATE"
167+
]
168+
}
169+
],
170+
"default": null
171+
},
172+
{
173+
"name": "collection",
174+
"type": {
175+
"type": "array",
176+
"items": "com.example.Collection"
177+
},
178+
"default": []
179+
},
180+
{
181+
"name": "archive",
182+
"type": {
183+
"type": "array",
184+
"items": "com.example.Collection"
185+
},
186+
"default": []
187+
}
188+
]
189+
}';
190+
$subschemaDefinition = json_encode(json_decode('{
191+
"type": "record",
192+
"namespace": "com.example",
193+
"schema_level": "root",
194+
"name": "Collection",
195+
"fields": [
196+
{ "name": "name", "type": "string" }
197+
]
198+
}'));
199+
200+
$subschemaDefinitionArray = \Safe\json_decode($subschemaDefinition, true);
201+
unset($subschemaDefinitionArray['schema_level']);
202+
$subschemaDefinitionWithoutLevel = json_encode($subschemaDefinitionArray);
203+
204+
$subschemaId = '"com.example.Collection"';
205+
$pos = strpos($rootDefinition, $subschemaId);
206+
$expectedResult = substr_replace($rootDefinition, $subschemaDefinitionWithoutLevel, $pos, strlen($subschemaId));
207+
208+
$subschemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
209+
$subschemaTemplate
210+
->expects(self::once())
211+
->method('getSchemaDefinition')
212+
->willReturn($subschemaDefinition);
213+
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
214+
$schemaRegistry
215+
->expects(self::once())
216+
->method('getSchemaById')
217+
->with('com.example.Collection')
218+
->willReturn($subschemaTemplate);
219+
$rootSchemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
220+
$rootSchemaTemplate
221+
->expects(self::once())
222+
->method('getSchemaDefinition')
223+
->willReturn($rootDefinition);
224+
$rootSchemaTemplate
225+
->expects(self::once())
226+
->method('withSchemaDefinition')
227+
->with($expectedResult)
228+
->willReturn($rootSchemaTemplate);
229+
230+
$merger = new SchemaMerger();
231+
$merger->setSchemaRegistry($schemaRegistry);
232+
233+
$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
234+
}
235+
137236
public function testGetResolvedSchemaTemplateWithMultiEmbedd(): void
138237
{
139238
$rootDefinition = $this->reformatJsonString('{
@@ -315,14 +414,14 @@ public function testGetResolvedSchemaTemplateWithDifferentNamespaceForEmbeddedSc
315414
{ "name": "items", "type": {"type": "array", "items": "com.example.other.Page" }, "default": [] }
316415
]
317416
}';
318-
$subschemaDefinition = '{
417+
$subschemaDefinition = json_encode(json_decode('{
319418
"type": "record",
320419
"namespace": "com.example.other",
321420
"name": "Page",
322421
"fields": [
323422
{ "name": "number", "type": "int" }
324423
]
325-
}';
424+
}'));
326425

327426
$expectedResult = str_replace('"com.example.other.Page"', $subschemaDefinition, $rootDefinition);
328427

0 commit comments

Comments
 (0)