Skip to content

Commit 8f350ac

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

File tree

2 files changed

+106
-1
lines changed

2 files changed

+106
-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 ($type !== 'integer' && $type !== 'number') {
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: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
namespace ApiPlatform\JsonSchema\Tests;
4+
5+
use ApiPlatform\JsonSchema\BackwardCompatibleSchemaFactory;
6+
use ApiPlatform\JsonSchema\Schema;
7+
use ApiPlatform\JsonSchema\SchemaFactoryInterface;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class BackwardCompatibleSchemaFactoryTest extends TestCase
11+
{
12+
public function testWithSingleType(): void
13+
{
14+
$schema = new Schema();
15+
$schema->setDefinitions(new \ArrayObject([
16+
'a' => new \ArrayObject([
17+
'properties' => new \ArrayObject([
18+
'foo' => new \ArrayObject(['type' => 'integer', 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1])
19+
])
20+
])
21+
]));
22+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
23+
$schemaFactory->method('buildSchema')->willReturn($schema);
24+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
25+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
26+
$schema = $schema->getDefinitions()['a'];
27+
28+
$this->assertTrue($schema['properties']['foo']['exclusiveMinimum']);
29+
$this->assertTrue($schema['properties']['foo']['exclusiveMaximum']);
30+
$this->assertEquals($schema['properties']['foo']['minimum'], 0);
31+
$this->assertEquals($schema['properties']['foo']['maximum'], 1);
32+
}
33+
34+
public function testWithMultipleType(): void
35+
{
36+
$schema = new Schema();
37+
$schema->setDefinitions(new \ArrayObject([
38+
'a' => new \ArrayObject([
39+
'properties' => new \ArrayObject([
40+
'foo' => new \ArrayObject(['type' => ['number', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1])
41+
])
42+
])
43+
]));
44+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
45+
$schemaFactory->method('buildSchema')->willReturn($schema);
46+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
47+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
48+
$schema = $schema->getDefinitions()['a'];
49+
50+
$this->assertTrue($schema['properties']['foo']['exclusiveMinimum']);
51+
$this->assertTrue($schema['properties']['foo']['exclusiveMaximum']);
52+
$this->assertEquals($schema['properties']['foo']['minimum'], 0);
53+
$this->assertEquals($schema['properties']['foo']['maximum'], 1);
54+
}
55+
56+
public function testWithoutNumber(): void
57+
{
58+
$schema = new Schema();
59+
$schema->setDefinitions(new \ArrayObject([
60+
'a' => new \ArrayObject([
61+
'properties' => new \ArrayObject([
62+
'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1])
63+
])
64+
])
65+
]));
66+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
67+
$schemaFactory->method('buildSchema')->willReturn($schema);
68+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
69+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => true]);
70+
$schema = $schema->getDefinitions()['a'];
71+
72+
$this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0);
73+
$this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1);
74+
}
75+
76+
public function testWithoutFlag(): void
77+
{
78+
$schema = new Schema();
79+
$schema->setDefinitions(new \ArrayObject([
80+
'a' => new \ArrayObject([
81+
'properties' => new \ArrayObject([
82+
'foo' => new \ArrayObject(['type' => ['string', 'null'], 'exclusiveMinimum' => 0, 'exclusiveMaximum' => 1])
83+
])
84+
])
85+
]));
86+
$schemaFactory = $this->createMock(SchemaFactoryInterface::class);
87+
$schemaFactory->method('buildSchema')->willReturn($schema);
88+
$schemaFactory = new BackwardCompatibleSchemaFactory($schemaFactory);
89+
$schema = $schemaFactory->buildSchema('a', serializerContext: [BackwardCompatibleSchemaFactory::SCHEMA_DRAFT4_VERSION => false]);
90+
$schema = $schema->getDefinitions()['a'];
91+
92+
$this->assertEquals($schema['properties']['foo']['exclusiveMinimum'], 0);
93+
$this->assertEquals($schema['properties']['foo']['exclusiveMaximum'], 1);
94+
}
95+
}

0 commit comments

Comments
 (0)