-
-
Notifications
You must be signed in to change notification settings - Fork 894
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(jsonschema): keep integer and number properties draft 4 compliant (…
- Loading branch information
Showing
5 changed files
with
177 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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; | ||
|
||
use ApiPlatform\Metadata\Operation; | ||
|
||
/** | ||
* This factory decorates range integer and number properties to keep Draft 4 backward compatibility. | ||
* | ||
* @see https://github.com/api-platform/core/issues/6041 | ||
* | ||
* @internal | ||
*/ | ||
final class BackwardCompatibleSchemaFactory implements SchemaFactoryInterface, SchemaFactoryAwareInterface | ||
{ | ||
public const SCHEMA_DRAFT4_VERSION = 'draft_4'; | ||
|
||
public function __construct(private readonly SchemaFactoryInterface $decorated) | ||
{ | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
public function buildSchema(string $className, string $format = 'json', string $type = Schema::TYPE_OUTPUT, Operation $operation = null, Schema $schema = null, array $serializerContext = null, bool $forceCollection = false): Schema | ||
{ | ||
$schema = $this->decorated->buildSchema($className, $format, $type, $operation, $schema, $serializerContext, $forceCollection); | ||
|
||
if (!($serializerContext[self::SCHEMA_DRAFT4_VERSION] ?? false)) { | ||
return $schema; | ||
} | ||
|
||
foreach ($schema->getDefinitions() as $definition) { | ||
foreach ($definition['properties'] ?? [] as $property) { | ||
if (isset($property['type']) && \in_array($property['type'], ['integer', 'number'], true)) { | ||
if (isset($property['exclusiveMinimum'])) { | ||
$property['minimum'] = $property['exclusiveMinimum']; | ||
$property['exclusiveMinimum'] = true; | ||
} | ||
if (isset($property['exclusiveMaximum'])) { | ||
$property['maximum'] = $property['exclusiveMaximum']; | ||
$property['exclusiveMaximum'] = true; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return $schema; | ||
} | ||
|
||
public function setSchemaFactory(SchemaFactoryInterface $schemaFactory): void | ||
{ | ||
if ($this->decorated instanceof SchemaFactoryAwareInterface) { | ||
$this->decorated->setSchemaFactory($schemaFactory); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
tests/Fixtures/TestBundle/Entity/Issue6041/NumericValidated.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the API Platform project. | ||
* | ||
* (c) Kévin Dunglas <[email protected]> | ||
* | ||
* 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\Tests\Fixtures\TestBundle\Entity\Issue6041; | ||
|
||
use ApiPlatform\Metadata\ApiResource; | ||
use ApiPlatform\Metadata\Get; | ||
use ApiPlatform\Metadata\GetCollection; | ||
use Doctrine\ORM\Mapping as ORM; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[ApiResource(operations: [ | ||
new Get(uriTemplate: 'numeric-validated/{id}'), | ||
new GetCollection(uriTemplate: 'numeric-validated'), | ||
])] | ||
#[ORM\Entity] | ||
class NumericValidated | ||
{ | ||
#[ORM\Column(type: 'integer')] | ||
#[ORM\Id] | ||
#[ORM\GeneratedValue(strategy: 'AUTO')] | ||
private ?int $id = null; | ||
|
||
#[Assert\Range(min: 1, max: 10)] | ||
#[ORM\Column] | ||
public int $range; | ||
|
||
#[Assert\GreaterThan(value: 10)] | ||
#[ORM\Column] | ||
public int $greaterThanMe; | ||
|
||
#[Assert\GreaterThanOrEqual(value: '10.99')] | ||
#[ORM\Column] | ||
public float $greaterThanOrEqualToMe; | ||
|
||
#[Assert\LessThan(value: 99)] | ||
#[ORM\Column] | ||
public int $lessThanMe; | ||
|
||
#[Assert\LessThanOrEqual(value: '99.33')] | ||
#[ORM\Column] | ||
public float $lessThanOrEqualToMe; | ||
|
||
#[Assert\Positive] | ||
#[ORM\Column] | ||
public int $positive; | ||
|
||
#[Assert\PositiveOrZero] | ||
#[ORM\Column] | ||
public int $positiveOrZero; | ||
|
||
#[Assert\Negative] | ||
#[ORM\Column] | ||
public int $negative; | ||
|
||
#[Assert\NegativeOrZero] | ||
#[ORM\Column] | ||
public int $negativeOrZero; | ||
|
||
public function getId(): ?int | ||
{ | ||
return $this->id; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters