Skip to content

Commit 7457351

Browse files
committed
add test to verify reading of YAML tags
make sure it is able to read specs written with traits/mixins as discussed in https://github.com/OAI/OpenAPI-Specification/issues/613#issuecomment-203673611
1 parent 7403ab2 commit 7457351

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

tests/ReaderTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,47 @@ public function testReadYaml()
3636
$this->assertApiContent($openapi);
3737
}
3838

39+
/**
40+
* Test if reading YAML file with anchors works
41+
*/
42+
public function testReadYamlWithAnchors()
43+
{
44+
$openApiFile = __DIR__ . '/spec/data/traits-mixins.yaml';
45+
$openapi = \cebe\openapi\Reader::readFromYamlFile($openApiFile);
46+
47+
$this->assertApiContent($openapi);
48+
49+
$putOperation = $openapi->paths['/foo']->put;
50+
$this->assertEquals('create foo', $putOperation->description);
51+
$this->assertTrue($putOperation->responses->hasResponse('200'));
52+
$this->assertTrue($putOperation->responses->hasResponse('404'));
53+
$this->assertTrue($putOperation->responses->hasResponse('428'));
54+
$this->assertTrue($putOperation->responses->hasResponse('default'));
55+
56+
$respOk = $putOperation->responses->getResponse('200');
57+
$this->assertEquals('request succeeded', $respOk->description);
58+
$this->assertEquals('the request id', $respOk->headers['X-Request-Id']->description);
59+
60+
$resp404 = $putOperation->responses->getResponse('404');
61+
$this->assertEquals('resource not found', $resp404->description);
62+
$this->assertEquals('the request id', $resp404->headers['X-Request-Id']->description);
63+
64+
$resp428 = $putOperation->responses->getResponse('428');
65+
$this->assertEquals('resource not found', $resp428->description);
66+
$this->assertEquals('the request id', $resp428->headers['X-Request-Id']->description);
67+
68+
$respDefault = $putOperation->responses->getResponse('default');
69+
$this->assertEquals('resource not found', $respDefault->description);
70+
$this->assertEquals('the request id', $respDefault->headers['X-Request-Id']->description);
71+
72+
$foo = $openapi->components->schemas['Foo'];
73+
$this->assertArrayHasKey('uuid', $foo->properties);
74+
$this->assertArrayHasKey('name', $foo->properties);
75+
$this->assertArrayHasKey('id', $foo->properties);
76+
$this->assertArrayHasKey('description', $foo->properties);
77+
$this->assertEquals('uuid of the resource', $foo->properties['uuid']->description);
78+
}
79+
3980
private function assertApiContent(\cebe\openapi\spec\OpenApi $openapi)
4081
{
4182
$result = $openapi->validate();

tests/spec/data/traits-mixins.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# test whether we can read YAML files that use mixin feature
2+
x-ResourceCommon: &ResourceCommon
3+
uuid:
4+
type: string
5+
description: uuid of the resource
6+
name:
7+
type: string
8+
description: name of the resource
9+
10+
openapi: 3.0.0
11+
info:
12+
title: Test API
13+
version: 1.0.0
14+
paths:
15+
/foo:
16+
put:
17+
description: create foo
18+
responses:
19+
'200': &RESP_OK
20+
description: request succeeded
21+
headers: &RESP_HEADERS
22+
X-Request-Id:
23+
description: the request id
24+
schema:
25+
$ref: '#/components/schemas/Foo'
26+
'404': &RESP_ERROR
27+
description: resource not found
28+
headers: *RESP_HEADERS
29+
schema:
30+
$ref: "#/components/schemas/Error"
31+
'428': *RESP_ERROR
32+
default: *RESP_ERROR
33+
components:
34+
responses:
35+
Bar:
36+
description: A bar
37+
content:
38+
application/json:
39+
schema: { type: object }
40+
schemas:
41+
Error:
42+
description: This is an error
43+
type: object
44+
properties:
45+
message:
46+
type: string
47+
code:
48+
type: number
49+
format: int64
50+
Foo:
51+
description: A foo resource type
52+
type: object
53+
properties:
54+
<<: *ResourceCommon
55+
id:
56+
type: string
57+
description: id of the foo
58+
description:
59+
type: string
60+
description: description of foo

0 commit comments

Comments
 (0)