Skip to content

Commit

Permalink
fix embedded root schema (#63)
Browse files Browse the repository at this point in the history
* fix embedded root schema

* small fix
  • Loading branch information
nick-zh committed Nov 24, 2023
1 parent 6c9f01d commit 42b0c4f
Show file tree
Hide file tree
Showing 4 changed files with 114 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 @@ -102,8 +102,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, JSON_THROW_ON_ERROR | JSON_PRESERVE_ZERO_FRACTION);
}

/**
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 @@ -59,7 +59,7 @@ public function testGetRootSchemas()

$rootSchemas = $registry->getRootSchemas();

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

foreach ($rootSchemas as $rootSchema) {
self::assertInstanceOf(SchemaTemplateInterface::class, $rootSchema);
Expand Down
106 changes: 102 additions & 4 deletions tests/Unit/Merger/SchemaMergerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,14 +86,14 @@ public function testGetResolvedSchemaTemplate()
{ "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 @@ -124,6 +124,104 @@ public function testGetResolvedSchemaTemplate()
$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($schemaRegistry);

$merger->getResolvedSchemaTemplate($rootSchemaTemplate);
}

public function testGetResolvedSchemaTemplateWithMultiEmbedd()
{
$rootDefinition = $this->reformatJsonString('{
Expand Down Expand Up @@ -304,14 +402,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 42b0c4f

Please sign in to comment.