From 1d38e9c2686772baefdb00794babd1a3b682f30f Mon Sep 17 00:00:00 2001 From: Romain Allanot <80783376+romainallanot@users.noreply.github.com> Date: Thu, 5 Oct 2023 17:07:09 +0200 Subject: [PATCH] fix(jsonschema): field with unknown_type (#5869) --- src/JsonSchema/SchemaFactory.php | 3 ++- .../Entity/Issue5793/BagOfTests.php | 19 ++++++++++++++++++- .../Command/JsonSchemaGenerateCommandTest.php | 5 +++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index 5ed381165cb..179a7cb11c7 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -175,7 +175,8 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str // or if property schema is already fully defined (type=string + format || enum) $propertySchemaType = $propertySchema['type'] ?? false; - $isUnknown = 'array' === $propertySchemaType && Schema::UNKNOWN_TYPE === ($propertySchema['items']['type'] ?? null); + $isUnknown = Schema::UNKNOWN_TYPE === $propertySchemaType + || ('array' === $propertySchemaType && Schema::UNKNOWN_TYPE === ($propertySchema['items']['type'] ?? null)); if ( !$isUnknown && ( diff --git a/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php b/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php index b84f18cfca2..17283e34de7 100644 --- a/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php +++ b/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php @@ -42,13 +42,18 @@ class BagOfTests #[ORM\OneToMany(mappedBy: 'bagOfTests', targetEntity: TestEntity::class)] #[Groups(['read', 'write'])] - #[ApiProperty(schema: ['type' => 'string'], jsonSchemaContext: ['foo' => 'bar'])] + #[ApiProperty(jsonSchemaContext: ['foo' => 'bar'], schema: ['type' => 'string'])] private Collection $tests; #[ORM\OneToMany(mappedBy: 'bagOfTests', targetEntity: NonResourceTestEntity::class)] #[Groups(['read', 'write'])] private Collection $nonResourceTests; + #[ORM\ManyToOne(targetEntity: TestEntity::class)] + #[ORM\JoinColumn(name: 'type', referencedColumnName: 'id', nullable: false)] + #[Groups(['read', 'write'])] + protected ?TestEntity $type = null; + public function __construct() { $this->tests = new ArrayCollection(); @@ -128,4 +133,16 @@ public function removeNonResourceTest(NonResourceTestEntity $nonResourceTest): s return $this; } + + public function getType(): ?TestEntity + { + return $this->type; + } + + public function setType(?TestEntity $type): self + { + $this->type = $type; + + return $this; + } } diff --git a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php index f810e44a6ad..1c338717d5c 100644 --- a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php +++ b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php @@ -118,5 +118,10 @@ public function testArraySchemaWithReference(): void $this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['description'], [ 'maxLength' => 255, ]); + + $this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['type'], [ + 'owl:maxCardinality' => 1, + '$ref' => '#/definitions/TestEntity.jsonld-write', + ]); } }