Skip to content

Commit b59b697

Browse files
[Hotfix][Issue-79] Avoid serialization when resolving servers (#80)
* add tests to ensure there is no regression * access the server property directly * fix heredoc issue in data providers * redefine yaml strings with heredoc Co-authored-by: Dmitriy Lezhnev <[email protected]>
1 parent 2284946 commit b59b697

File tree

2 files changed

+208
-13
lines changed

2 files changed

+208
-13
lines changed

src/PSR7/PathFinder.php

+4-13
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use cebe\openapi\spec\PathItem;
99
use cebe\openapi\spec\Server;
1010
use const PHP_URL_PATH;
11-
use function array_key_exists;
1211
use function count;
1312
use function ltrim;
1413
use function parse_url;
@@ -154,24 +153,16 @@ private function findServersForOperation(OperationAddress $opAddress) : array
154153
$operation = $path->getOperations()[$opAddress->method()];
155154

156155
// 1. Check servers on operation level
157-
if (array_key_exists('servers', (array) $operation->getSerializableData()) &&
158-
count($operation->servers) > 0) {
156+
if (isset($operation->servers) && count($operation->servers) > 0) {
159157
return $operation->servers;
160158
}
161159

162160
// 2. Check servers on path level
163-
if (array_key_exists('servers', (array) $path->getSerializableData()) &&
164-
count($path->servers) > 0) {
161+
if (isset($path->servers) && count($path->servers) > 0) {
165162
return $path->servers;
166163
}
167164

168-
// 3. Check servers on root level
169-
if (array_key_exists('servers', (array) $this->openApiSpec->getSerializableData()) &&
170-
count($this->openApiSpec->servers) > 0) {
171-
return $this->openApiSpec->servers;
172-
}
173-
174-
// fallback
175-
return [new Server(['url' => '/'])];
165+
// 3. Fallback with servers on root level
166+
return $this->openApiSpec->servers;
176167
}
177168
}

tests/FromCommunity/Issue79Test.php

+204
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace League\OpenAPIValidation\Tests\FromCommunity;
6+
7+
use cebe\openapi\Reader;
8+
use League\OpenAPIValidation\PSR7\PathFinder;
9+
use PHPUnit\Framework\TestCase;
10+
11+
/**
12+
* @see https://github.com/thephpleague/openapi-psr7-validator/issues/79
13+
*/
14+
final class Issue79Test extends TestCase
15+
{
16+
/**
17+
* @dataProvider provideSpecAndOperationToMatch()
18+
*/
19+
public function testItFindsMatchingOperationWithTheRightServer(
20+
string $spec,
21+
string $path,
22+
string $method,
23+
string $expectedPath
24+
) : void {
25+
$pathFinder = new PathFinder(Reader::readFromYaml($spec), $path, $method);
26+
$opAddrs = $pathFinder->search();
27+
28+
$this->assertCount(1, $opAddrs);
29+
$this->assertEquals($expectedPath, $opAddrs[0]->path());
30+
}
31+
32+
/**
33+
* @return string[]
34+
*/
35+
public function provideSpecAndOperationToMatch() : iterable
36+
{
37+
yield 'Server override on the operation level' => [
38+
<<<YAML
39+
openapi: "3.0.0"
40+
info:
41+
title: Uber API
42+
description: Move your app forward with the Uber API
43+
version: "1.0.0"
44+
servers:
45+
- url: /v1
46+
paths:
47+
/products/{id}:
48+
servers:
49+
- url: /v2
50+
get:
51+
summary: Product Types
52+
servers:
53+
- url: /v3
54+
YAML
55+
,
56+
'/v3/products/10',
57+
'get',
58+
'/products/{id}',
59+
];
60+
61+
yield 'Server override on the path level' => [
62+
<<<YAML
63+
openapi: "3.0.0"
64+
info:
65+
title: Uber API
66+
description: Move your app forward with the Uber API
67+
version: "1.0.0"
68+
servers:
69+
- url: /v1
70+
paths:
71+
/products/{id}:
72+
servers:
73+
- url: /v2
74+
get:
75+
summary: Product Types
76+
YAML
77+
,
78+
'/v2/products/10',
79+
'get',
80+
'/products/{id}',
81+
];
82+
83+
yield 'Server from the root level' => [
84+
<<<YAML
85+
openapi: "3.0.0"
86+
info:
87+
title: Uber API
88+
description: Move your app forward with the Uber API
89+
version: "1.0.0"
90+
servers:
91+
- url: /v1
92+
paths:
93+
/products/{id}:
94+
get:
95+
summary: Product Types
96+
YAML
97+
,
98+
'/v1/products/10',
99+
'get',
100+
'/products/{id}',
101+
];
102+
103+
yield 'Default server' => [
104+
<<<YAML
105+
openapi: "3.0.0"
106+
info:
107+
title: Uber API
108+
description: Move your app forward with the Uber API
109+
version: "1.0.0"
110+
paths:
111+
/products/{id}:
112+
get:
113+
summary: Product Types
114+
YAML
115+
,
116+
'/products/10',
117+
'get',
118+
'/products/{id}',
119+
];
120+
}
121+
122+
/**
123+
* @dataProvider provideSpecAndOperationToNotMatch()
124+
*/
125+
public function testItDoesNotFindMatchingOperationWithTheWrongServer(
126+
string $spec,
127+
string $path,
128+
string $method
129+
) : void {
130+
$pathFinder = new PathFinder(Reader::readFromYaml($spec), $path, $method);
131+
$opAddrs = $pathFinder->search();
132+
133+
$this->assertCount(0, $opAddrs);
134+
}
135+
136+
/**
137+
* @return string[]
138+
*/
139+
public function provideSpecAndOperationToNotMatch() : iterable
140+
{
141+
yield 'Server override on the operation level' => [
142+
<<<YAML
143+
openapi: "3.0.0"
144+
info:
145+
title: Uber API
146+
description: Move your app forward with the Uber API
147+
version: "1.0.0"
148+
servers:
149+
- url: /v1
150+
paths:
151+
/products/{id}:
152+
servers:
153+
- url: /v2
154+
get:
155+
summary: Product Types
156+
servers:
157+
- url: /v3
158+
YAML
159+
,
160+
'/v2/products/10',
161+
'get',
162+
];
163+
164+
yield 'Server override on the path level' => [
165+
<<<YAML
166+
openapi: "3.0.0"
167+
info:
168+
title: Uber API
169+
description: Move your app forward with the Uber API
170+
version: "1.0.0"
171+
servers:
172+
- url: /v1
173+
paths:
174+
/products/{id}:
175+
servers:
176+
- url: /v2
177+
get:
178+
summary: Product Types
179+
YAML
180+
,
181+
'/v1/products/10',
182+
'get',
183+
];
184+
185+
yield 'Server from the root level' => [
186+
<<<YAML
187+
openapi: "3.0.0"
188+
info:
189+
title: Uber API
190+
description: Move your app forward with the Uber API
191+
version: "1.0.0"
192+
servers:
193+
- url: /v1
194+
paths:
195+
/products/{id}:
196+
get:
197+
summary: Product Types
198+
YAML
199+
,
200+
'/products/10',
201+
'get',
202+
];
203+
}
204+
}

0 commit comments

Comments
 (0)