Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for WebHooks #115

Merged
merged 1 commit into from
Dec 21, 2021
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
12 changes: 11 additions & 1 deletion src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,23 @@ protected function hasProperty(string $name): bool
return isset($this->_properties[$name]) || isset($this->attributes()[$name]);
}

protected function requireProperties(array $names)
protected function requireProperties(array $names, array $atLeastOne = [])
{
foreach ($names as $name) {
if (!isset($this->_properties[$name])) {
$this->addError(" is missing required property: $name", get_class($this));
}
}

if (count($atLeastOne) > 0) {
foreach ($atLeastOne as $name) {
if (array_key_exists($name, $this->_properties)) {
return;
}
}

$this->addError(" is missing at least one of the following required properties: " . implode(', ', $atLeastOne), get_class($this));
}
}

protected function validateEmail(string $property)
Expand Down
9 changes: 8 additions & 1 deletion src/spec/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
* @property Server[] $servers
* @property Paths|PathItem[] $paths
* @property Components|null $components
* @property PathItem[]|null $webhooks
* @property SecurityRequirement[] $security
* @property Tag[] $tags
* @property ExternalDocumentation|null $externalDocs
Expand All @@ -46,6 +47,7 @@ protected function attributes(): array
'info' => Info::class,
'servers' => [Server::class],
'paths' => Paths::class,
'webhooks' => [PathItem::class],
'components' => Components::class,
'security' => [SecurityRequirement::class],
'tags' => [Tag::class],
Expand Down Expand Up @@ -83,7 +85,12 @@ public function __get($name)
*/
public function performValidation()
{
$this->requireProperties(['openapi', 'info', 'paths']);
if ($this->getMajorVersion() === static::VERSION_3_0) {
$this->requireProperties(['openapi', 'info', 'paths']);
} else {
$this->requireProperties(['openapi', 'info'], ['paths', 'webhooks', 'components']);
}

if (!empty($this->openapi) && !preg_match(static::PATTERN_VERSION, $this->openapi)) {
$this->addError('Unsupported openapi version: ' . $this->openapi);
}
Expand Down
2 changes: 1 addition & 1 deletion src/spec/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
* @property string[] $required list of required properties
* @property array $enum
*
* @property string $type
* @property string|string[] $type type can only be `string` in OpenAPI 3.0, but can be an array of strings since OpenAPI 3.1
* @property Schema[]|Reference[] $allOf
* @property Schema[]|Reference[] $oneOf
* @property Schema[]|Reference[] $anyOf
Expand Down
2 changes: 2 additions & 0 deletions src/spec/Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ class Type
const BOOLEAN = 'boolean';
const OBJECT = 'object';
const ARRAY = 'array';
const NULL = 'null'; // Since OpenAPI 3.1

/**
* Indicate whether a type is a scalar type, i.e. not an array or object.
Expand All @@ -38,6 +39,7 @@ public static function isScalar(string $type): bool
self::NUMBER,
self::STRING,
self::BOOLEAN,
self::NULL,
]);
}
}
9 changes: 7 additions & 2 deletions tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testEmpty()
$this->assertEquals([
'OpenApi is missing required property: openapi',
'OpenApi is missing required property: info',
'OpenApi is missing required property: paths',
'OpenApi is missing at least one of the following required properties: paths, webhooks, components',
], $openapi->getErrors());

// check default value of servers
Expand Down Expand Up @@ -232,10 +232,15 @@ public function testSpecs($openApiFile)
$this->assertAllInstanceOf(\cebe\openapi\spec\Server::class, $openapi->servers);

// paths
if ($openapi->components !== null) {
if ($openapi->paths !== null) {
$this->assertInstanceOf(\cebe\openapi\spec\Paths::class, $openapi->paths);
}

// webhooks
if ($openapi->webhooks !== null) {
$this->assertAllInstanceOf(\cebe\openapi\spec\PathItem::class, $openapi->webhooks);
}

// components
if ($openapi->components !== null) {
$this->assertInstanceOf(\cebe\openapi\spec\Components::class, $openapi->components);
Expand Down
2 changes: 1 addition & 1 deletion tests/spec/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public function testRead()
}
}

public function testCreateionFromObjects()
public function testCreationFromObjects()
{
$paths = new Paths([
'/pets' => new PathItem([
Expand Down