Skip to content

Commit eddccee

Browse files
authored
Merge branch 'master' into wip-reference-cache
2 parents 333aa5f + dac8870 commit eddccee

11 files changed

+129
-27
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,21 @@ matrix:
44
include:
55
# linux tests
66
- php: '7.1'
7-
env: YAML=^3.0
7+
env: YAML=^3.4
88
- php: '7.1'
99
env: YAML=~4.3.0
1010
- php: '7.2'
11-
env: YAML=^3.0
11+
env: YAML=^3.4
1212
- php: '7.2'
1313
env: YAML=~4.3.0
1414
- php: '7.3'
15-
env: YAML=^3.0
15+
env: YAML=^3.4
1616
- php: '7.3'
1717
env: YAML=~4.3.0
1818
- php: '7.3'
1919
env: YAML=^5.0
2020
- php: '7.4'
21-
env: YAML=^3.0
21+
env: YAML=^3.4
2222
- php: '7.4'
2323
env: YAML=~4.3.0
2424
- php: '7.4'

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
TESTCASE=
22
XDEBUG=0
3-
PHPARGS=-dmemory_limit=64M
3+
PHPARGS=-dmemory_limit=512M
44
XPHPARGS=
55
ifeq ($(XDEBUG),1)
66
XPHPARGS=-dzend_extension=xdebug.so -dxdebug.remote_enable=1 -dxdebug.remote_autostart=1

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ do awesome work:
2626
- [cebe/yii2-openapi](https://github.com/cebe/yii2-openapi) Code Generator for REST API from OpenAPI 3 Descriptions, includes fake data generator.
2727
- [cebe/yii2-app-api](https://github.com/cebe/yii2-app-api) Yii framework application template for developing API-first applications.
2828
- [league/openapi-psr7-validator](https://github.com/thephpleague/openapi-psr7-validator) validates PSR-7 messages (HTTP request/response) against OpenAPI descriptions.
29+
- [dsuurlant/response2schema](https://github.com/dsuurlant/response2schema) a quick and easy tool for generating OpenAPI schemas based on example data.
2930
- ... ([add yours](https://github.com/cebe/php-openapi/edit/master/README.md#L24))
3031

3132
## Usage
@@ -89,7 +90,7 @@ Read OpenAPI Description from YAML:
8990
use cebe\openapi\Reader;
9091

9192
// realpath is needed for resolving references with relative Paths or URLs
92-
$openapi = Reader::readFromYamlFile(realpath('openapi.json'));
93+
$openapi = Reader::readFromYamlFile(realpath('openapi.yaml'));
9394
// you may also specify the URL to your API Description file
9495
$openapi = Reader::readFromYamlFile('https://raw.githubusercontent.com/OAI/OpenAPI-Specification/3.0.2/examples/v3.0/petstore-expanded.yaml');
9596
```

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"require": {
2121
"php": ">=7.1.0",
2222
"ext-json": "*",
23-
"symfony/yaml": "^3.0 | ~4.0.0 | ~4.1.0 | ~4.2.0 | ~4.3.0 | ^5.0",
23+
"symfony/yaml": "^3.4 | ^4.0 | ^5.0",
2424
"justinrainbow/json-schema": "^5.0"
2525
},
2626
"require-dev": {

src/spec/MediaType.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,13 @@ public function __construct(array $data)
5353
if ($encodingData instanceof Encoding) {
5454
$encoding[$property] = $encodingData;
5555
} elseif (is_array($encodingData)) {
56-
$encoding[$property] = new Encoding($encodingData, $this->schema->properties[$property] ?? null);
56+
$schema = $this->schema->properties[$property] ?? null;
57+
// Don't pass the schema if it's still an unresolved reference.
58+
if ($schema instanceof Reference) {
59+
$encoding[$property] = new Encoding($encodingData);
60+
} else {
61+
$encoding[$property] = new Encoding($encodingData, $schema);
62+
}
5763
} else {
5864
$givenType = gettype($encodingData);
5965
if ($givenType === 'object') {

src/spec/Schema.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@
2626
* @property string $title
2727
* @property int|float $multipleOf
2828
* @property int|float $maximum
29-
* @property int|float $exclusiveMaximum
29+
* @property bool $exclusiveMaximum
3030
* @property int|float $minimum
31-
* @property int|float $exclusiveMinimum
31+
* @property bool $exclusiveMinimum
3232
* @property int $maxLength
3333
* @property int $minLength
3434
* @property string $pattern (This string SHOULD be a valid regular expression, according to the [ECMA 262 regular expression dialect](https://www.ecma-international.org/ecma-262/5.1/#sec-7.8.5))

tests/ReaderTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,12 @@ public function testSymfonyYamlBugHunt()
9696
{
9797
// skip test on symfony/yaml 5.0 due to bug https://github.com/symfony/symfony/issues/34805
9898
$installed = json_decode(file_get_contents(__DIR__ . '/../vendor/composer/installed.json'), true);
99+
// Check for composer 2.0 structure
100+
if (array_key_exists('packages', $installed)) {
101+
$installed = $installed['packages'];
102+
}
99103
foreach($installed as $pkg) {
100-
if ($pkg['name'] === 'symfony/yaml' && strncmp($pkg['version_normalized'], '5.0', 3) === 0) {
104+
if ($pkg['name'] === 'symfony/yaml' && version_compare($pkg['version'], 'v4.4', '>=')) {
101105
$this->markTestSkipped(
102106
'This test is incompatible with symfony/yaml 4.4 and 5.0, see symfony bug https://github.com/symfony/symfony/issues/34805'
103107
);

tests/spec/MediaTypeTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
use cebe\openapi\Reader;
44
use cebe\openapi\spec\MediaType;
55
use cebe\openapi\spec\Example;
6+
use cebe\openapi\spec\OpenApi;
67
use cebe\openapi\spec\Reference;
8+
use Symfony\Component\Yaml\Yaml;
79

810
/**
911
* @covers \cebe\openapi\spec\MediaType
@@ -114,4 +116,54 @@ public function testPathsCanNotBeCreatedFromBullshit($config, $expectedException
114116

115117
new MediaType($config);
116118
}
119+
120+
public function testUnresolvedReferencesInEncoding()
121+
{
122+
$yaml = Yaml::parse(<<<'YAML'
123+
openapi: "3.0.0"
124+
info:
125+
version: 1.0.0
126+
title: Encoding test
127+
paths:
128+
/pets:
129+
post:
130+
summary: Create a pet
131+
operationId: createPets
132+
requestBody:
133+
content:
134+
multipart/form-data:
135+
schema:
136+
type: object
137+
properties:
138+
pet:
139+
$ref: '#/components/schemas/Pet'
140+
petImage:
141+
type: string
142+
format: binary
143+
encoding:
144+
pet:
145+
contentType: application/json
146+
petImage:
147+
contentType: image/*
148+
application/json:
149+
schema:
150+
$ref: '#/components/schemas/Pet'
151+
responses:
152+
'201':
153+
description: Null response
154+
components:
155+
schemas:
156+
Pet:
157+
type: object
158+
properties:
159+
name:
160+
type: string
161+
YAML
162+
);
163+
$openapi = new OpenApi($yaml);
164+
$result = $openapi->validate();
165+
166+
$this->assertEquals([], $openapi->getErrors());
167+
$this->assertTrue($result);
168+
}
117169
}

tests/spec/OpenApiTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,12 @@ public function testSpecs($openApiFile)
186186
// skip test on symfony/yaml 5.0 due to bug https://github.com/symfony/symfony/issues/34805
187187
if ($openApiFile === 'oai/openapi-specification/examples/v3.0/uspto.yaml') {
188188
$installed = json_decode(file_get_contents(__DIR__ . '/../../vendor/composer/installed.json'), true);
189+
// Check for composer 2.0 structure
190+
if (array_key_exists('packages', $installed)) {
191+
$installed = $installed['packages'];
192+
}
189193
foreach ($installed as $pkg) {
190-
if ($pkg['name'] === 'symfony/yaml' && strncmp($pkg['version_normalized'], '5.0', 3) === 0) {
194+
if ($pkg['name'] === 'symfony/yaml' && version_compare($pkg['version'], 'v4.4', '>=')) {
191195
$this->markTestSkipped(
192196
'This test is incompatible with symfony/yaml 4.4 and 5.0, see symfony bug https://github.com/symfony/symfony/issues/34805'
193197
);

tests/spec/SchemaTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,4 +320,39 @@ public function testRefAdditionalProperties()
320320
$schema = new Schema(['additionalProperties' => new Reference(['$ref' => '#/here'], Schema::class)]);
321321
$this->assertInstanceOf(Reference::class, $schema->additionalProperties);
322322
}
323+
324+
/**
325+
* Ensure that a property named "$ref" is not interpreted as a reference.
326+
* @link https://github.com/OAI/OpenAPI-Specification/issues/2173
327+
*/
328+
public function testPropertyNameRef()
329+
{
330+
$json = <<<'JSON'
331+
{
332+
"components": {
333+
"schemas": {
334+
"person": {
335+
"type": "object",
336+
"properties": {
337+
"name": {
338+
"type": "string"
339+
},
340+
"$ref": {
341+
"type": "string"
342+
}
343+
}
344+
}
345+
}
346+
}
347+
}
348+
JSON;
349+
$openApi = Reader::readFromJson($json);
350+
$this->assertInstanceOf(Schema::class, $person = $openApi->components->schemas['person']);
351+
352+
$this->assertEquals(['name', '$ref'], array_keys($person->properties));
353+
$this->assertInstanceOf(Schema::class, $person->properties['name']);
354+
$this->assertInstanceOf(Schema::class, $person->properties['$ref']);
355+
$this->assertEquals('string', $person->properties['name']->type);
356+
$this->assertEquals('string', $person->properties['$ref']->type);
357+
}
323358
}

yarn.lock

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ classnames@^2.2.0, classnames@^2.2.3, classnames@^2.2.6:
164164
integrity sha512-JR/iSQOSt+LQIWwrwEzJ9uk0xfN3mTVYMwt1Ir5mUcSN6pU+V4zQFFaJsclJbPuAUQH+yfWef6tm7l1quW3C8Q==
165165

166166
clipboard@^2.0.0:
167-
version "2.0.4"
168-
resolved "https://registry.yarnpkg.com/clipboard/-/clipboard-2.0.4.tgz#836dafd66cf0fea5d71ce5d5b0bf6e958009112d"
169-
integrity sha512-Vw26VSLRpJfBofiVaFb/I8PVfdI1OxKcYShe6fm0sP/DtmiWQNCjhM/okTvdCo0G+lMMm1rMYbk4IK4x1X+kgQ==
167+
version "2.0.6"
168+
resolved "https://registry.yarnpkg.com/clipboard/-/clipboard-2.0.6.tgz#52921296eec0fdf77ead1749421b21c968647376"
169+
integrity sha512-g5zbiixBRk/wyKakSwCKd7vQXDjFnAMGHoEyBogG/bw9kTD9GvdAvaoRR1ALcEzt3pVKxZR0pViekPMIS0QyGg==
170170
dependencies:
171171
good-listener "^1.2.2"
172172
select "^1.1.2"
@@ -557,9 +557,9 @@ [email protected]:
557557
integrity sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==
558558

559559
ini@^1.3.0:
560-
version "1.3.5"
561-
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.5.tgz#eee25f56db1c9ec6085e0c22778083f596abf927"
562-
integrity sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw==
560+
version "1.3.7"
561+
resolved "https://registry.yarnpkg.com/ini/-/ini-1.3.7.tgz#a09363e1911972ea16d7a8851005d84cf09a9a84"
562+
integrity sha512-iKpRpXP+CrP2jyrxvg1kMUpXDyRUFDWurxbnVT1vQPx+Wz9uCYsMIqYuSBLV+PAaZG/d7kRLKRFc9oDMsH+mFQ==
563563

564564
invert-kv@^1.0.0:
565565
version "1.0.0"
@@ -783,9 +783,9 @@ min-document@^2.19.0:
783783
dom-walk "^0.1.0"
784784

785785
minimist@^1.2.0:
786-
version "1.2.0"
787-
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.0.tgz#a35008b20f41383eec1fb914f4cd5df79a264284"
788-
integrity sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=
786+
version "1.2.5"
787+
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.5.tgz#67d66014b66a6a8aaa0c083c5fd58df4e4e97602"
788+
integrity sha512-FM9nNUYrRBAELZQT3xeZQ7fmMOBg6nWNmJKTcgsJeaLstP/UODVpGsr5OhXhhXg6f+qtJ8uiZ+PUxkDWcgIXLw==
789789

790790
mobx-react@^5.4.3:
791791
version "5.4.4"
@@ -840,9 +840,9 @@ node-fetch-h2@^2.3.0:
840840
http2-client "^1.2.5"
841841

842842
node-fetch@^2.3.0:
843-
version "2.6.0"
844-
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.0.tgz#e633456386d4aa55863f676a7ab0daa8fdecb0fd"
845-
integrity sha512-8dG4H5ujfvFiqDmVu9fQ5bOHUC15JMjMY/Zumv26oOvvVJjM67KF8koCWIabKQ1GJIa9r2mMZscBq/TbdOcmNA==
843+
version "2.6.1"
844+
resolved "https://registry.yarnpkg.com/node-fetch/-/node-fetch-2.6.1.tgz#045bd323631f76ed2e2b55573394416b639a0052"
845+
integrity sha512-V4aYg89jEoVRxRb2fJdAg8FHvI7cEyYdVAh94HH0UIK8oJxUfkjlDQN9RbMx+bEjP7+ggMiFRprSti032Oipxw==
846846

847847
node-readfiles@^0.2.0:
848848
version "0.2.0"
@@ -1026,9 +1026,9 @@ polished@^3.0.3:
10261026
"@babel/runtime" "^7.4.5"
10271027

10281028
prismjs@^1.15.0:
1029-
version "1.17.1"
1030-
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.17.1.tgz#e669fcbd4cdd873c35102881c33b14d0d68519be"
1031-
integrity sha512-PrEDJAFdUGbOP6xK/UsfkC5ghJsPJviKgnQOoxaDbBjwc8op68Quupwt1DeAFoG8GImPhiKXAvvsH7wDSLsu1Q==
1029+
version "1.21.0"
1030+
resolved "https://registry.yarnpkg.com/prismjs/-/prismjs-1.21.0.tgz#36c086ec36b45319ec4218ee164c110f9fc015a3"
1031+
integrity sha512-uGdSIu1nk3kej2iZsLyDoJ7e9bnPzIgY0naW/HdknGj61zScaprVEVGHrPoXqI+M9sP0NDnTK2jpkvmldpuqDw==
10321032
optionalDependencies:
10331033
clipboard "^2.0.0"
10341034

0 commit comments

Comments
 (0)