diff --git a/src/RawSpecDataInterface.php b/src/RawSpecDataInterface.php new file mode 100644 index 00000000..3fecc1f0 --- /dev/null +++ b/src/RawSpecDataInterface.php @@ -0,0 +1,16 @@ + and contributors + * @license https://github.com/cebe/php-openapi/blob/master/LICENSE + */ + +namespace cebe\openapi; + +/** + * Make raw spec data available to the implementing classes + */ +interface RawSpecDataInterface +{ + public function getRawSpecData(): array; +} diff --git a/src/SpecBaseObject.php b/src/SpecBaseObject.php index 1de429bd..ef93401e 100644 --- a/src/SpecBaseObject.php +++ b/src/SpecBaseObject.php @@ -20,8 +20,9 @@ * Implements property management and validation basics. * */ -abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface +abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface, RawSpecDataInterface { + private $_rawSpec; private $_properties = []; private $_errors = []; @@ -63,6 +64,7 @@ abstract protected function performValidation(); */ public function __construct(array $data) { + $this->_rawSpec = $data; foreach ($this->attributes() as $property => $type) { if (!isset($data[$property])) { continue; @@ -525,4 +527,9 @@ public function getExtensions(): array } return $extensions; } + + public function getRawSpecData(): array + { + return $this->_rawSpec; + } } diff --git a/src/spec/Reference.php b/src/spec/Reference.php index cda612a9..f9b84d67 100644 --- a/src/spec/Reference.php +++ b/src/spec/Reference.php @@ -8,16 +8,15 @@ namespace cebe\openapi\spec; use cebe\openapi\DocumentContextInterface; -use cebe\openapi\exceptions\IOException; use cebe\openapi\exceptions\TypeErrorException; use cebe\openapi\exceptions\UnresolvableReferenceException; use cebe\openapi\json\InvalidJsonPointerSyntaxException; use cebe\openapi\json\JsonPointer; use cebe\openapi\json\JsonReference; use cebe\openapi\json\NonexistentJsonPointerReferenceException; +use cebe\openapi\RawSpecDataInterface; use cebe\openapi\ReferenceContext; use cebe\openapi\SpecObjectInterface; -use Symfony\Component\Yaml\Yaml; /** * Reference Object @@ -27,8 +26,14 @@ * @link https://tools.ietf.org/html/rfc6901 * */ -class Reference implements SpecObjectInterface, DocumentContextInterface +class Reference implements SpecObjectInterface, DocumentContextInterface, RawSpecDataInterface { + /** + * Holds raw spec data + * @var array + */ + private $_rawSpec; + /** * @var string */ @@ -61,11 +66,12 @@ class Reference implements SpecObjectInterface, DocumentContextInterface /** * Create an object from spec data. * @param array $data spec data read from YAML or JSON - * @param string $to class name of the type referenced by this Reference + * @param string|null $to class name of the type referenced by this Reference * @throws TypeErrorException in case invalid data is supplied. */ public function __construct(array $data, string $to = null) { + $this->_rawSpec = $data; if (!isset($data['$ref'])) { throw new TypeErrorException( "Unable to instantiate Reference Object with data '" . print_r($data, true) . "'." @@ -402,4 +408,9 @@ public function getDocumentPosition(): ?JsonPointer { return $this->_jsonPointer; } + + public function getRawSpecData(): array + { + return $this->_rawSpec; + } } diff --git a/tests/ReaderTest.php b/tests/ReaderTest.php index 589b7cbb..eee95951 100644 --- a/tests/ReaderTest.php +++ b/tests/ReaderTest.php @@ -128,6 +128,114 @@ public function testSymfonyYamlBugHunt() $this->assertEquals($expectedArray, $inlineYamlExample); } + public function testGetRawSpecData() + { + $spec = <<assertSame($openapi->getRawSpecData(), [ + 'openapi' => '3.0.0', + 'info' => [ + 'version' => '1.0.0', + 'title' => 'Check storage of raw spec data', + ], + 'paths' => [ + '/' => [ + 'get' => [ + 'summary' => 'List', + 'operationId' => 'list', + 'responses' => [ + '200' => [ + 'description' => 'The information', + ] + ] + ] + ] + ], + 'components' => [ + 'schemas' => [ + 'User' => [ + 'type' => 'object', + 'properties' => [ + 'id' => [ + 'type' => 'integer', + ], + 'name' => [ + 'type' => 'string', + ] + ] + ], + 'Post' => [ + 'type' => 'object', + 'properties' => [ + 'id' => [ + 'type' => 'integer', + ], + 'title' => [ + 'type' => 'string', + ], + 'user' => [ + '$ref' => '#/components/schemas/User', + ] + ] + ] + ] + ] + ]); + + $this->assertSame($openapi->components->schemas['User']->getRawSpecData(), [ + 'type' => 'object', + 'properties' => [ + 'id' => [ + 'type' => 'integer', + ], + 'name' => [ + 'type' => 'string', + ] + ] + ]); + + $this->assertSame($openapi->components->schemas['Post']->properties['user']->getRawSpecData(), [ + '$ref' => '#/components/schemas/User', + ]); + + } + // TODO test invalid JSON // TODO test invalid YAML