Skip to content

fix(json-schema): multiple type support (draft4) #6171

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion src/JsonSchema/BackwardCompatibleSchemaFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,15 @@ public function buildSchema(string $className, string $format = 'json', string $

foreach ($schema->getDefinitions() as $definition) {
foreach ($definition['properties'] ?? [] as $property) {
if (isset($property['type']) && \in_array($property['type'], ['integer', 'number'], true)) {
if (!isset($property['type'])) {
continue;
}

foreach ((array) $property['type'] as $type) {
if ('integer' !== $type && 'number' !== $type) {
continue;
}

if (isset($property['exclusiveMinimum'])) {
$property['minimum'] = $property['exclusiveMinimum'];
$property['exclusiveMinimum'] = true;
Expand All @@ -52,6 +60,8 @@ public function buildSchema(string $className, string $format = 'json', string $
$property['maximum'] = $property['exclusiveMaximum'];
$property['exclusiveMaximum'] = true;
}

break;
}
}
}
Expand Down
106 changes: 106 additions & 0 deletions src/JsonSchema/Tests/BackwardCompatibleSchemaFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?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\Tests;

use ApiPlatform\JsonSchema\BackwardCompatibleSchemaFactory;
use ApiPlatform\JsonSchema\Schema;
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
use PHPUnit\Framework\TestCase;

class BackwardCompatibleSchemaFactoryTest extends TestCase
{
public function testWithSingleType(): void
{
$schema = new Schema();
$schema->setDefinitions(new \ArrayObject([
'a' => new \ArrayObject([
'properties' => new \ArrayObject([
'foo' => new \ArrayObject(['type' => 'integer', 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
]),
]),
]));
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
$schemaFactory->method('buildSchema')->willReturn($schema);
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
$schema = $schema->getDefinitions()['a'];

$this->assertTrue($schema['properties']['foo']['exclusiveMinimum']);
$this->assertTrue($schema['properties']['foo']['exclusiveMaximum']);
$this->assertEquals($schema['properties']['foo']['minimum'], 0);
$this->assertEquals($schema['properties']['foo']['maximum'], 1);
}

public function testWithMultipleType(): void
{
$schema = new Schema();
$schema->setDefinitions(new \ArrayObject([
'a' => new \ArrayObject([
'properties' => new \ArrayObject([
'foo' => new \ArrayObject(['type' => ['number', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
]),
]),
]));
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
$schemaFactory->method('buildSchema')->willReturn($schema);
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
$schema = $schema->getDefinitions()['a'];

$this->assertTrue($schema['properties']['foo']['exclusiveMinimum']);
$this->assertTrue($schema['properties']['foo']['exclusiveMaximum']);
$this->assertEquals($schema['properties']['foo']['minimum'], 0);
$this->assertEquals($schema['properties']['foo']['maximum'], 1);
}

public function testWithoutNumber(): void
{
$schema = new Schema();
$schema->setDefinitions(new \ArrayObject([
'a' => new \ArrayObject([
'properties' => new \ArrayObject([
'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
]),
]),
]));
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
$schemaFactory->method('buildSchema')->willReturn($schema);
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
$schema = $schema->getDefinitions()['a'];

$this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0);
$this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1);
}

public function testWithoutFlag(): void
{
$schema = new Schema();
$schema->setDefinitions(new \ArrayObject([
'a' => new \ArrayObject([
'properties' => new \ArrayObject([
'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
]),
]),
]));
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
$schemaFactory->method('buildSchema')->willReturn($schema);
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => false]);
$schema = $schema->getDefinitions()['a'];

$this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0);
$this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1);
}
}