Skip to content

Commit f9e1ed3

Browse files
authored
feat(MPM-703): add reserved keywords checkup for names (#48)
1 parent 6420f7e commit f9e1ed3

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Command/CheckAllSchemaTemplatesNamesCommand.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ class CheckAllSchemaTemplatesNamesCommand extends Command
1919
'fixed'
2020
];
2121

22+
private const RESERVED_KEYWORDS = [
23+
'null',
24+
'boolean',
25+
'int',
26+
'long',
27+
'float',
28+
'double',
29+
'bytes',
30+
'string',
31+
'record',
32+
'enum',
33+
'array',
34+
'map',
35+
'fixed',
36+
];
37+
2238
private const REGEX_MATCH_NAME_NAMING_CONVENTION = '/^[A-Za-z_][A-Za-z0-9_]*$/';
2339

2440
private const REGEX_MATCH_NAMESPACE_NAMING_CONVENTION =
@@ -113,7 +129,10 @@ private function validateNameField(string $name, string $schemaName): array
113129
{
114130
$failed = [];
115131

116-
if (!preg_match(self::REGEX_MATCH_NAME_NAMING_CONVENTION, $name)) {
132+
if (
133+
!preg_match(self::REGEX_MATCH_NAME_NAMING_CONVENTION, $name) ||
134+
in_array($name, self::RESERVED_KEYWORDS)
135+
) {
117136
$failed[] = $schemaName;
118137
}
119138

tests/Command/CheckAllSchemaTemplatesNamesCommandTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ class CheckAllSchemaTemplatesNamesCommandTest extends AbstractSchemaRegistryTest
176176
}
177177
EOF;
178178

179+
protected const BAD_SCHEMA4 = <<<EOF
180+
{
181+
"type": "record",
182+
"name": "null",
183+
"namespace": "ch.jobcloud",
184+
"doc": "This is a sample Avro schema to get you started. Please edit",
185+
"fields": [
186+
{
187+
"name": "name",
188+
"type": "string",
189+
"doc": "some desc"
190+
}
191+
]
192+
}
193+
EOF;
194+
179195
/**
180196
* This method is called before each test.
181197
*/
@@ -354,4 +370,30 @@ public function testOutputWhenNamespaceStartsWithDot(): void
354370
self::assertStringContainsString('* test.schema.bad3', $commandOutput);
355371
self::assertEquals(1, $commandTester->getStatusCode());
356372
}
373+
374+
public function testOutputWhenNameIsReservedKeyword(): void
375+
{
376+
file_put_contents(
377+
sprintf('%s/test.schema.bad4.avsc', self::SCHEMA_DIRECTORY),
378+
self::BAD_SCHEMA4
379+
);
380+
381+
$application = new Application();
382+
$application->add(new CheckAllSchemaTemplatesNamesCommand());
383+
$command = $application->find('kafka-schema-registry:check:template:names:all');
384+
$commandTester = new CommandTester($command);
385+
386+
$commandTester->execute([
387+
'schemaTemplateDirectory' => self::SCHEMA_DIRECTORY
388+
]);
389+
390+
$commandOutput = trim($commandTester->getDisplay());
391+
392+
self::assertStringContainsString(
393+
'A template schema names must comply with the following AVRO naming',
394+
$commandOutput
395+
);
396+
self::assertStringContainsString('* test.schema.bad4', $commandOutput);
397+
self::assertEquals(1, $commandTester->getStatusCode());
398+
}
357399
}

0 commit comments

Comments
 (0)