Skip to content
Closed
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
114 changes: 114 additions & 0 deletions src/Type/TypeInfoTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,121 @@ protected function doAugment(Analysis $analysis, OA\Schema $schema, \Reflector $
}
}

<<<<<<< HEAD
/**
=======
protected function setSchemaType(OA\Schema $schema, Type $type, Analysis $analysis, string $sourceClass = OA\Schema::class): OA\Schema
{
if ($type instanceof CompositeTypeInterface) {
$types = $type->getTypes();

$isNonZeroInt = 2 === count($types) && $types[0] instanceof IntRangeType && $types[1] instanceof IntRangeType;

if ($isNonZeroInt) {
$schema->type = 'int';
$schema->not = $schema->_context->isVersion('3.0.x')
? ['enum' => [0]]
: ['const' => 0];
} else {
$allBuiltin = array_reduce($types, static fn ($carry, $t): bool => $carry && $t instanceof BuiltinType, true);

if ($type instanceof UnionType) {
if ($allBuiltin) {
$schema->type = array_map(static fn (Type $t): string => (string) $t, $types);
} else {
$builtinTypes = array_filter($types, static fn (Type $t): bool => $t instanceof BuiltinType);
$otherTypes = array_filter($types, static fn (Type $t): bool => !$t instanceof BuiltinType);

if ($schema->items instanceof OA\Items) {
// nothing more we can do here
return $schema;
}

$schema->type = Generator::UNDEFINED;
$schema->oneOf = [];

if ($builtinTypes) {
$schema->oneOf[] = $builtinSchema = new OA\Schema([
'type' => array_values(array_map(static fn (Type $t): string => (string) $t, $builtinTypes)),
'_context' => new Context(['generated' => true], $schema->_context),
]);
$this->type2ref($builtinSchema, $analysis);
$analysis->addAnnotation($builtinSchema, $builtinSchema->_context);
}

foreach ($otherTypes as $otherType) {
$otherSchema = new OA\Schema([
'_context' => new Context(['generated' => true], $schema->_context),
]);
$schema->oneOf[] = $this->setSchemaType($otherSchema, $otherType, $analysis);
$this->type2ref($otherSchema, $analysis);
$analysis->addAnnotation($otherSchema, $otherSchema->_context);
}
}
} elseif ($type instanceof IntersectionType) {
$schema->type = Generator::UNDEFINED;
$schema->allOf = [];

foreach ($types as $intersectionType) {
$intersectionSchema = new OA\Schema([
'_context' => new Context(['generated' => true], $schema->_context),
]);
$schema->allOf[] = $this->setSchemaType($intersectionSchema, $intersectionType, $analysis);
$this->type2ref($intersectionSchema, $analysis);
$analysis->addAnnotation($intersectionSchema, $intersectionSchema->_context);
}
}
}
} else {
if ($type instanceof BuiltinType || $type instanceof ObjectType) {
$schema->type = (string) $type;
} elseif ($type instanceof IntRangeType) {
$schema->type = $type->getTypeIdentifier()->value;

$schema->minimum = $type->getFrom();
$schema->maximum = $type->getTo();
} elseif ($type instanceof ExplicitType) {
$schema->type = $type->getTypeIdentifier()->value;
} elseif ($type instanceof CollectionType) {
if ($type->isList() || $type->getCollectionKeyType() instanceof UnionType) {
// list<T>, array<T>, T[] → ordered list
$schema->type = 'array';

if (Generator::isDefault($schema->items)) {
$schema->items = new OA\Items(['_context' => new Context(['generated' => true], $schema->_context)]);
$this->setSchemaType($schema->items, $type->getCollectionValueType(), $analysis);
$this->type2ref($schema->items, $analysis);
$analysis->addAnnotation($schema->items, $schema->items->_context);
} elseif (Generator::isDefault($schema->items->type, $schema->items->oneOf, $schema->items->allOf, $schema->items->anyOf)) {
$this->setSchemaType($schema->items, $type->getCollectionValueType(), $analysis);
$this->type2ref($schema->items, $analysis);
}

$this->mapNativeType($schema->items, $schema->items->type);
} else {
// explicit key type (e.g. array<string, string>) → map
$schema->type = 'object';

if (Generator::isDefault($schema->additionalProperties)) {
$schema->additionalProperties = new OA\AdditionalProperties(['_context' => new Context(['generated' => true], $schema->_context)]);
$this->setSchemaType($schema->additionalProperties, $type->getCollectionValueType(), $analysis);
$this->type2ref($schema->additionalProperties, $analysis);
$analysis->addAnnotation($schema->additionalProperties, $schema->additionalProperties->_context);
} elseif (Generator::isDefault($schema->additionalProperties->type, $schema->additionalProperties->oneOf, $schema->additionalProperties->allOf, $schema->additionalProperties->anyOf)) {
$this->setSchemaType($schema->additionalProperties, $type->getCollectionValueType(), $analysis);
$this->type2ref($schema->additionalProperties, $analysis);
}

$this->mapNativeType($schema->additionalProperties, $schema->additionalProperties->type);
}
}
}

return $schema;
}

/**645 1050272 02 1268 0026220 00
>>>>>>> f66289a (Map `array<K, V>` to `type: object` + `additionalProperties` in TypeInfoTypeResolver (#2001) (#2003))
* @param \ReflectionParameter|\ReflectionProperty|\ReflectionMethod $reflector
*/
protected function getReflectionType(\Reflector $reflector): ?Type
Expand Down
12 changes: 12 additions & 0 deletions tests/Fixtures/PHP/DocblockAndTypehintTypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,18 @@ class DocblockAndTypehintTypes
#[OAT\Property]
public array $arrayShape;

/**
* @var array<string, string>
*/
#[OAT\Property]
public array $stringMap;

/**
* @var array<int, string>
*/
#[OAT\Property]
public array $intKeyedMap;

/**
* @var int|string
*/
Expand Down
8 changes: 8 additions & 0 deletions tests/Fixtures/TypedProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,12 @@ class TypedProperties
* @OA\Property()
*/
public array $nativeArray;

/**
* A map of string to string.
*
* @var array<string, string>
*/
#[OAT\Property]
public array $stringMap;
}
12 changes: 12 additions & 0 deletions tests/Processors/AugmentPropertiesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public function testTypedProperties(): void
$staticString,
$staticNullableString,
$nativeArray,
$stringMap,
] = $analysis->openapi->components->schemas[0]->properties;

$this->assertName($stringType, [
Expand Down Expand Up @@ -258,6 +259,10 @@ public function testTypedProperties(): void
'property' => Generator::UNDEFINED,
'type' => Generator::UNDEFINED,
]);
$this->assertName($stringMap, [
'property' => Generator::UNDEFINED,
'type' => Generator::UNDEFINED,
]);

$analysis->process($this->initializeProcessors([new AugmentProperties()]));

Expand Down Expand Up @@ -351,6 +356,13 @@ public function testTypedProperties(): void
'string',
$nativeArray->items->type
);
$this->assertName($stringMap, [
'property' => 'stringMap',
'type' => 'object',
]);
$this->assertFalse(Generator::isDefault($stringMap->additionalProperties));
$this->assertSame('string', $stringMap->additionalProperties->type);
$this->assertTrue(Generator::isDefault($stringMap->items));
}

protected function assertName(OA\Property $property, array $expectedValues): void
Expand Down
Loading