-
Notifications
You must be signed in to change notification settings - Fork 937
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add resolving references of components (#1544)
- Loading branch information
Showing
8 changed files
with
172 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Processors; | ||
|
||
use OpenApi\Analysis; | ||
use OpenApi\Annotations as OA; | ||
use OpenApi\Generator; | ||
|
||
/** | ||
* Use the RequestBody context to extract useful information and inject that into the annotation. | ||
*/ | ||
class AugmentRequestBody implements ProcessorInterface | ||
{ | ||
public function __invoke(Analysis $analysis) | ||
{ | ||
/** @var array<OA\RequestBody> $requests */ | ||
$requests = $analysis->getAnnotationsOfType(OA\RequestBody::class); | ||
|
||
$this->augmentRequestBody($requests); | ||
} | ||
|
||
/** | ||
* @param array<OA\RequestBody> $requests | ||
*/ | ||
protected function augmentRequestBody(array $requests): void | ||
{ | ||
foreach ($requests as $request) { | ||
if (!$request->isRoot(OA\RequestBody::class)) { | ||
continue; | ||
} | ||
if (Generator::isDefault($request->request)) { | ||
if ($request->_context->is('class')) { | ||
$request->request = $request->_context->class; | ||
} elseif ($request->_context->is('interface')) { | ||
$request->request = $request->_context->interface; | ||
} elseif ($request->_context->is('trait')) { | ||
$request->request = $request->_context->trait; | ||
} elseif ($request->_context->is('enum')) { | ||
$request->request = $request->_context->enum; | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
// NOTE: this file uses "\r\n" linebreaks on purpose | ||
|
||
namespace OpenApi\Tests\Fixtures; | ||
|
||
use OpenApi\Annotations as OA; | ||
|
||
/** | ||
* @OA\RequestBody | ||
*/ | ||
class Request | ||
{ | ||
/** | ||
* @OA\Post( | ||
* path="/", | ||
* @OA\RequestBody(ref=OpenApi\Tests\Fixtures\Request::class), | ||
* @OA\Response(response="200", description="An example resource") | ||
* ) | ||
*/ | ||
public function post() | ||
{ | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Tests\Processors; | ||
|
||
use OpenApi\Processors\AugmentRefs; | ||
use OpenApi\Processors\AugmentRequestBody; | ||
use OpenApi\Processors\BuildPaths; | ||
use OpenApi\Processors\Concerns\DocblockTrait; | ||
use OpenApi\Processors\MergeIntoComponents; | ||
use OpenApi\Processors\MergeIntoOpenApi; | ||
use OpenApi\Tests\OpenApiTestCase; | ||
|
||
class AugmentRefsTest extends OpenApiTestCase | ||
{ | ||
use DocblockTrait; | ||
|
||
public function testAugmentRefsForRequestBody(): void | ||
{ | ||
$analysis = $this->analysisFromFixtures(['Request.php']); | ||
$analysis->process([ | ||
// create openapi->components | ||
new MergeIntoOpenApi(), | ||
// Merge standalone Scheme's into openapi->components | ||
new MergeIntoComponents(), | ||
new BuildPaths(), | ||
new AugmentRequestBody(), | ||
]); | ||
|
||
$this->assertSame($analysis->openapi->paths[0]->post->requestBody->ref, 'OpenApi\Tests\Fixtures\Request'); | ||
|
||
$analysis->process([ | ||
new AugmentRefs(), | ||
]); | ||
|
||
$this->assertSame($analysis->openapi->paths[0]->post->requestBody->ref, '#/components/requestBodies/Request'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** | ||
* @license Apache 2.0 | ||
*/ | ||
|
||
namespace OpenApi\Tests\Processors; | ||
|
||
use OpenApi\Generator; | ||
use OpenApi\Processors\AugmentRequestBody; | ||
use OpenApi\Processors\MergeIntoComponents; | ||
use OpenApi\Processors\MergeIntoOpenApi; | ||
use OpenApi\Tests\OpenApiTestCase; | ||
|
||
class AugmentRequestBodyTest extends OpenApiTestCase | ||
{ | ||
public function testAugmentSchemas(): void | ||
{ | ||
$analysis = $this->analysisFromFixtures(['Request.php']); | ||
$analysis->process([ | ||
// create openapi->components | ||
new MergeIntoOpenApi(), | ||
// Merge standalone Scheme's into openapi->components | ||
new MergeIntoComponents(), | ||
]); | ||
|
||
$this->assertCount(1, $analysis->openapi->components->requestBodies); | ||
$request = $analysis->openapi->components->requestBodies[0]; | ||
$this->assertSame(Generator::UNDEFINED, $request->request, 'Sanity check. No request was defined'); | ||
$analysis->process([new AugmentRequestBody()]); | ||
|
||
$this->assertSame('Request', $request->request, '@OA\RequestBody()->request based on classname'); | ||
} | ||
} |