Skip to content

Commit 3562b76

Browse files
DanielWiedemann248LKaemmerling
authored andcommitted
Fix Image Parsing Between Object And Array
The default value for Image::labels is an array, according to the API, an object is returned, however.
1 parent e57cf1f commit 3562b76

2 files changed

Lines changed: 21 additions & 21 deletions

File tree

src/Models/Images/Image.php

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ class Image extends Model implements Resource
135135
public $protection;
136136

137137
/**
138-
* @var array
138+
* @var \stdClass|null
139139
*/
140140
public $labels;
141141

@@ -158,21 +158,21 @@ class Image extends Model implements Resource
158158
* Image constructor.
159159
*
160160
* @param int $id
161-
* @param string $type
162-
* @param string $status
163-
* @param string $name
164-
* @param string $description
165-
* @param float $imageSize
166-
* @param int $diskSize
167-
* @param string $created
168-
* @param \LKDev\HetznerCloud\Models\Servers\Server $createdFrom
169-
* @param int $boundTo
170-
* @param string $osFlavor
171-
* @param string $osVersion
161+
* @param string|null $type
162+
* @param string|null $status
163+
* @param string|null $name
164+
* @param string|null $description
165+
* @param float|null $imageSize
166+
* @param int|null $diskSize
167+
* @param string|null $created
168+
* @param null $createdFrom
169+
* @param int|null $boundTo
170+
* @param string|null $osFlavor
171+
* @param string|null $osVersion
172172
* @param bool $rapidDeploy
173-
* @param Protection $protection
174-
* @param string $architecture
175-
* @param array $labels
173+
* @param Protection|null $protection
174+
* @param string|null $architecture
175+
* @param \stdClass $labels
176176
* @param string|null $deleted
177177
* @param string|null $deprecated
178178
*/
@@ -192,7 +192,7 @@ public function __construct(
192192
?bool $rapidDeploy = null,
193193
?Protection $protection = null,
194194
?string $architecture = null,
195-
array $labels = [],
195+
\stdClass $labels = new \stdClass(),
196196
?string $deleted = null,
197197
?string $deprecated = null
198198
) {
@@ -293,15 +293,15 @@ public function delete(): bool
293293

294294
/**
295295
* @param $input
296-
* @return \LKDev\HetznerCloud\Models\Images\Image|static
296+
* @return \LKDev\HetznerCloud\Models\Images\Image|static|null
297297
*/
298298
public static function parse($input): ?Image
299299
{
300300
if ($input == null) {
301301
return null;
302302
}
303303

304-
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy, Protection::parse($input->protection), $input->architecture ?? null, get_object_vars($input->labels), $input->deleted ?? null, $input->deprecated ?? null);
304+
return new self($input->id, $input->type, $input->status, $input->name, $input->description, $input->image_size, $input->disk_size, $input->created, $input->created_from, $input->bound_to, $input->os_flavor, $input->os_version, $input->rapid_deploy, Protection::parse($input->protection), $input->architecture ?? null, $input->labels, $input->deleted ?? null, $input->deprecated ?? null);
305305
}
306306

307307
public function reload()

tests/Unit/Models/Images/ImagesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testGet()
3333
$this->assertEquals($image->id, 4711);
3434
$this->assertEquals($image->name, 'ubuntu-20.04');
3535

36-
$this->assertEmpty($image->labels);
36+
$this->assertEmpty((array) $image->labels);
3737

3838
$this->assertLastRequestEquals('GET', '/images/4711');
3939
}
@@ -45,7 +45,7 @@ public function testGetByName()
4545
$this->assertEquals($image->id, 4711);
4646
$this->assertEquals($image->name, 'ubuntu-20.04');
4747

48-
$this->assertEmpty($image->labels);
48+
$this->assertEmpty((array) $image->labels);
4949
$this->assertLastRequestEquals('GET', '/images');
5050
$this->assertLastRequestQueryParametersContains('name', 'ubuntu-20.04');
5151
}
@@ -57,7 +57,7 @@ public function testGetByNameWithArchitecture()
5757
$this->assertEquals($image->id, 4711);
5858
$this->assertEquals($image->name, 'ubuntu-20.04');
5959

60-
$this->assertEmpty($image->labels);
60+
$this->assertEmpty((array) $image->labels);
6161
$this->assertLastRequestEquals('GET', '/images');
6262
$this->assertLastRequestQueryParametersContains('name', 'ubuntu-20.04');
6363
$this->assertLastRequestQueryParametersContains('architecture', 'arm');

0 commit comments

Comments
 (0)