Skip to content

Commit 3187cf1

Browse files
authored
Exclude namespace in type and items (#10)
1 parent 87cb2d4 commit 3187cf1

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

src/Merger/SchemaMerger.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,12 @@ public function merge(
132132
} catch (SchemaMergerException $e) {
133133
throw $e;
134134
}
135-
$this->exportSchema($resolvedTemplate, $prefixWithNamespace, $useTemplateName);
135+
$this->exportSchema(
136+
$resolvedTemplate,
137+
$prefixWithNamespace,
138+
$useTemplateName,
139+
$optimizeSubSchemaNamespaces
140+
);
136141
++$mergedFiles;
137142
}
138143

@@ -148,7 +153,8 @@ public function merge(
148153
public function exportSchema(
149154
SchemaTemplateInterface $rootSchemaTemplate,
150155
bool $prefixWithNamespace = false,
151-
bool $useTemplateName = false
156+
bool $useTemplateName = false,
157+
bool $optimizeSubSchemaNamespaces = false
152158
): void {
153159
$rootSchemaDefinition = $this->transformExportSchemaDefinition(
154160
json_decode($rootSchemaTemplate->getSchemaDefinition(), true)
@@ -170,8 +176,14 @@ public function exportSchema(
170176
mkdir($this->getOutputDirectory());
171177
}
172178

179+
/** @var string $fileContents */
173180
$fileContents = json_encode($rootSchemaDefinition);
174181

182+
if (true === $optimizeSubSchemaNamespaces) {
183+
$embeddedSchemaNamespace = $rootSchemaDefinition['namespace'] . '.';
184+
$fileContents = str_replace($embeddedSchemaNamespace, '', $fileContents);
185+
}
186+
175187
file_put_contents($this->getOutputDirectory() . '/' . $schemaFilename, $fileContents);
176188
}
177189

tests/Unit/Merger/SchemaMergerTest.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -368,14 +368,42 @@ public function testExportSchema()
368368
->willReturn('{"name": "test"}');
369369
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
370370

371-
372371
$merger = new SchemaMerger($schemaRegistry);
373372
$merger->exportSchema($schemaTemplate);
374373

375374
self::assertFileExists('/tmp/test.avsc');
376375
unlink('/tmp/test.avsc');
377376
}
378377

378+
public function testExportSchemaWithExcludingNamespaces()
379+
{
380+
$mergedSchema = '{"type":"record","name":"schema","namespace":"root.level.entity","schema_level":"root","fields":[{"name":"rootField1","type":{"type":"record","name":"embeddedSchema","fields":[{"name":"embeddedField","type":["null","string"],"default":null}]}},{"name":"rootField2","type":["null","root.level.entity.embeddedSchema"],"default":null}]}';
381+
382+
$expectedSchema = '{"type":"record","name":"schema","namespace":"root.level.entity","fields":[{"name":"rootField1","type":{"type":"record","name":"embeddedSchema","fields":[{"name":"embeddedField","type":["null","string"],"default":null}]}},{"name":"rootField2","type":["null","embeddedSchema"],"default":null}]}';
383+
384+
$schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);
385+
$schemaTemplate
386+
->expects(self::once())
387+
->method('getSchemaDefinition')
388+
->willReturn($mergedSchema);
389+
390+
$schemaTemplate
391+
->expects(self::once())
392+
->method('getFilename')
393+
->willReturn('test.avsc');
394+
395+
$schemaRegistry = $this->getMockForAbstractClass(SchemaRegistryInterface::class);
396+
397+
$merger = new SchemaMerger($schemaRegistry);
398+
$merger->exportSchema($schemaTemplate, false, true, true);
399+
file_put_contents('/tmp/test_expected_schema.avsc', $expectedSchema);
400+
401+
self::assertFileExists('/tmp/test.avsc');
402+
self::assertFileEquals('/tmp/test_expected_schema.avsc', '/tmp/test.avsc');
403+
unlink('/tmp/test_expected_schema.avsc');
404+
unlink('/tmp/test.avsc');
405+
}
406+
379407
public function testExportSchemaPrimitiveWithWrongOptions()
380408
{
381409
$schemaTemplate = $this->getMockForAbstractClass(SchemaTemplateInterface::class);

0 commit comments

Comments
 (0)