From dfb3bbdf29ede212971c0321ba77d613573099fe Mon Sep 17 00:00:00 2001 From: John Charman Date: Mon, 18 Nov 2024 19:39:02 +0000 Subject: [PATCH] Validate properties have keys --- src/Exception/InvalidOpenAPI.php | 12 +++++++ src/ValueObject/Valid/V30/Schema.php | 21 +++++++---- tests/ValueObject/Valid/V30/SchemaTest.php | 42 +++++++++++++--------- 3 files changed, 52 insertions(+), 23 deletions(-) diff --git a/src/Exception/InvalidOpenAPI.php b/src/Exception/InvalidOpenAPI.php index 9a51323..4283ab9 100644 --- a/src/Exception/InvalidOpenAPI.php +++ b/src/Exception/InvalidOpenAPI.php @@ -409,4 +409,16 @@ public static function mustSpecifyItemsForArrayType( return new self($message); } + + public static function mustHaveStringKeys( + Identifier $identifier, + string $keyword, + ): self { + $message = << $subSchemas - * @return array + * @param array $properties + * @return array */ - private function validateProperties(?array $subSchemas): array + private function validateProperties(?array $properties): array { - $subSchemas ??= []; + $properties ??= []; $result = []; - foreach ($subSchemas as $index => $subSchema) { - $result[] = new Schema( - $this->getIdentifier()->append("properties($index)"), + foreach ($properties as $key => $subSchema) { + if (!is_string($key)) { + throw InvalidOpenAPI::mustHaveStringKeys( + $this->getIdentifier(), + 'properties', + ); + } + + $result[$key] = new Schema( + $this->getIdentifier()->append("properties($key)"), $subSchema ); } diff --git a/tests/ValueObject/Valid/V30/SchemaTest.php b/tests/ValueObject/Valid/V30/SchemaTest.php index b097732..b3184d9 100644 --- a/tests/ValueObject/Valid/V30/SchemaTest.php +++ b/tests/ValueObject/Valid/V30/SchemaTest.php @@ -33,8 +33,9 @@ #[UsesClass(Warnings::class)] class SchemaTest extends TestCase { - #[Test, DataProvider('provideInvalidComplexSchemas')] - public function itInvalidatesEmptyComplexSchemas( + #[Test] + #[DataProvider('provideInvalidSchemas')] + public function itThrowsOnInvalidSchemas( InvalidOpenAPI $expected, Identifier $identifier, Partial\Schema $partialSchema, @@ -44,19 +45,6 @@ public function itInvalidatesEmptyComplexSchemas( new Schema($identifier, $partialSchema); } - #[Test] - public function itInvalidatesInvalidTypes(): void - { - $identifier = new Identifier(''); - $schema = PartialHelper::createSchema(type: 'invalid'); - - - - self::expectExceptionObject(InvalidOpenAPI::invalidType($identifier, 'invalid')); - - new Schema($identifier, $schema); - } - /** @param Type[] $typesItCanBe */ #[Test, DataProvider('provideSchemasToCheckTypes')] public function itKnowsIfItCanBeACertainType( @@ -152,7 +140,29 @@ public function itGetsTypes(array $expected, Partial\Schema $schema): void self::assertEqualsCanonicalizing($expected, $sut->getTypes()); } - public static function provideInvalidComplexSchemas(): Generator + public static function provideInvalidSchemas(): Generator + { + foreach (self::provideInvalidComplexSchemas() as $case => $dataset) { + yield $case => $dataset; + } + + yield 'invalid type' => [ + InvalidOpenAPI::invalidType(new Identifier('invalid type'), 'invalid'), + new Identifier('invalid type'), + new Partial\Schema(type: 'invalid'), + ]; + + yield 'properties list' => [ + InvalidOpenAPI::mustHaveStringKeys( + new Identifier('properties list'), + 'properties', + ), + new Identifier('properties list'), + new Partial\Schema(properties: [new Partial\Schema()]), + ]; + } + + private static function provideInvalidComplexSchemas(): Generator { $xOfs = [ 'allOf' => fn(Partial\Schema ...$subSchemas) => PartialHelper::createSchema(