Skip to content

Commit

Permalink
fix embedded root schema
Browse files Browse the repository at this point in the history
  • Loading branch information
nick-zh committed Nov 24, 2023
1 parent 64776fd commit da2d170
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"type": "record",
"namespace": "com.example",
"schema_level": "root",
"name": "Collection",
"fields": [
{ "name": "name", "type": "string" },
Expand Down
11 changes: 10 additions & 1 deletion src/Merger/SchemaMerger.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,17 @@ private function replaceSchemaIdWithDefinition(
): string {
$idString = '"' . $schemaId . '"';
$pos = (int) strpos($rootDefinition, $idString);
$embeddedDefinitionWithoutLevel = $this->removeSchemaLevel($embeddedDefinition);

return substr_replace($rootDefinition, $embeddedDefinition, $pos, strlen($idString));
return substr_replace($rootDefinition, $embeddedDefinitionWithoutLevel, $pos, strlen($idString));
}

private function removeSchemaLevel(string $embeddedDefinition): string
{
$arraySchema = json_decode($embeddedDefinition, true);
unset($arraySchema['schema_level']);

return json_encode($arraySchema) ?? '';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Integration/Registry/SchemaRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function testGetRootSchemas(): void

$rootSchemas = $registry->getRootSchemas();

self::assertCount(2, $rootSchemas);
self::assertCount(3, $rootSchemas);

foreach ($rootSchemas as $rootSchema) {
self::assertInstanceOf(SchemaTemplateInterface::class, $rootSchema);
Expand Down
107 changes: 103 additions & 4 deletions tests/Unit/Merger/SchemaMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,14 @@ public function testGetResolvedSchemaTemplate(): void
{ "name": "items", "type": {"type": "array", "items": "com.example.Page" }, "default": [] }
]
}';
$subschemaDefinition = '{
$subschemaDefinition = json_encode(json_decode('{
"type": "record",
"namespace": "com.example",
"name": "Page",
"fields": [
{ "name": "number", "type": "int" }
]
}';
}'));

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

Expand Down Expand Up @@ -134,6 +134,105 @@ public function testGetResolvedSchemaTemplate(): void
$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
}

public function testGetResolvedSchemaTemplateWithEmbeddedRoot(): void
{
$rootDefinition = '{
"type": "record",
"namespace": "com.example",
"schema_level": "root",
"name": "Library",
"fields": [
{
"name": "name",
"type": "string"
},
{
"name": "foundingYear",
"type": [
"null",
"int"
],
"default": null
},
{
"name": "type",
"type": [
"null",
{
"name": "type",
"type": "enum",
"symbols": [
"PUBLIC",
"PRIVATE"
]
}
],
"default": null
},
{
"name": "collection",
"type": {
"type": "array",
"items": "com.example.Collection"
},
"default": []
},
{
"name": "archive",
"type": {
"type": "array",
"items": "com.example.Collection"
},
"default": []
}
]
}';
$subschemaDefinition = json_encode(json_decode('{
"type": "record",
"namespace": "com.example",
"schema_level": "root",
"name": "Collection",
"fields": [
{ "name": "name", "type": "string" }
]
}'));

$subschemaDefinitionArray = \Safe\json_decode($subschemaDefinition, true);
unset($subschemaDefinitionArray['schema_level']);
$subschemaDefinitionWithoutLevel = json_encode($subschemaDefinitionArray);

$subschemaId = '"com.example.Collection"';
$pos = strpos($rootDefinition, $subschemaId);
$expectedResult = substr_replace($rootDefinition, $subschemaDefinitionWithoutLevel, $pos, strlen($subschemaId));

$subschemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
$subschemaTemplate
->expects(self::once())
->method('getSchemaDefinition')
->willReturn($subschemaDefinition);
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
$schemaRegistry
->expects(self::once())
->method('getSchemaById')
->with('com.example.Collection')
->willReturn($subschemaTemplate);
$rootSchemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
$rootSchemaTemplate
->expects(self::once())
->method('getSchemaDefinition')
->willReturn($rootDefinition);
$rootSchemaTemplate
->expects(self::once())
->method('withSchemaDefinition')
->with($expectedResult)
->willReturn($rootSchemaTemplate);

$merger = new SchemaMerger();
$merger->setSchemaRegistry($schemaRegistry);

$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
}

public function testGetResolvedSchemaTemplateWithMultiEmbedd(): void
{
$rootDefinition = $this->reformatJsonString('{
Expand Down Expand Up @@ -315,14 +414,14 @@ public function testGetResolvedSchemaTemplateWithDifferentNamespaceForEmbeddedSc
{ "name": "items", "type": {"type": "array", "items": "com.example.other.Page" }, "default": [] }
]
}';
$subschemaDefinition = '{
$subschemaDefinition = json_encode(json_decode('{
"type": "record",
"namespace": "com.example.other",
"name": "Page",
"fields": [
{ "name": "number", "type": "int" }
]
}';
}'));

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

Expand Down

0 comments on commit da2d170

Please sign in to comment.