diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index ef93401..fd2b140 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -13,6 +13,7 @@ use cebe\openapi\json\JsonReference; use cebe\openapi\spec\Reference; use cebe\openapi\spec\Type; +use \JsonSerializable; /** * Base class for all spec objects. @@ -20,7 +21,7 @@ * 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 = []; @@ -528,6 +529,12 @@ public function getExtensions(): array return $extensions; } + #[\ReturnTypeWillChange] + public function jsonSerialize() + { + return $this->getSerializableData(); + } + public function getRawSpecData(): array { return $this->_rawSpec; diff --git a/tests/spec/OpenApiTest.php b/tests/spec/OpenApiTest.php index 433bd35..de2d344 100644 --- a/tests/spec/OpenApiTest.php +++ b/tests/spec/OpenApiTest.php @@ -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"); } } @@ -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(); } @@ -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(); } @@ -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) @@ -233,4 +233,26 @@ public function testSpecs($openApiFile) } } + + public function testJsonSerialize() + { + $spec = <<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"}}}}}}'); + } }