Skip to content

Commit 4018163

Browse files
committed
fix support for references in Responses object
1 parent c4e639d commit 4018163

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

src/spec/OpenApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* @property-read string $openapi
1919
* @property-read Info $info
2020
* @property-read Server[] $servers
21-
* @property-read Paths $paths
21+
* @property-read Paths|PathItem[] $paths
2222
* @property-read Components|null $components
2323
* @property-read SecurityRequirement[] $security
2424
* @property-read Tag[] $tags

src/spec/Responses.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public function __construct(array $data)
4242
// From Spec: This field MUST be enclosed in quotation marks (for example, "200") for compatibility between JSON and YAML.
4343
$statusCode = (string) $statusCode;
4444
if (preg_match('~^(?:default|[1-5](?:[0-9][0-9]|XX))$~', $statusCode)) {
45-
$this->_responses[$statusCode] = new Response($response);
45+
if (isset($response['$ref'])) {
46+
$this->_responses[$statusCode] = new Reference($response, Response::class);
47+
} else {
48+
$this->_responses[$statusCode] = new Response($response);
49+
}
4650
} else {
4751
$this->_errors[] = "Responses: $statusCode is not a valid HTTP status code.";
4852
}
@@ -60,15 +64,15 @@ public function hasResponse($statusCode): bool
6064

6165
/**
6266
* @param string $statusCode HTTP status code
63-
* @return PathItem
67+
* @return Response|Reference
6468
*/
65-
public function getResponse($statusCode): ?Response
69+
public function getResponse($statusCode)
6670
{
6771
return $this->_responses[$statusCode] ?? null;
6872
}
6973

7074
/**
71-
* @return Response[]
75+
* @return Response[]|Reference[]
7276
*/
7377
public function getResponses(): array
7478
{

tests/spec/ReferenceTest.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use cebe\openapi\Reader;
44
use cebe\openapi\spec\OpenApi;
55
use cebe\openapi\spec\Reference;
6+
use cebe\openapi\spec\Response;
67
use cebe\openapi\spec\Schema;
78
use cebe\openapi\spec\Example;
89

@@ -29,6 +30,9 @@ public function testResolveInDocument()
2930
examples:
3031
frog-example:
3132
description: a frog
33+
responses:
34+
Pet:
35+
description: returns a pet
3236
paths:
3337
'/pet':
3438
get:
@@ -42,25 +46,33 @@ public function testResolveInDocument()
4246
examples:
4347
frog:
4448
$ref: "#/components/examples/frog-example"
49+
'/pet/1':
50+
get:
51+
responses:
52+
200:
53+
$ref: "#/components/responses/Pet"
4554
YAML
4655
, OpenApi::class);
4756

4857
$result = $openapi->validate();
4958
$this->assertEquals([], $openapi->getErrors());
5059
$this->assertTrue($result);
5160

52-
/** @var $response \cebe\openapi\spec\Response */
53-
$response = $openapi->paths->getPath('/pet')->get->responses['200'];
54-
$this->assertInstanceOf(Reference::class, $response->content['application/json']->schema);
55-
$this->assertInstanceOf(Reference::class, $response->content['application/json']->examples['frog']);
61+
/** @var $petResponse Response */
62+
$petResponse = $openapi->paths->getPath('/pet')->get->responses['200'];
63+
$this->assertInstanceOf(Reference::class, $petResponse->content['application/json']->schema);
64+
$this->assertInstanceOf(Reference::class, $petResponse->content['application/json']->examples['frog']);
65+
$this->assertInstanceOf(Reference::class, $openapi->paths->getPath('/pet/1')->get->responses['200']);
5666

5767
$openapi->resolveReferences(new \cebe\openapi\ReferenceContext($openapi, 'file:///tmp/openapi.yaml'));
5868

59-
$this->assertInstanceOf(Schema::class, $refSchema = $response->content['application/json']->schema);
60-
$this->assertInstanceOf(Example::class, $refExample = $response->content['application/json']->examples['frog']);
69+
$this->assertInstanceOf(Schema::class, $refSchema = $petResponse->content['application/json']->schema);
70+
$this->assertInstanceOf(Example::class, $refExample = $petResponse->content['application/json']->examples['frog']);
71+
$this->assertInstanceOf(Response::class, $refResponse = $openapi->paths->getPath('/pet/1')->get->responses['200']);
6172

6273
$this->assertSame($openapi->components->schemas['Pet'], $refSchema);
6374
$this->assertSame($openapi->components->examples['frog-example'], $refExample);
75+
$this->assertSame($openapi->components->responses['Pet'], $refResponse);
6476
}
6577

6678
public function testResolveCyclicReferenceInDocument()
@@ -105,7 +117,7 @@ public function testResolveCyclicReferenceInDocument()
105117
$this->assertEquals([], $openapi->getErrors());
106118
$this->assertTrue($result);
107119

108-
/** @var $response \cebe\openapi\spec\Response */
120+
/** @var $response Response */
109121
$response = $openapi->paths->getPath('/pet')->get->responses['200'];
110122
$this->assertInstanceOf(Reference::class, $response->content['application/json']->schema);
111123
$this->assertInstanceOf(Reference::class, $response->content['application/json']->examples['frog']);

0 commit comments

Comments
 (0)