From f354367344d6f2c1e19a3a9655c0b4db6343fe22 Mon Sep 17 00:00:00 2001 From: Vincent Chalamon <407859+vincentchalamon@users.noreply.github.com> Date: Wed, 4 Oct 2023 11:24:36 +0200 Subject: [PATCH] fix(jsonschema): do not override nor complete ApiProperty::schema user value --- src/JsonSchema/SchemaFactory.php | 10 ++++++++++ .../TestBundle/Entity/Issue5793/BagOfTests.php | 1 + .../Command/JsonSchemaGenerateCommandTest.php | 6 ++---- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/JsonSchema/SchemaFactory.php b/src/JsonSchema/SchemaFactory.php index 831f4330430..5ed381165cb 100644 --- a/src/JsonSchema/SchemaFactory.php +++ b/src/JsonSchema/SchemaFactory.php @@ -13,6 +13,7 @@ namespace ApiPlatform\JsonSchema; +use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory; use ApiPlatform\Metadata\ApiProperty; use ApiPlatform\Metadata\CollectionOperationInterface; use ApiPlatform\Metadata\Exception\OperationNotFoundException; @@ -158,6 +159,15 @@ private function buildPropertySchema(Schema $schema, string $definitionName, str $additionalPropertySchema ?? [] ); + $extraProperties = $propertyMetadata->getExtraProperties() ?? []; + // see AttributePropertyMetadataFactory + if (true === ($extraProperties[SchemaPropertyMetadataFactory::JSON_SCHEMA_USER_DEFINED] ?? false)) { + // schema seems to have been declared by the user: do not override nor complete user value + $schema->getDefinitions()[$definitionName]['properties'][$normalizedPropertyName] = new \ArrayObject($propertySchema); + + return; + } + $types = $propertyMetadata->getBuiltinTypes() ?? []; // never override the following keys if at least one is already set diff --git a/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php b/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php index 638efd6f657..b84f18cfca2 100644 --- a/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php +++ b/tests/Fixtures/TestBundle/Entity/Issue5793/BagOfTests.php @@ -42,6 +42,7 @@ class BagOfTests #[ORM\OneToMany(mappedBy: 'bagOfTests', targetEntity: TestEntity::class)] #[Groups(['read', 'write'])] + #[ApiProperty(schema: ['type' => 'string'], jsonSchemaContext: ['foo' => 'bar'])] private Collection $tests; #[ORM\OneToMany(mappedBy: 'bagOfTests', targetEntity: NonResourceTestEntity::class)] diff --git a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php index f2582e966c2..f810e44a6ad 100644 --- a/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php +++ b/tests/JsonSchema/Command/JsonSchemaGenerateCommandTest.php @@ -104,10 +104,8 @@ public function testArraySchemaWithReference(): void $json = json_decode($result, associative: true); $this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['tests'], [ - 'type' => 'array', - 'items' => [ - '$ref' => '#/definitions/TestEntity.jsonld-write', - ], + 'type' => 'string', + 'foo' => 'bar', ]); $this->assertEquals($json['definitions']['BagOfTests.jsonld-write']['properties']['nonResourceTests'], [