Skip to content

Commit 6348137

Browse files
authored
Fixes schema collection bug (#569)
* Fixes schema collection bug
1 parent 56dd2c0 commit 6348137

2 files changed

Lines changed: 14 additions & 7 deletions

File tree

src/Lib/OpenApi/SchemaProperty.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,8 +122,11 @@ public function toArray(): array
122122
$vars['enum'] = array_values($vars['enum']);
123123
}
124124

125-
if (!empty($vars['items'])) {
126-
$vars['items'] = (object)reset($vars['items']);
125+
if (!empty($vars['items']) && $vars['type'] === 'array' && count($vars['items']) === 1) {
126+
$item = reset($vars['items']);
127+
if (is_array($item) && $item['type'] === 'object') {
128+
$vars['items'] = (object)$item;
129+
}
127130
}
128131

129132
return $vars;

tests/TestCase/Lib/MediaType/GenericTest.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
declare(strict_types=1);
23

34
namespace SwaggerBake\Test\TestCase\Lib\MediaType;
45

@@ -36,7 +37,7 @@ public function setUp(): void
3637
'controllers' => ['\SwaggerBakeTest\App\\'],
3738
'entities' => ['\SwaggerBakeTest\App\\'],
3839
'tables' => ['\SwaggerBakeTest\App\\'],
39-
]
40+
],
4041
], SWAGGER_BAKE_TEST_APP);
4142
}
4243

@@ -47,7 +48,7 @@ public function test_collection(): void
4748
$schema = (new Generic($swagger))->buildSchema('#/components/schemas/thing', 'array');
4849
$this->assertEquals(
4950
'#/x-swagger-bake/components/schemas/Generic-Collection',
50-
$schema->getAllOf()[0]['$ref']
51+
$schema->getAllOf()[0]['$ref'],
5152
);
5253
$this->assertArrayHasKey('data', $schema->getProperties());
5354
}
@@ -70,7 +71,7 @@ public function test_collection_with_associations(): void
7071
->setName('PostalCode')
7172
->setProperties([
7273
(new SchemaProperty())->setName('id'),
73-
(new SchemaProperty())->setName('PostalCode')
74+
(new SchemaProperty())->setName('PostalCode'),
7475
]),
7576
]);
7677

@@ -81,8 +82,11 @@ public function test_collection_with_associations(): void
8182
$this->assertArrayHasKey('PostalCode', $items['properties']);
8283
$this->assertEquals(
8384
'#/x-swagger-bake/components/schemas/Generic-Collection',
84-
$schema->getAllOf()[0]['$ref']
85+
$schema->getAllOf()[0]['$ref'],
8586
);
8687
$this->assertArrayHasKey('data', $schema->getProperties());
88+
89+
$arr = json_decode(json_encode($schema->toArray()), true);
90+
$this->assertArrayHasKey('Country', $arr['properties']['data']['items']['properties']);
8791
}
88-
}
92+
}

0 commit comments

Comments
 (0)