Skip to content

Commit f577a4d

Browse files
authored
Merge pull request #53 from php-api-clients/ensure-webhooks-have-names-not-numbers
Ensure webhooks haves names not numbers
2 parents 445b5a7 + 7ec8f9e commit f577a4d

File tree

3 files changed

+50
-25
lines changed

3 files changed

+50
-25
lines changed

composer.lock

Lines changed: 14 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

external_files/cebe/SpecBaseObject.php

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,22 @@ public function __construct(array $data)
8686
} else {
8787
// array
8888
$this->_properties[$property] = [];
89-
foreach ($data[$property] as $item) {
89+
foreach ($data[$property] as $key => $item) {
9090
if ($type[0] === Type::STRING) {
9191
if (!is_string($item)) {
9292
$this->_errors[] = "property '$property' must be array of strings, but array has " . gettype($item) . " element.";
9393
}
94-
$this->_properties[$property][] = $item;
94+
$this->_properties[$property][$key] = $item;
9595
} elseif (Type::isScalar($type[0])) {
96-
$this->_properties[$property][] = $item;
96+
$this->_properties[$property][$key] = $item;
9797
} elseif ($type[0] === Type::ANY) {
9898
if (is_array($item) && isset($item['$ref'])) {
99-
$this->_properties[$property][] = new Reference($item, null);
99+
$this->_properties[$property][$key] = new Reference($item, null);
100100
} else {
101-
$this->_properties[$property][] = $item;
101+
$this->_properties[$property][$key] = $item;
102102
}
103103
} else {
104-
$this->_properties[$property][] = $this->instantiate($type[0], $item);
104+
$this->_properties[$property][$key] = $this->instantiate($type[0], $item);
105105
}
106106
}
107107
}
@@ -190,15 +190,23 @@ public function getSerializableData()
190190
if ($v instanceof SpecObjectInterface) {
191191
$data[$k] = $v->getSerializableData();
192192
} elseif (is_array($v)) {
193+
// test if php arrays should be represented as object in YAML/JSON
193194
$toObject = false;
194-
$j = 0;
195-
foreach ($v as $i => $d) {
196-
if ($j++ !== $i) {
197-
$toObject = true;
198-
}
199-
if ($d instanceof SpecObjectInterface) {
200-
$data[$k][$i] = $d->getSerializableData();
195+
if (!empty($v)) {
196+
// case 1: non-empty array should be an object if it does not contain
197+
// consecutive numeric keys
198+
$j = 0;
199+
foreach ($v as $i => $d) {
200+
if ($j++ !== $i) {
201+
$toObject = true;
202+
}
203+
if ($d instanceof SpecObjectInterface) {
204+
$data[$k][$i] = $d->getSerializableData();
205+
}
201206
}
207+
} elseif (isset($this->attributes()[$k]) && is_array($this->attributes()[$k]) && 2 === count($this->attributes()[$k])) {
208+
// case 2: Attribute type is an object (specified in attributes() by an array which specifies two items (key and value type)
209+
$toObject = true;
202210
}
203211
if ($toObject) {
204212
$data[$k] = (object) $data[$k];
@@ -297,11 +305,25 @@ protected function addError(string $error, $class = '')
297305
$this->_errors[] = end($shortName).$error;
298306
}
299307

308+
/**
309+
* @param string $name property name.
310+
* @return bool true when this object has a property with a non-null value or the property is defined in the OpenAPI spec.
311+
* @deprecated since 1.6.0, will be removed in 2.0.0
312+
*/
300313
protected function hasProperty(string $name): bool
301314
{
302315
return isset($this->_properties[$name]) || isset($this->attributes()[$name]);
303316
}
304317

318+
/**
319+
* @param string $name property name.
320+
* @return bool true, when a property has a non-null value (does not check for default values)
321+
*/
322+
protected function hasPropertyValue(string $name): bool
323+
{
324+
return isset($this->_properties[$name]);
325+
}
326+
305327
protected function requireProperties(array $names, array $atLeastOne = [])
306328
{
307329
foreach ($names as $name) {

src/Generator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ private function all(string $namespace): iterable
117117
if (count($this->spec->webhooks ?? []) > 0) {
118118
$pathClassNameMapping = [];
119119
foreach ($this->spec->webhooks as $path => $pathItem) {
120-
$webHookClassName = 'WebHook_' . $this->className($path);
120+
$webHookClassName = $this->className($path);
121121
$pathClassNameMapping[$path] = $this->fqcn($namespace . 'WebHook/' . $webHookClassName);
122122
if (strlen($webHookClassName) === 0) {
123123
continue;

0 commit comments

Comments
 (0)