Skip to content

Commit 4d0d308

Browse files
authored
Consider oneOf, allOf and anyOf when deciding on whether to modify the schema type (#1847)
1 parent 2b832aa commit 4d0d308

File tree

6 files changed

+39
-9
lines changed

6 files changed

+39
-9
lines changed

src/Generator.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,15 @@ public function __construct(?LoggerInterface $logger = null)
7575
$this->setNamespaces(self::DEFAULT_NAMESPACES);
7676
}
7777

78-
public static function isDefault($value): bool
78+
public static function isDefault(...$value): bool
7979
{
80-
return $value === Generator::UNDEFINED;
80+
foreach ($value as $v) {
81+
if ($v !== Generator::UNDEFINED) {
82+
return false;
83+
}
84+
}
85+
86+
return true;
8187
}
8288

8389
/**

src/Type/AbstractTypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ protected function augmentItems(OA\Schema $schema, Analysis $analysis): void
4141
$schema->items->ref = $schema->ref;
4242
$schema->ref = Generator::UNDEFINED;
4343
}
44-
} elseif (Generator::isDefault($schema->items->type)) {
44+
} elseif (Generator::isDefault($schema->items->type, $schema->items->oneOf, $schema->items->allOf, $schema->items->anyOf)) {
4545
$schema->items->type = $schema->type;
4646

4747
$this->type2ref($schema->items, $analysis);

src/Type/LegacyTypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected function doAugment(Analysis $analysis, OA\Schema $schema, \Reflector $
2828
$schema->nullable = true;
2929
}
3030

31-
if (Generator::isDefault($schema->type) && ($docblockDetails->explicitType || $reflectionTypeDetails->explicitType)) {
31+
if (Generator::isDefault($schema->type, $schema->oneOf, $schema->allOf, $schema->anyOf) && ($docblockDetails->explicitType || $reflectionTypeDetails->explicitType)) {
3232
$details = $docblockDetails->types ? $docblockDetails : $reflectionTypeDetails;
3333

3434
// for now

src/Type/TypeInfoTypeResolver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ protected function doAugment(Analysis $analysis, OA\Schema $schema, \Reflector $
5050
$docblockType = $docblockType instanceof NullableType ? $docblockType->getWrappedType() : $docblockType;
5151
$reflectionType = $reflectionType instanceof NullableType ? $reflectionType->getWrappedType() : $reflectionType;
5252

53-
if (Generator::isDefault($schema->type) && ($docblockType || $reflectionType)) {
53+
if (Generator::isDefault($schema->type, $schema->oneOf, $schema->allOf, $schema->anyOf) && ($docblockType || $reflectionType)) {
5454
$type = $docblockType ?? $reflectionType;
5555

5656
$isNonZeroInt = false;

tests/Fixtures/PHP/DocblockAndTypehintTypes.php

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -137,10 +137,10 @@ class DocblockAndTypehintTypes
137137
* @param bool $bool
138138
*/
139139
public function __construct(
140-
private OAT\Tag $tag,
140+
private OAT\Tag $tag,
141141
#[OAT\Property]
142-
protected string $promotedString,
143-
bool $bool = true,
142+
protected string $promotedString,
143+
bool $bool = true,
144144
#[OAT\Property(example: 'My value')]
145145
public string|array $mixedUnion = [],
146146
)
@@ -156,6 +156,28 @@ public function getString(): string
156156
return 'string';
157157
}
158158

159+
/**
160+
* @var DocblockAndTypehintTypes
161+
*/
162+
#[OAT\Property(
163+
oneOf: [
164+
new OAT\Schema(type: 'string'),
165+
new OAT\Schema(type: 'bool'),
166+
]
167+
)]
168+
public $oneOfVar;
169+
170+
/**
171+
* @var array<DocblockAndTypehintTypes>
172+
*/
173+
#[OAT\Property(
174+
items: new OAT\Items(oneOf: [
175+
new OAT\Schema(type: 'string'),
176+
new OAT\Schema(type: 'bool'),
177+
])
178+
)]
179+
public array $oneOfList;
180+
159181
/**
160182
* @param \DateTimeImmutable[] $paramDateTimeList
161183
* @param string[] $paramStringList
@@ -176,7 +198,7 @@ public function blah(
176198
#[OAT\Property(example: 'My blah')]
177199
?string $blah,
178200
#[OAT\Property(nullable: true, items: new OAT\Items(type: 'string', example: 'hello'))]
179-
?array $blah_values,
201+
?array $blah_values,
180202
) {
181203
}
182204
}

tests/Type/TypeResolverTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ public static function resolverAugmentCases(): iterable
5757
'paramstringlist' => '{ "type": "array", "items": { "type": "string" }, "property": "paramStringList" }',
5858
'blah' => '{ "type": "string", "example": "My blah", "nullable": true, "property": "blah" }',
5959
'blah_values' => '{ "type": "array", "items": { "type": "string", "example": "hello" }, "nullable": true, "property": "blah_values" }',
60+
'oneofvar' => '{ "oneOf": [ { "type": "string" }, { "type": "bool" } ], "property": "oneOfVar" }',
61+
'oneoflist' => '{ "type": "array", "items": { "oneOf": [ { "type": "string" }, { "type": "bool" } ] }, "property": "oneOfList" }',
6062
];
6163

6264
$rc = new \ReflectionClass(DocblockAndTypehintTypes::class);

0 commit comments

Comments
 (0)