Skip to content

Commit 87c5a7c

Browse files
committed
Added support OPENAPI version 3.1
1 parent 157123c commit 87c5a7c

2 files changed

Lines changed: 16 additions & 8 deletions

File tree

src/TypeParser.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class TypeParser
2929
const MODE_JSON_SCHEMA = 1;
3030
const MODE_OPEN_API = 2;
3131
const MODE_REF_SCHEMA = 4;
32+
const MODE_OPEN_API_31 = 8;
3233

3334
protected $mode = 0;
3435
protected $builtinTypes = [];
@@ -148,9 +149,7 @@ protected function parseInputField($definition)
148149
'in' => $fetcher,
149150
'schema' => $schema,
150151
];
151-
if ($required) {
152-
$result['required'] = $required;
153-
}
152+
$result['required'] = $required;
154153
return $result;
155154
}
156155

@@ -234,9 +233,14 @@ protected function parseEnum(string $definition)
234233
}
235234
}
236235

236+
protected function isVersion31()
237+
{
238+
return $this->mode & self::MODE_OPEN_API_31;
239+
}
240+
237241
protected function makeNullableSchema(array $schema, $nullable)
238242
{
239-
if ($this->mode & self::MODE_OPEN_API) {
243+
if (($this->mode & self::MODE_OPEN_API) && ! $this->isVersion31()) {
240244
// OpenAPI specficition does not support this, just ingore the nullable setting.
241245
return $schema;
242246
}
@@ -246,7 +250,7 @@ protected function makeNullableSchema(array $schema, $nullable)
246250
}
247251

248252
return [
249-
'anyOf' => [
253+
'oneOf' => [
250254
['type' => 'null'],
251255
$schema,
252256
],
@@ -266,6 +270,8 @@ protected function parseScalar($definition)
266270

267271
if (($this->mode & self::MODE_JSON_SCHEMA) && $nullable) {
268272
$schema['type'] = [$schema['type'], 'null'];
273+
} elseif (($this->mode & self::MODE_OPEN_API) && $this->isVersion31() && $nullable) {
274+
$schema['type'] = [$schema['type'], 'null'];
269275
} elseif (($this->mode & self::MODE_OPEN_API) && $nullable) {
270276
$schema['nullable'] = $nullable;
271277
}

tests/TypeTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ public function typeToArrayCases()
378378

379379
],
380380
'related2' => [
381-
'anyOf' => [
381+
'oneOf' => [
382382
[
383383
'type' => 'null',
384384
],
@@ -586,11 +586,13 @@ public function typeToArrayCases()
586586
public function testTypeToArray($type, $expect1, $expect2)
587587
{
588588
$parser = new TypeParser(TypeParser::MODE_JSON_SCHEMA);
589-
var_dump($parser->parse($type));
590589
$this->assertEquals($expect1, $parser->parse($type));
591590

592591
$parser = new TypeParser(TypeParser::MODE_OPEN_API);
593592
$this->assertEquals($expect2 ?? $expect1, $parser->parse($type));
593+
594+
$parser = new TypeParser(TypeParser::MODE_OPEN_API | TypeParser::MODE_OPEN_API_31);
595+
$this->assertEquals($expect1, $parser->parse($type));
594596
}
595597

596598
public function typeToArrayWithRefCases()
@@ -797,7 +799,7 @@ public function dataCases()
797799
'type' => 'object',
798800
'properties' => [
799801
'foo' => [
800-
'anyOf' => [
802+
'oneOf' => [
801803
['type' => 'null'],
802804
],
803805
],

0 commit comments

Comments
 (0)