Skip to content

Commit 83b67e5

Browse files
committed
fix(json-schema): multiple type support (draft4)
1 parent 09aacf9 commit 83b67e5

File tree

2 files changed

+117
-1
lines changed

2 files changed

+117
-1
lines changed

src/JsonSchema/BackwardCompatibleSchemaFactory.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ public function buildSchema(string $className, string $format = 'json', string $
4343

4444
foreach ($schema->getDefinitions() as $definition) {
4545
foreach ($definition['properties'] ?? [] as $property) {
46-
if (isset($property['type']) && \in_array($property['type'], ['integer', 'number'], true)) {
46+
if (!isset($property['type'])) {
47+
continue;
48+
}
49+
50+
foreach ((array) $property['type'] as $type) {
51+
if ('integer' !== $type && 'number' !== $type) {
52+
continue;
53+
}
54+
4755
if (isset($property['exclusiveMinimum'])) {
4856
$property['minimum'] = $property['exclusiveMinimum'];
4957
$property['exclusiveMinimum'] = true;
@@ -52,6 +60,8 @@ public function buildSchema(string $className, string $format = 'json', string $
5260
$property['maximum'] = $property['exclusiveMaximum'];
5361
$property['exclusiveMaximum'] = true;
5462
}
63+
64+
break;
5565
}
5666
}
5767
}
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\JsonSchema\Tests;
15+
16+
use ApiPlatform\JsonSchema\BackwardCompatibleSchemaFactory;
17+
use ApiPlatform\JsonSchema\Schema;
18+
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
19+
use PHPUnit\Framework\TestCase;
20+
21+
class BackwardCompatibleSchemaFactoryTest extends TestCase
22+
{
23+
public function testWithSingleType(): void
24+
{
25+
$schema = new Schema();
26+
$schema->setDefinitions(new \ArrayObject([
27+
'a' => new \ArrayObject([
28+
'properties' => new \ArrayObject([
29+
'foo' => new \ArrayObject(['type' => 'integer', 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
30+
]),
31+
]),
32+
]));
33+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
34+
$schemaFactory->method('buildSchema')->willReturn($schema);
35+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
36+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
37+
$schema = $schema->getDefinitions()['a'];
38+
39+
$this->assertTrue($schema['properties']['foo']['exclusiveMinimum']);
40+
$this->assertTrue($schema['properties']['foo']['exclusiveMaximum']);
41+
$this->assertEquals($schema['properties']['foo']['minimum'], 0);
42+
$this->assertEquals($schema['properties']['foo']['maximum'], 1);
43+
}
44+
45+
public function testWithMultipleType(): void
46+
{
47+
$schema = new Schema();
48+
$schema->setDefinitions(new \ArrayObject([
49+
'a' => new \ArrayObject([
50+
'properties' => new \ArrayObject([
51+
'foo' => new \ArrayObject(['type' => ['number', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
52+
]),
53+
]),
54+
]));
55+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
56+
$schemaFactory->method('buildSchema')->willReturn($schema);
57+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
58+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
59+
$schema = $schema->getDefinitions()['a'];
60+
61+
$this->assertTrue($schema['properties']['foo']['exclusiveMinimum']);
62+
$this->assertTrue($schema['properties']['foo']['exclusiveMaximum']);
63+
$this->assertEquals($schema['properties']['foo']['minimum'], 0);
64+
$this->assertEquals($schema['properties']['foo']['maximum'], 1);
65+
}
66+
67+
public function testWithoutNumber(): void
68+
{
69+
$schema = new Schema();
70+
$schema->setDefinitions(new \ArrayObject([
71+
'a' => new \ArrayObject([
72+
'properties' => new \ArrayObject([
73+
'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
74+
]),
75+
]),
76+
]));
77+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
78+
$schemaFactory->method('buildSchema')->willReturn($schema);
79+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
80+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
81+
$schema = $schema->getDefinitions()['a'];
82+
83+
$this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0);
84+
$this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1);
85+
}
86+
87+
public function testWithoutFlag(): void
88+
{
89+
$schema = new Schema();
90+
$schema->setDefinitions(new \ArrayObject([
91+
'a' => new \ArrayObject([
92+
'properties' => new \ArrayObject([
93+
'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1]),
94+
]),
95+
]),
96+
]));
97+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
98+
$schemaFactory->method('buildSchema')->willReturn($schema);
99+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
100+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => false]);
101+
$schema = $schema->getDefinitions()['a'];
102+
103+
$this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0);
104+
$this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1);
105+
}
106+
}

0 commit comments

Comments
 (0)