Skip to content

Commit 03a3781

Browse files
authored
Merge pull request #228 from SOHELAHMED7/cebe-php-openapi-avoid-calling-getserializabledata
Hold raw spec data
2 parents 6f4e76f + 69e4891 commit 03a3781

File tree

4 files changed

+147
-5
lines changed

4 files changed

+147
-5
lines changed

src/RawSpecDataInterface.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* @copyright Copyright (c) 2018 Carsten Brandt <mail@cebe.cc> and contributors
5+
* @license https://github.com/cebe/php-openapi/blob/master/LICENSE
6+
*/
7+
8+
namespace cebe\openapi;
9+
10+
/**
11+
* Make raw spec data available to the implementing classes
12+
*/
13+
interface RawSpecDataInterface
14+
{
15+
public function getRawSpecData(): array;
16+
}

src/SpecBaseObject.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
* Implements property management and validation basics.
2121
*
2222
*/
23-
abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface
23+
abstract class SpecBaseObject implements SpecObjectInterface, DocumentContextInterface, RawSpecDataInterface
2424
{
25+
private $_rawSpec;
2526
private $_properties = [];
2627
private $_errors = [];
2728

@@ -63,6 +64,7 @@ abstract protected function performValidation();
6364
*/
6465
public function __construct(array $data)
6566
{
67+
$this->_rawSpec = $data;
6668
foreach ($this->attributes() as $property => $type) {
6769
if (!isset($data[$property])) {
6870
continue;
@@ -525,4 +527,9 @@ public function getExtensions(): array
525527
}
526528
return $extensions;
527529
}
530+
531+
public function getRawSpecData(): array
532+
{
533+
return $this->_rawSpec;
534+
}
528535
}

src/spec/Reference.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,15 @@
88
namespace cebe\openapi\spec;
99

1010
use cebe\openapi\DocumentContextInterface;
11-
use cebe\openapi\exceptions\IOException;
1211
use cebe\openapi\exceptions\TypeErrorException;
1312
use cebe\openapi\exceptions\UnresolvableReferenceException;
1413
use cebe\openapi\json\InvalidJsonPointerSyntaxException;
1514
use cebe\openapi\json\JsonPointer;
1615
use cebe\openapi\json\JsonReference;
1716
use cebe\openapi\json\NonexistentJsonPointerReferenceException;
17+
use cebe\openapi\RawSpecDataInterface;
1818
use cebe\openapi\ReferenceContext;
1919
use cebe\openapi\SpecObjectInterface;
20-
use Symfony\Component\Yaml\Yaml;
2120

2221
/**
2322
* Reference Object
@@ -27,8 +26,14 @@
2726
* @link https://tools.ietf.org/html/rfc6901
2827
*
2928
*/
30-
class Reference implements SpecObjectInterface, DocumentContextInterface
29+
class Reference implements SpecObjectInterface, DocumentContextInterface, RawSpecDataInterface
3130
{
31+
/**
32+
* Holds raw spec data
33+
* @var array
34+
*/
35+
private $_rawSpec;
36+
3237
/**
3338
* @var string
3439
*/
@@ -61,11 +66,12 @@ class Reference implements SpecObjectInterface, DocumentContextInterface
6166
/**
6267
* Create an object from spec data.
6368
* @param array $data spec data read from YAML or JSON
64-
* @param string $to class name of the type referenced by this Reference
69+
* @param string|null $to class name of the type referenced by this Reference
6570
* @throws TypeErrorException in case invalid data is supplied.
6671
*/
6772
public function __construct(array $data, string $to = null)
6873
{
74+
$this->_rawSpec = $data;
6975
if (!isset($data['$ref'])) {
7076
throw new TypeErrorException(
7177
"Unable to instantiate Reference Object with data '" . print_r($data, true) . "'."
@@ -402,4 +408,9 @@ public function getDocumentPosition(): ?JsonPointer
402408
{
403409
return $this->_jsonPointer;
404410
}
411+
412+
public function getRawSpecData(): array
413+
{
414+
return $this->_rawSpec;
415+
}
405416
}

tests/ReaderTest.php

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,114 @@ public function testSymfonyYamlBugHunt()
128128
$this->assertEquals($expectedArray, $inlineYamlExample);
129129
}
130130

131+
public function testGetRawSpecData()
132+
{
133+
$spec = <<<YML
134+
openapi: "3.0.0"
135+
info:
136+
version: 1.0.0
137+
title: Check storage of raw spec data
138+
139+
paths:
140+
/:
141+
get:
142+
summary: List
143+
operationId: list
144+
responses:
145+
'200':
146+
description: The information
147+
148+
components:
149+
schemas:
150+
User:
151+
type: object
152+
properties:
153+
id:
154+
type: integer
155+
name:
156+
type: string
157+
158+
Post:
159+
type: object
160+
properties:
161+
id:
162+
type: integer
163+
title:
164+
type: string
165+
user:
166+
\$ref: "#/components/schemas/User"
167+
168+
YML;
169+
170+
$openapi = \cebe\openapi\Reader::readFromYaml($spec);
171+
$this->assertSame($openapi->getRawSpecData(), [
172+
'openapi' => '3.0.0',
173+
'info' => [
174+
'version' => '1.0.0',
175+
'title' => 'Check storage of raw spec data',
176+
],
177+
'paths' => [
178+
'/' => [
179+
'get' => [
180+
'summary' => 'List',
181+
'operationId' => 'list',
182+
'responses' => [
183+
'200' => [
184+
'description' => 'The information',
185+
]
186+
]
187+
]
188+
]
189+
],
190+
'components' => [
191+
'schemas' => [
192+
'User' => [
193+
'type' => 'object',
194+
'properties' => [
195+
'id' => [
196+
'type' => 'integer',
197+
],
198+
'name' => [
199+
'type' => 'string',
200+
]
201+
]
202+
],
203+
'Post' => [
204+
'type' => 'object',
205+
'properties' => [
206+
'id' => [
207+
'type' => 'integer',
208+
],
209+
'title' => [
210+
'type' => 'string',
211+
],
212+
'user' => [
213+
'$ref' => '#/components/schemas/User',
214+
]
215+
]
216+
]
217+
]
218+
]
219+
]);
220+
221+
$this->assertSame($openapi->components->schemas['User']->getRawSpecData(), [
222+
'type' => 'object',
223+
'properties' => [
224+
'id' => [
225+
'type' => 'integer',
226+
],
227+
'name' => [
228+
'type' => 'string',
229+
]
230+
]
231+
]);
232+
233+
$this->assertSame($openapi->components->schemas['Post']->properties['user']->getRawSpecData(), [
234+
'$ref' => '#/components/schemas/User',
235+
]);
236+
237+
}
238+
131239

132240
// TODO test invalid JSON
133241
// TODO test invalid YAML

0 commit comments

Comments
 (0)