Skip to content

Hold raw spec data #228

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

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
16 changes: 16 additions & 0 deletions src/RawSpecDataInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/**
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> 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;
}
9 changes: 8 additions & 1 deletion src/SpecBaseObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -525,4 +527,9 @@ public function getExtensions(): array
}
return $extensions;
}

public function getRawSpecData(): array
{
return $this->_rawSpec;
}
}
19 changes: 15 additions & 4 deletions src/spec/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
*/
Expand Down Expand Up @@ -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) . "'."
Expand Down Expand Up @@ -402,4 +408,9 @@ public function getDocumentPosition(): ?JsonPointer
{
return $this->_jsonPointer;
}

public function getRawSpecData(): array
{
return $this->_rawSpec;
}
}
108 changes: 108 additions & 0 deletions tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,114 @@ public function testSymfonyYamlBugHunt()
$this->assertEquals($expectedArray, $inlineYamlExample);
}

public function testGetRawSpecData()
{
$spec = <<<YML
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"

YML;

$openapi = \cebe\openapi\Reader::readFromYaml($spec);
$this->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
Expand Down