Skip to content
Open
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
9 changes: 8 additions & 1 deletion src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
use cebe\openapi\json\JsonReference;
use cebe\openapi\spec\Reference;
use cebe\openapi\spec\Type;
use \JsonSerializable;

/**
* Base class for all spec objects.
*
* Implements property management and validation basics.
*
*/
abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface, RawSpecDataInterface
abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface, JsonSerializable, RawSpecDataInterface
{
private $_rawSpec;
private $_properties = [];
Expand Down Expand Up @@ -528,6 +529,12 @@ public function getExtensions(): array
return $extensions;
}

#[\ReturnTypeWillChange]
public function jsonSerialize()
{
return $this->getSerializableData();
}

public function getRawSpecData(): array
{
return $this->_rawSpec;
Expand Down
34 changes: 28 additions & 6 deletions tests/spec/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testReadPetStore()

public function assertAllInstanceOf($className, $array)
{
foreach($array as $k => $v) {
foreach ($array as $k => $v) {
$this->assertInstanceOf($className, $v, "Asserting that item with key '$k' is instance of $className");
}
}
Expand Down Expand Up @@ -147,7 +147,7 @@ public function specProvider()
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/apis-guru/openapi-directory/APIs'));
$it->rewind();
while($it->valid()) {
while ($it->valid()) {
if ($it->getBasename() === 'openapi.yaml') {
$apisGuruExamples[] = $it->key();
}
Expand All @@ -159,10 +159,10 @@ public function specProvider()
/** @var $it RecursiveDirectoryIterator|RecursiveIteratorIterator */
$it = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(__DIR__ . '/../../vendor/nexmo/api-specification/definitions'));
$it->rewind();
while($it->valid()) {
while ($it->valid()) {
if ($it->getExtension() === 'yml'
&& strpos($it->getSubPath(), 'common') === false
&& $it->getBasename() !== 'voice.v2.yml' // contains invalid references
&& strpos($it->getSubPath(), 'common') === false
&& $it->getBasename() !== 'voice.v2.yml' // contains invalid references
) {
$nexmoExamples[] = $it->key();
}
Expand All @@ -175,7 +175,7 @@ public function specProvider()
$apisGuruExamples,
$nexmoExamples
);
foreach($all as $path) {
foreach ($all as $path) {
yield [
substr($path, strlen(__DIR__ . '/../../vendor/')),
basename(dirname($path, 2)) . DIRECTORY_SEPARATOR . basename(dirname($path, 1)) . DIRECTORY_SEPARATOR . basename($path)
Expand Down Expand Up @@ -233,4 +233,26 @@ public function testSpecs($openApiFile)
}

}

public function testJsonSerialize()
{
$spec = <<<YML
openapi: 3.0.0
info:
title: Minimal API
version: 1.0.0
paths:
/:
get:
summary: Retrieves a minimal response
responses:
'200':
description: OK

YML;
$yaml = Yaml::parse($spec);
$openapi = new OpenApi($yaml);

$this->assertSame(json_encode($openapi), '{"openapi":"3.0.0","info":{"title":"Minimal API","version":"1.0.0"},"paths":{"\/":{"get":{"summary":"Retrieves a minimal response","responses":{"200":{"description":"OK"}}}}}}');
}
}