Skip to content

Commit

Permalink
add test to verify reading of YAML tags
Browse files Browse the repository at this point in the history
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
  • Loading branch information
cebe committed Dec 4, 2019
1 parent 7403ab2 commit 7457351
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
41 changes: 41 additions & 0 deletions tests/ReaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,47 @@ public function testReadYaml()
$this->assertApiContent($openapi);
}

/**
* Test if reading YAML file with anchors works
*/
public function testReadYamlWithAnchors()
{
$openApiFile = __DIR__ . '/spec/data/traits-mixins.yaml';
$openapi = \cebe\openapi\Reader::readFromYamlFile($openApiFile);

$this->assertApiContent($openapi);

$putOperation = $openapi->paths['/foo']->put;
$this->assertEquals('create foo', $putOperation->description);
$this->assertTrue($putOperation->responses->hasResponse('200'));
$this->assertTrue($putOperation->responses->hasResponse('404'));
$this->assertTrue($putOperation->responses->hasResponse('428'));
$this->assertTrue($putOperation->responses->hasResponse('default'));

$respOk = $putOperation->responses->getResponse('200');
$this->assertEquals('request succeeded', $respOk->description);
$this->assertEquals('the request id', $respOk->headers['X-Request-Id']->description);

$resp404 = $putOperation->responses->getResponse('404');
$this->assertEquals('resource not found', $resp404->description);
$this->assertEquals('the request id', $resp404->headers['X-Request-Id']->description);

$resp428 = $putOperation->responses->getResponse('428');
$this->assertEquals('resource not found', $resp428->description);
$this->assertEquals('the request id', $resp428->headers['X-Request-Id']->description);

$respDefault = $putOperation->responses->getResponse('default');
$this->assertEquals('resource not found', $respDefault->description);
$this->assertEquals('the request id', $respDefault->headers['X-Request-Id']->description);

$foo = $openapi->components->schemas['Foo'];
$this->assertArrayHasKey('uuid', $foo->properties);
$this->assertArrayHasKey('name', $foo->properties);
$this->assertArrayHasKey('id', $foo->properties);
$this->assertArrayHasKey('description', $foo->properties);
$this->assertEquals('uuid of the resource', $foo->properties['uuid']->description);
}

private function assertApiContent(\cebe\openapi\spec\OpenApi $openapi)
{
$result = $openapi->validate();
Expand Down
60 changes: 60 additions & 0 deletions tests/spec/data/traits-mixins.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# test whether we can read YAML files that use mixin feature
x-ResourceCommon: &ResourceCommon
uuid:
type: string
description: uuid of the resource
name:
type: string
description: name of the resource

openapi: 3.0.0
info:
title: Test API
version: 1.0.0
paths:
/foo:
put:
description: create foo
responses:
'200': &RESP_OK
description: request succeeded
headers: &RESP_HEADERS
X-Request-Id:
description: the request id
schema:
$ref: '#/components/schemas/Foo'
'404': &RESP_ERROR
description: resource not found
headers: *RESP_HEADERS
schema:
$ref: "#/components/schemas/Error"
'428': *RESP_ERROR
default: *RESP_ERROR
components:
responses:
Bar:
description: A bar
content:
application/json:
schema: { type: object }
schemas:
Error:
description: This is an error
type: object
properties:
message:
type: string
code:
type: number
format: int64
Foo:
description: A foo resource type
type: object
properties:
<<: *ResourceCommon
id:
type: string
description: id of the foo
description:
type: string
description: description of foo

0 comments on commit 7457351

Please sign in to comment.