Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Adding more complex types found in spec
Browse files Browse the repository at this point in the history
  • Loading branch information
kleijnweb committed Aug 3, 2018
1 parent 0396dca commit 7b86550
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
11 changes: 10 additions & 1 deletion src/Description/Builder/OpenApiBuilder.php
Expand Up @@ -49,7 +49,7 @@ public function build(): Description

$description->accept(new ClosureVisitor($this, function ($schema) use (&$typeDefinitions) {
if ($schema instanceof ObjectSchema) {
if ($schema->isType(Schema::TYPE_OBJECT) && isset($schema->getDefinition()->{'x-ref-id'})) {
if (isset($schema->getDefinition()->{'x-ref-id'})) {
$typeName = substr(
$schema->getDefinition()->{'x-ref-id'},
strrpos($schema->getDefinition()->{'x-ref-id'}, '/') + 1
Expand All @@ -60,6 +60,15 @@ public function build(): Description
}
}));

$this->document->apply(function ($composite, $attribute, $parent, $parentAttribute) use (&$typeDefinitions) {
if ($parentAttribute === 'definitions') {
$schema = $this->schemaFactory->create($composite);
if ($schema instanceof ObjectSchema) {
$typeDefinitions[$attribute] = $schema;
}
}
});

foreach ($typeDefinitions as $name => $schema) {
$type = new ComplexType($name, $schema);
$schema->setComplexType($type);
Expand Down
44 changes: 44 additions & 0 deletions tests/Description/Builder/OpenApi/DefinitionsBuilderTest.php
@@ -0,0 +1,44 @@
<?php declare(strict_types=1);
/*
* This file is part of the KleijnWeb\PhpApi\Descriptions package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace KleijnWeb\PhpApi\Descriptions\Tests\Description\Builder\OpenApi;

use KleijnWeb\PhpApi\Descriptions\Description\ComplexType;
use KleijnWeb\PhpApi\Descriptions\Tests\Description\Builder\OpenApiBuilderTest;

/**
* @author John Kleijn <john@kleijnweb.nl>
*/
class DefinitionsBuilderTest extends OpenApiBuilderTest
{
protected function setUp()
{
$this->setUpDescription(__DIR__ . '/../../../definitions/openapi/definitions.yml');
}

/**
* @test
*/
public function willAddAllSchemaUnderDefinitionsAsComplexTypes()
{
$typeNames = array_map(function (ComplexType $type) {
return $type->getName();
}, $this->description->getComplexTypes());

sort($typeNames);

$expected = [
'Bar',
'Baz',
'Foo',
'UnusedDefinition',
];

$this->assertSame($expected, $typeNames);
}
}
35 changes: 35 additions & 0 deletions tests/definitions/openapi/definitions.yml
@@ -0,0 +1,35 @@
swagger: '2.0'
info:
description: 'Data api'
version: 1.0.0
basePath: /data/v1
paths:
/baz:
get:
responses:
'200':
schema: { $ref: '#/definitions/Baz' }

definitions:
Foo:
type: object
properties:
fooProperty: { type: string }
Bar:
type: object
properties:
barProperty: { type: string }

Baz:
allOf:
- { $ref: '#/definitions/Foo' }
- { $ref: '#/definitions/Bar' }
- properties:
bazProperty: { type: string }

StringDefinition:
type: string

UnusedDefinition:
properties:
barProperty: { type: string }

0 comments on commit 7b86550

Please sign in to comment.