Skip to content
Merged
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
42 changes: 30 additions & 12 deletions src/Type/TypeInfoTypeResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,19 +148,37 @@ protected function setSchemaType(OA\Schema $schema, Type $type, Analysis $analys
} elseif ($type instanceof ExplicitType) {
$schema->type = $type->getTypeIdentifier()->value;
} elseif ($type instanceof CollectionType) {
$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);
}
if ($type->isList() || $type->getCollectionKeyType() instanceof UnionType) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is tricky. UnionType means int|string so could be an object, we just don't know. However, from what I understand this happens when no key is specified.
I have to admin I do not quite like this check as it is quite loose, however I do not know if there is a better alternative... hmmm.

// 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);
$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);
}
}
}

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 @@ -82,4 +82,12 @@ class TypedProperties
*/
#[OAT\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 @@ -177,6 +177,7 @@ public function testTypedProperties(): void
$staticString,
$staticNullableString,
$nativeArray,
$stringMap,
] = $analysis->openapi->components->schemas[0]->properties;

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

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

Expand Down Expand Up @@ -348,6 +353,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
18 changes: 14 additions & 4 deletions tests/Type/TypeResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public static function resolverAugmentCases(): iterable
'intrange' => '{ "type": "integer", "maximum": 10, "minimum": -9223372036854775808, "property": "intRange" }',
'positiveint' => '{ "type": "integer", "maximum": 9223372036854775807, "minimum": 1, "property": "positiveInt" }',
'nonzeroint' => '{ "type": "integer", "not": { "enum": [ 0 ] }, "property": "nonZeroInt" }',
'arrayshape' => '{ "type": "array", "items": { "type": "boolean" }, "property": "arrayShape" }',
'legacy:arrayshape' => '{ "type": "array", "items": { "type": "boolean" }, "property": "arrayShape" }',
'type-info:arrayshape' => '{ "type": "object", "additionalProperties": { "type": "boolean" }, "property": "arrayShape" }',
'legacy:stringmap' => '{ "type": "array", "items": { "type": "mixed" }, "property": "stringMap" }',
'type-info:stringmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "stringMap" }',
'legacy:intkeyedmap' => '{ "type": "array", "items": { "type": "mixed" }, "property": "intKeyedMap" }',
'type-info:intkeyedmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "intKeyedMap" }',
'uniontype' => '{ "property": "unionType" }',
'promotedstring' => '{ "type": "string", "property": "promotedString" }',
'legacy:mixedunion' => '{ "example": "My value", "property": "mixedUnion" }',
Expand All @@ -58,7 +63,7 @@ public static function resolverAugmentCases(): iterable
'legacy:nullabletypedlistunion' => '{ "nullable": true, "property": "nullableTypedListUnion" }',
'type-info:nullabletypedlistunion' => '{ "nullable": true, "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } ], "property": "nullableTypedListUnion" }',
'legacy:nullablenestedtypedlistunion' => '{ "nullable": true, "property": "nullableNestedTypedListUnion" }',
'type-info:nullablenestedtypedlistunion' => '{ "nullable": true, "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } } ], "property": "nullableNestedTypedListUnion" }',
'type-info:nullablenestedtypedlistunion' => '{ "nullable": true, "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "object", "additionalProperties": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } } ], "property": "nullableNestedTypedListUnion" }',
'reflectionvalue' => '{ "example": true, "nullable": true, "property": "reflectionValue" }',
'legacy:intersectionvar' => '{ "property": "intersectionVar" }',
'type-info:intersectionvar' => '{ "allOf": [ { "$ref": "#/components/schemas/FirstInterface" }, { "$ref": "#/components/schemas/SecondInterface" } ], "property": "intersectionVar" }',
Expand Down Expand Up @@ -87,7 +92,12 @@ public static function resolverAugmentCases(): iterable
'intrange' => '{ "type": "integer", "maximum": 10, "minimum": -9223372036854775808, "property": "intRange" }',
'positiveint' => '{ "type": "integer", "maximum": 9223372036854775807, "minimum": 1, "property": "positiveInt" }',
'nonzeroint' => '{ "type": "integer", "not": { "const": 0 }, "property": "nonZeroInt" } ',
'arrayshape' => '{ "type": "array", "items": { "type": "boolean" }, "property": "arrayShape" }',
'legacy:arrayshape' => '{ "type": "array", "items": { "type": "boolean" }, "property": "arrayShape" }',
'type-info:arrayshape' => '{ "type": "object", "additionalProperties": { "type": "boolean" }, "property": "arrayShape" }',
'legacy:stringmap' => '{ "type": "array", "items": { "type": "mixed" }, "property": "stringMap" }',
'type-info:stringmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "stringMap" }',
'legacy:intkeyedmap' => '{ "type": "array", "items": { "type": "mixed" }, "property": "intKeyedMap" }',
'type-info:intkeyedmap' => '{ "type": "object", "additionalProperties": { "type": "string" }, "property": "intKeyedMap" }',
'legacy:uniontype' => '{ "property": "unionType" }',
'type-info:uniontype' => '{ "type": [ "integer", "string" ], "property": "unionType" }',
'promotedstring' => '{ "type": "string", "property": "promotedString" }',
Expand All @@ -103,7 +113,7 @@ public static function resolverAugmentCases(): iterable
'legacy:nullabletypedlistunion' => '{ "property": "nullableTypedListUnion" }',
'type-info:nullabletypedlistunion' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "null" } ], "property": "nullableTypedListUnion" }',
'legacy:nullablenestedtypedlistunion' => '{ "property": "nullableNestedTypedListUnion" }',
'type-info:nullablenestedtypedlistunion' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } }, { "type": "null" } ], "property": "nullableNestedTypedListUnion" }',
'type-info:nullablenestedtypedlistunion' => '{ "oneOf": [ { "$ref": "#/components/schemas/DocblockAndTypehintTypes" }, { "type": "array", "items": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } }, { "type": "array", "items": { "type": "object", "additionalProperties": { "$ref": "#/components/schemas/DocblockAndTypehintTypes" } } }, { "type": "null" } ], "property": "nullableNestedTypedListUnion" }',
'legacy:reflectionvalue' => '{ "example": true, "property": "reflectionValue" }',
'type-info:reflectionvalue' => '{ "type": [ "boolean", "integer", "null" ], "example": true, "property": "reflectionValue" }',
'legacy:intersectionvar' => '{ "property": "intersectionVar" }',
Expand Down