Skip to content

Commit 09610b2

Browse files
authored
Add Schema::hasType() to encapsulate string|array duality of schema type (#1936)
1 parent 9cdd14a commit 09610b2

15 files changed

Lines changed: 38 additions & 27 deletions

docs/examples/processors/schema-query-parameter/scan.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
};
2424

2525
$openapi = (new Generator())
26-
->withProcessorPipeline(function (Pipeline $pipeline) use ($insertMatch) { $pipeline->insert(new SchemaQueryParameter(), $insertMatch); })
26+
->withProcessorPipeline(fn (Pipeline $pipeline) => $pipeline->insert(new SchemaQueryParameter(), $insertMatch))
2727
->generate([__DIR__ . '/app']);
2828
// file_put_contents(__DIR__ . '/schema-query-parameter.yaml', $openapi->toYaml());
2929
echo $openapi->toYaml();

src/Annotations/Schema.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,14 @@ public function isNullable(): bool
514514
return !Generator::isDefault($this->nullable) && $this->nullable;
515515
}
516516

517+
/**
518+
* Check if the given type is valid for this schema.
519+
*/
520+
public function hasType(string $type): bool
521+
{
522+
return in_array($type, (array) $this->type, true);
523+
}
524+
517525
public function jsonSerialize(): \stdClass
518526
{
519527
$data = parent::jsonSerialize();
@@ -534,7 +542,7 @@ public function jsonSerialize(): \stdClass
534542
*/
535543
public function validate(array $stack = [], array $skip = [], string $ref = '', ?object $context = null): bool
536544
{
537-
if ($this->type === 'array' && Generator::isDefault($this->items)) {
545+
if ($this->hasType('array') && Generator::isDefault($this->items)) {
538546
$this->_context->logger->warning('@OA\\Items() is required when ' . $this->identity() . ' has type "array" in ' . $this->_context);
539547

540548
return false;

tests/Annotations/AttachableTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testCustomAttachableImplementationsAreAttached(): void
3333
->setTypeResolver($this->getTypeResolver())
3434
->addAlias('oaf', 'OpenApi\Tests\Fixtures\Annotations')
3535
->addNamespace('OpenApi\Tests\Fixtures\Annotations\\')
36-
->withProcessorPipeline(function (Pipeline $processor) { $processor->remove(null, function ($pipe) { return !$pipe instanceof CleanUnusedComponents; }); })
36+
->withProcessorPipeline(fn (Pipeline $processor) => $processor->remove(null, fn ($pipe) => !$pipe instanceof CleanUnusedComponents))
3737
->generate($this->fixtures(['UsingCustomAttachables.php']), $analysis);
3838

3939
$schemas = $analysis->getAnnotationsOfType(OA\Schema::class, true);

tests/Fixtures/Scratch/NestedSchema.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function __construct(
7070
uniqueItems: true,
7171
additionalProperties: new OAT\AdditionalProperties(
7272
description: 'Array of error messages for property',
73-
type: 'array',
73+
type: ['array'],
7474
items: new OAT\Items(
7575
type: 'string',
7676
),

tests/Fixtures/Scratch/PromotedProperty3.0.0.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ components:
2424
other:
2525
description: 'Other value.'
2626
type: string
27-
myEnum:
28-
$ref: '#/components/schemas/MyEnum'
29-
thename:
30-
description: 'Property name.'
31-
type: string
3227
themeta:
3328
description: 'Property meta.'
3429
type: string
3530
different:
3631
description: 'Property different.'
3732
type: string
33+
myEnum:
34+
$ref: '#/components/schemas/MyEnum'
35+
thename:
36+
description: 'Property name.'
37+
type: string
3838
type: object

tests/Fixtures/Scratch/PromotedProperty3.1.0.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ components:
2424
other:
2525
description: 'Other value.'
2626
type: string
27-
myEnum:
28-
$ref: '#/components/schemas/MyEnum'
29-
thename:
30-
description: 'Property name.'
31-
type: string
3227
themeta:
3328
description: 'Property meta.'
3429
type: string
3530
different:
3631
description: 'Property different.'
3732
type: string
33+
myEnum:
34+
$ref: '#/components/schemas/MyEnum'
35+
thename:
36+
description: 'Property name.'
37+
type: string
3838
type: object

tests/Fixtures/Scratch/PromotedProperty3.2.0.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,15 @@ components:
2424
other:
2525
description: 'Other value.'
2626
type: string
27-
myEnum:
28-
$ref: '#/components/schemas/MyEnum'
29-
thename:
30-
description: 'Property name.'
31-
type: string
3227
themeta:
3328
description: 'Property meta.'
3429
type: string
3530
different:
3631
description: 'Property different.'
3732
type: string
33+
myEnum:
34+
$ref: '#/components/schemas/MyEnum'
35+
thename:
36+
description: 'Property name.'
37+
type: string
3838
type: object

tests/Fixtures/Scratch/PropertyItems.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class PropertyItems
3333
items: new OAT\Items(type: ItemDto::class),
3434
minItems: 2,
3535
)]
36-
public array $list1;
36+
public ?array $list1;
3737

3838
/**
3939
* @var ItemDto[] $list2

tests/Fixtures/Scratch/PropertyItems3.0.0.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ components:
3232
items:
3333
$ref: '#/components/schemas/ItemDto'
3434
minItems: 2
35+
nullable: true
3536
list2:
3637
description: 'With docblock'
3738
type: array

tests/Fixtures/Scratch/PropertyItems3.1.0.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ components:
2828
properties:
2929
list1:
3030
description: 'Missing docblock'
31-
type: array
31+
type:
32+
- array
33+
- 'null'
3234
items:
3335
$ref: '#/components/schemas/ItemDto'
3436
minItems: 2

0 commit comments

Comments
 (0)