Skip to content

Commit

Permalink
Merge d57eab4 into e32a4c7
Browse files Browse the repository at this point in the history
  • Loading branch information
monitaurus committed Jun 15, 2024
2 parents e32a4c7 + d57eab4 commit 2075f0d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,10 @@ public function create(string $resourceClass, string $property, array $options =
$propertySchema['example'] = $propertySchema['default'];
}

// never override the following keys if at least one is already set
// never override the following keys if at least one is already set or if there's a custom openapi context
if ([] === $types
|| ($propertySchema['type'] ?? $propertySchema['$ref'] ?? $propertySchema['anyOf'] ?? $propertySchema['allOf'] ?? $propertySchema['oneOf'] ?? false)
|| ($propertyMetadata->getOpenapiContext() ?? false)
) {
return $propertyMetadata->withSchema($propertySchema);
}
Expand Down
33 changes: 33 additions & 0 deletions src/JsonSchema/Tests/Fixtures/DummyWithCustomOpenApiContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\JsonSchema\Tests\Fixtures;

use ApiPlatform\Metadata\ApiProperty;
use ApiPlatform\Metadata\ApiResource;

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

#[ApiResource]
class DummyWithCustomOpenApiContext
{
#[ApiProperty(openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]])]
public $acme;
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\JsonSchema\Tests\Metadata\Property\Factory;

use ApiPlatform\JsonSchema\Metadata\Property\Factory\SchemaPropertyMetadataFactory;
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithCustomOpenApiContext;
use ApiPlatform\JsonSchema\Tests\Fixtures\DummyWithEnum;
use ApiPlatform\JsonSchema\Tests\Fixtures\Enum\IntEnumAsIdentifier;
use ApiPlatform\Metadata\ApiProperty;
Expand All @@ -34,4 +35,18 @@ public function testEnum(): void
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithEnum::class, 'intEnumAsIdentifier');
$this->assertEquals(['type' => ['integer', 'null'], 'enum' => [1, 2, null]], $apiProperty->getSchema());
}

public function testWithCustomOpenApiContext(): void
{
$resourceClassResolver = $this->createMock(ResourceClassResolverInterface::class);
$apiProperty = new ApiProperty(
builtinTypes: [new Type(builtinType: 'object', nullable: true, class: IntEnumAsIdentifier::class)],
openapiContext: ['type' => 'object', 'properties' => ['alpha' => ['type' => 'integer']]],
);
$decorated = $this->createMock(PropertyMetadataFactoryInterface::class);
$decorated->expects($this->once())->method('create')->with(DummyWithCustomOpenApiContext::class, 'acme')->willReturn($apiProperty);
$schemaPropertyMetadataFactory = new SchemaPropertyMetadataFactory($resourceClassResolver, $decorated);
$apiProperty = $schemaPropertyMetadataFactory->create(DummyWithCustomOpenApiContext::class, 'acme');
$this->assertEquals([], $apiProperty->getSchema());
}
}

0 comments on commit 2075f0d

Please sign in to comment.