-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Multiple responses for same code in openapi (#138)
- Loading branch information
1 parent
0eb4d68
commit a581bc7
Showing
3 changed files
with
130 additions
and
27 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tomaj\NetteApi\Test\Handler; | ||
|
||
use Tomaj\NetteApi\Handlers\BaseHandler; | ||
use Tomaj\NetteApi\Output\JsonOutput; | ||
use Tomaj\NetteApi\Response\JsonApiResponse; | ||
use Tomaj\NetteApi\Response\ResponseInterface; | ||
|
||
class MultipleOutputTestHandler extends BaseHandler | ||
{ | ||
public function summary(): string | ||
{ | ||
return 'Multiple output test handler'; | ||
} | ||
|
||
public function description(): string | ||
{ | ||
return 'This API handler is for test multiple '; | ||
} | ||
|
||
public function handle(array $params): ResponseInterface | ||
{ | ||
return new JsonApiResponse(200, ['hello' => 'world']); | ||
} | ||
|
||
public function outputs(): array | ||
{ | ||
return [ | ||
new JsonOutput(200, '{"type": "object"}'), | ||
new JsonOutput(200, '{"type": "string"}'), | ||
]; | ||
} | ||
} |
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,54 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tomaj\NetteApi\Test\Handler; | ||
|
||
use Nette\Application\LinkGenerator; | ||
use Nette\Application\Routers\SimpleRouter; | ||
use Nette\Http\UrlScript; | ||
use PHPUnit\Framework\TestCase; | ||
use Tomaj\NetteApi\ApiDecider; | ||
use Tomaj\NetteApi\Authorization\NoAuthorization; | ||
use Tomaj\NetteApi\EndpointIdentifier; | ||
use Tomaj\NetteApi\Handlers\OpenApiHandler; | ||
use Tomaj\NetteApi\Link\ApiLink; | ||
use Nette\Http\Request; | ||
|
||
class OpenApiHandlerTest extends TestCase | ||
{ | ||
public function testHandlerWithMultipleResponseSchemas() | ||
{ | ||
$linkGenerator = new LinkGenerator(new SimpleRouter([]), new UrlScript('http://test/')); | ||
$apiLink = new ApiLink($linkGenerator); | ||
$request = new Request(new UrlScript('http://test/')); | ||
|
||
$apiDecider = new ApiDecider(); | ||
$apiDecider->addApi( | ||
new EndpointIdentifier('GET', 1, 'test'), | ||
new MultipleOutputTestHandler(), | ||
new NoAuthorization() | ||
); | ||
|
||
$apiDecider->addApi( | ||
new EndpointIdentifier('GET', 1, 'docs', 'open-api'), | ||
new OpenApiHandler($apiDecider, $apiLink, $request), | ||
new NoAuthorization() | ||
); | ||
|
||
$result = $apiDecider->getApi('GET', 1, 'docs', 'open-api'); | ||
$handler = $result->getHandler(); | ||
|
||
$response = $handler->handle(['format' => 'json']); | ||
$this->assertEquals(200, $response->getCode()); | ||
$payload = $response->getPayload(); | ||
|
||
$this->assertEquals(2, count($payload['paths'])); | ||
|
||
$def = array_values($payload['paths'])[0]; // MultipleOutputTestHandler | ||
$this->assertEquals(2, count($def['get']['responses'][200]['content']['application/json; charset=utf-8']['schema']['oneOf'])); | ||
|
||
$def = array_values($payload['paths'])[1]; // OpenApiHandler | ||
$this->assertFalse(isset($def['get']['responses'][200]['content']['application/json; charset=utf-8']['schema']['oneOf'])); | ||
} | ||
} |