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
110 changes: 56 additions & 54 deletions src/Processors/AugmentProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __invoke(Analysis $analysis): void
$typeAndDescription = $this->extractVarTypeAndDescription((string) $context->comment);

if (Generator::isDefault($property->type)) {
$this->augmentType($analysis, $property, $context, $typeAndDescription['type']);
$this->augmentSchemaType($analysis, $property, $context, $typeAndDescription['type']);
} else {
if (!is_array($property->type)) {
$this->mapNativeType($property, $property->type);
Expand All @@ -63,98 +63,100 @@ public function __invoke(Analysis $analysis): void
}
}

protected function augmentType(Analysis $analysis, OA\Property $property, Context $context, ?string $varType): void
protected function augmentSchemaType(Analysis $analysis, OA\Schema $schema, Context $context, ?string $varType): void
{
// docblock typehints
if ($varType) {
$allTypes = trim($varType);

if ($this->isNullable($allTypes) && Generator::isDefault($property->nullable)) {
$property->nullable = true;
if ($this->isNullable($allTypes) && Generator::isDefault($schema->nullable)) {
$schema->nullable = true;
}

$allTypes = $this->stripNull($allTypes);
preg_match('/^([^\[\<]+)(.*$)/', $allTypes, $typeMatches);
$type = $typeMatches[1];

// finalise property type/ref
if (!$this->mapNativeType($property, $type) && Generator::isDefault($property->items)) {
$schema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
if (Generator::isDefault($property->ref) && $schema) {
$property->ref = OA\Components::ref($schema);
if (!$this->mapNativeType($schema, $type) && Generator::isDefault($schema->items)) {
$typeSchema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
if (Generator::isDefault($schema->ref) && $typeSchema) {
$schema->ref = OA\Components::ref($typeSchema);
}
}

// ok, so we possibly have a type or ref
if (!Generator::isDefault($property->ref) && $typeMatches[2] === '' && !Generator::isDefault($property->nullable) && $property->nullable) {
$schema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
if ($schema) {
$property->ref = OA\Components::ref($schema);
if (!Generator::isDefault($schema->ref) && $typeMatches[2] === '' && !Generator::isDefault($schema->nullable) && $schema->nullable) {
$typeSchema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
if ($typeSchema) {
$schema->ref = OA\Components::ref($typeSchema);
}
} elseif ($typeMatches[2] === '[]') {
if (Generator::isDefault($property->items)) {
$property->items = new OA\Items([
'type' => $property->type,
'_context' => new Context(['generated' => true], $context),
]);
$analysis->addAnnotation($property->items, $property->items->_context);
if (!Generator::isDefault($property->ref)) {
$property->items->ref = $property->ref;
$property->ref = Generator::UNDEFINED;
if (Generator::isDefault($schema->items)) {
$schema->items = new OA\Items(
[
'type' => $schema->type,
'_context' => new Context(['generated' => true], $context),
]
);
$analysis->addAnnotation($schema->items, $schema->items->_context);
if (!Generator::isDefault($schema->ref)) {
$schema->items->ref = $schema->ref;
$schema->ref = Generator::UNDEFINED;
}
$property->type = 'array';
$schema->type = 'array';
}
} elseif ($property->type === 'integer' && str_starts_with($typeMatches[2], '<') && str_ends_with($typeMatches[2], '>')) {
} elseif ($schema->type === 'integer' && str_starts_with($typeMatches[2], '<') && str_ends_with($typeMatches[2], '>')) {
[$min, $max] = explode(',', substr($typeMatches[2], 1, -1));

if (is_numeric($min)) {
$property->minimum = (int) $min;
$schema->minimum = (int) $min;
}
if (is_numeric($max)) {
$property->maximum = (int) $max;
$schema->maximum = (int) $max;
}
} elseif ($type === 'positive-int') {
$property->type = 'integer';
$property->minimum = 1;
$schema->type = 'integer';
$schema->minimum = 1;
} elseif ($type === 'negative-int') {
$property->type = 'integer';
$property->maximum = -1;
$schema->type = 'integer';
$schema->maximum = -1;
} elseif ($type === 'non-positive-int') {
$property->type = 'integer';
$property->maximum = 0;
$schema->type = 'integer';
$schema->maximum = 0;
} elseif ($type === 'non-negative-int') {
$property->type = 'integer';
$property->minimum = 0;
$schema->type = 'integer';
$schema->minimum = 0;
} elseif ($type === 'non-zero-int') {
$property->type = 'integer';
$property->not = $property->_context->isVersion('3.1.x') ? ['const' => 0] : ['enum' => [0]];
$schema->type = 'integer';
$schema->not = $schema->_context->isVersion('3.1.x') ? ['const' => 0] : ['enum' => [0]];
}
}

// native typehints
if ($context->type && !Generator::isDefault($context->type)) {
if ($context->nullable === true && Generator::isDefault($property->nullable)) {
$property->nullable = true;
if ($context->nullable === true && Generator::isDefault($schema->nullable)) {
$schema->nullable = true;
}
$type = strtolower($context->type);
if (!$this->mapNativeType($property, $type)) {
$schema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
if (Generator::isDefault($property->ref) && $schema) {
$this->applyRef($analysis, $property, OA\Components::ref($schema));
if (!$this->mapNativeType($schema, $type)) {
$typeSchema = $analysis->getSchemaForSource($context->fullyQualifiedName($type));
if (Generator::isDefault($schema->ref) && $typeSchema) {
$this->applyRef($analysis, $schema, OA\Components::ref($typeSchema));
} else {
if (is_string($context->type) && $typeSchema = $analysis->getSchemaForSource($context->type)) {
if (Generator::isDefault($property->format)) {
$property->ref = OA\Components::ref($typeSchema);
$property->type = Generator::UNDEFINED;
if (Generator::isDefault($schema->format)) {
$schema->ref = OA\Components::ref($typeSchema);
$schema->type = Generator::UNDEFINED;
}
}
}
}
}

if (!Generator::isDefault($property->const) && Generator::isDefault($property->type)) {
if (!$this->mapNativeType($property, gettype($property->const))) {
$property->type = Generator::UNDEFINED;
if (!Generator::isDefault($schema->const) && Generator::isDefault($schema->type)) {
if (!$this->mapNativeType($schema, gettype($schema->const))) {
$schema->type = Generator::UNDEFINED;
}
}
}
Expand All @@ -180,18 +182,18 @@ protected function stripNull(string $typeDescription): string
return implode('|', $types);
}

protected function applyRef(Analysis $analysis, OA\Property $property, string $ref): void
protected function applyRef(Analysis $analysis, OA\Schema $schema, string $ref): void
{
if ($property->nullable === true) {
$property->oneOf = [
$schema = new OA\Schema([
if ($schema->nullable === true) {
$schema->oneOf = [
$nullableSchema = new OA\Schema([
'ref' => $ref,
'_context' => new Context(['generated' => true], $property->_context),
'_context' => new Context(['generated' => true], $schema->_context),
]),
];
$analysis->addAnnotation($schema, $schema->_context);
$analysis->addAnnotation($nullableSchema, $nullableSchema->_context);
} else {
$property->ref = $ref;
$schema->ref = $ref;
}
}
}
4 changes: 2 additions & 2 deletions tests/Annotations/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public static function securityData(): iterable
/**
* @dataProvider securityData
*/
public function testSecuritySerialization(array $security, string $dockBlock, string $expected): void
public function testSecuritySerialization(array $security, string $docBlock, string $expected): void
{
// test with Get implementation...
$operation = new OA\Get([
Expand All @@ -51,7 +51,7 @@ public function testSecuritySerialization(array $security, string $dockBlock, st
$json = $operation->toJson($flags);
$this->assertEquals($expected, $json);

$annotations = $this->annotationsFromDocBlockParser($dockBlock);
$annotations = $this->annotationsFromDocBlockParser($docBlock);
$this->assertCount(1, $annotations);
$json = $annotations[0]->toJson($flags);
$this->assertEquals($expected, $json);
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Scratch/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class DocblockSchemaChild extends DocblocksSchema
}

/**
* @OA\Info(title="Dockblocks", version="1.0")
* @OA\Info(title="Docblocks", version="1.0")
* @OA\Get(
* path="/api/endpoint",
* @OA\Response(
Expand All @@ -99,6 +99,6 @@ class DocblockSchemaChild extends DocblocksSchema
* )
* )
*/
class DockblocksEndpoint
class DocblocksEndpoint
{
}
4 changes: 2 additions & 2 deletions tests/Fixtures/Scratch/Docblocks3.0.0.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
openapi: 3.0.0
info:
title: Dockblocks
title: Docblocks
version: '1.0'
paths:
/api/endpoint:
get:
operationId: 2dd3513e31e559b14abab58814959d68
operationId: 9ed0ad39e1cf5baebc3c0e2c101c9cad
responses:
'200':
description: 'successful operation'
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/Scratch/Docblocks3.1.0.yaml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
openapi: 3.1.0
info:
title: Dockblocks
title: Docblocks
version: '1.0'
paths:
/api/endpoint:
get:
operationId: 2dd3513e31e559b14abab58814959d68
operationId: 9ed0ad39e1cf5baebc3c0e2c101c9cad
responses:
'200':
description: 'successful operation'
Expand Down