Skip to content

Commit 29e7095

Browse files
committed
Merge branch 'master' of github.com:SOHELAHMED7/php-openapi into how-to-use-security-and-securityrequirement-75
2 parents e8711e1 + 4c670b0 commit 29e7095

File tree

9 files changed

+213
-18
lines changed

9 files changed

+213
-18
lines changed

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"php": ">=7.1.0",
2222
"ext-json": "*",
2323
"symfony/yaml": "^3.4 || ^4 || ^5 || ^6 || ^7.0",
24-
"justinrainbow/json-schema": "^5.2"
24+
"justinrainbow/json-schema": "^5.2 || ^6.0"
2525
},
2626
"require-dev": {
2727
"cebe/indent": "*",
@@ -30,7 +30,7 @@
3030
"mermade/openapi3-examples": "1.0.0",
3131
"apis-guru/openapi-directory": "1.0.0",
3232
"nexmo/api-specification": "1.0.0",
33-
"phpstan/phpstan": "^0.12.0"
33+
"phpstan/phpstan": "^0.12.0 || ^1.9"
3434
},
3535
"conflict": {
3636
"symfony/yaml": "3.4.0 - 3.4.4 || 4.0.0 - 4.4.17 || 5.0.0 - 5.1.9 || 5.2.0"

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 <[email protected]> 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/IssueTest.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
use cebe\openapi\Reader;
4+
5+
class IssueTest extends \PHPUnit\Framework\TestCase
6+
{
7+
// https://github.com/cebe/php-openapi/issues/175
8+
public function test175UnableToReferenceOtherLocalJsonFile()
9+
{
10+
$openapi = Reader::readFromJsonFile(__DIR__.'/data/issue/175/spec.json');
11+
$this->assertInstanceOf(\cebe\openapi\SpecObjectInterface::class, $openapi);
12+
}
13+
}

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

tests/data/issue/175/401.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"description": "401 response",
3+
"content": {
4+
"application/json": {
5+
"schema": {
6+
"properties": {
7+
"message": {
8+
"type": "string"
9+
}
10+
},
11+
"required": [
12+
"message"
13+
]
14+
},
15+
"example": {
16+
"message": "Unauthenticated."
17+
}
18+
}
19+
}
20+
}

tests/data/issue/175/spec.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "My API",
5+
"version": "1, 2"
6+
},
7+
"paths": {
8+
"/v1/users/profile": {
9+
"get": {
10+
"operationId": "V1GetUserProfile",
11+
"summary": "Returns the user profile",
12+
"responses": {
13+
"401": {
14+
"$ref": "./401.json"
15+
}
16+
}
17+
}
18+
}
19+
}
20+
}

yarn.lock

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
js-tokens "^4.0.0"
2525

2626
"@babel/runtime@^7.0.0", "@babel/runtime@^7.12.5":
27-
version "7.17.2"
28-
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.2.tgz#66f68591605e59da47523c631416b18508779941"
29-
integrity sha512-hzeyJyMA1YGdJTuWU0e/j4wKXrU4OMFvY2MSlaI9B7VQb0r5cxTE3EAIS2Q7Tn2RIcDkRvTA/v2JsAEhxe99uw==
27+
version "7.27.0"
28+
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.27.0.tgz#fbee7cf97c709518ecc1f590984481d5460d4762"
29+
integrity sha512-VtPOkrdPHZsKc/clNqyi9WUA8TINkZ4cGk63UUE3u4pmB2k+ZMQRDuIOagv8UVd6j7k0T3+RRIb7beKTebNbcw==
3030
dependencies:
31-
regenerator-runtime "^0.13.4"
31+
regenerator-runtime "^0.14.0"
3232

3333
"@cloudflare/json-schema-walker@^0.1.1":
3434
version "0.1.1"
@@ -1144,9 +1144,9 @@ polished@^3.0.3:
11441144
"@babel/runtime" "^7.12.5"
11451145

11461146
prismjs@^1.15.0:
1147-
version "1.27.0"
1148-
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.27.0.tgz#bb6ee3138a0b438a3653dd4d6ce0cc6510a45057"
1149-
integrity sha512-t13BGPUlFDR7wRB5kQDG4jjl7XeuH6jbJGt11JHPL96qwsEHNX2+68tFXqc1/k+/jALsbSWJKUOT/hcYAZ5LkA==
1147+
version "1.30.0"
1148+
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.30.0.tgz#d9709969d9d4e16403f6f348c63553b19f0975a9"
1149+
integrity sha512-DEvV2ZF2r2/63V+tK8hQvrR2ZGn10srHbXviTlcv7Kpzw8jWiNTqbVgjO3IY8RxrrOUF8VPMQQFysYYYv0YZxw==
11501150

11511151
process@^0.11.10:
11521152
version "0.11.10"
@@ -1273,10 +1273,10 @@ reftools@^1.1.0, reftools@^1.1.9:
12731273
resolved "https://registry.yarnpkg.com/reftools/-/reftools-1.1.9.tgz#e16e19f662ccd4648605312c06d34e5da3a2b77e"
12741274
integrity sha512-OVede/NQE13xBQ+ob5CKd5KyeJYU2YInb1bmV4nRoOfquZPkAkxuOXicSe1PvqIuZZ4kD13sPKBbR7UFDmli6w==
12751275

1276-
regenerator-runtime@^0.13.4:
1277-
version "0.13.9"
1278-
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52"
1279-
integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==
1276+
regenerator-runtime@^0.14.0:
1277+
version "0.14.1"
1278+
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.14.1.tgz#356ade10263f685dda125100cd862c1db895327f"
1279+
integrity sha512-dYnhHh0nJoMfnkZs6GmmhFknAGRrLznOu5nc9ML+EJxGvrx6H7teuevqVqCuPcPK//3eDrrjQhehXVx9cnkGdw==
12801280

12811281
require-directory@^2.1.1:
12821282
version "2.1.1"

0 commit comments

Comments
 (0)