Skip to content

Commit

Permalink
Merge pull request #48 from domiTEN/fix/responsebody-without-content
Browse files Browse the repository at this point in the history
Fix match ResponseBody with $ref and no content
  • Loading branch information
byjg committed Apr 23, 2020
2 parents b688238 + f842c11 commit e2c0b34
Show file tree
Hide file tree
Showing 4 changed files with 150 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/OpenApi/OpenApiResponseBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ class OpenApiResponseBody extends Body
*/
public function match($body)
{
if (empty($this->structure['content'])) {
if (empty($this->structure['content']) && !isset($this->structure['$ref'])) {
if (!empty($body)) {
throw new NotMatchedException("Expected empty body for " . $this->name);
}
return true;
}

if(!isset($this->structure['content']) && isset($this->structure['$ref'])){
$defintion = $this->schema->getDefinition($this->structure['$ref']);
return $this->matchSchema($this->name, $defintion, $body);
}

return $this->matchSchema($this->name, $this->structure['content'][key($this->structure['content'])]['schema'], $body);
}
}
20 changes: 20 additions & 0 deletions tests/OpenApiBodyTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,18 @@ protected static function openApiSchema3($allowNullValues = false)
);
}

/**
* @param bool $allowNullValues
* @return OpenApiSchema
*/
protected static function openApiSchema5($allowNullValues = false)
{
return \ByJG\ApiTools\Base\Schema::getInstance(
self::getOpenApiJsonContent_No5(),
$allowNullValues
);
}

/**
* @return string
*/
Expand All @@ -66,4 +78,12 @@ protected static function getOpenApiJsonContent_No3()
{
return file_get_contents(__DIR__ . '/example/openapi3.json');
}

/**
* @return string
*/
protected static function getOpenApiJsonContent_No5()
{
return file_get_contents(__DIR__ . '/example/openapi5.json');
}
}
22 changes: 22 additions & 0 deletions tests/OpenApiResponseBodyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,28 @@ public function testMatchResponseBody()
$this->assertTrue($responseParameter->match($body));
}

/**
* @throws \ByJG\ApiTools\Exception\DefinitionNotFoundException
* @throws \ByJG\ApiTools\Exception\GenericSwaggerException
* @throws \ByJG\ApiTools\Exception\HttpMethodNotFoundException
* @throws \ByJG\ApiTools\Exception\InvalidDefinitionException
* @throws \ByJG\ApiTools\Exception\InvalidRequestException
* @throws \ByJG\ApiTools\Exception\NotMatchedException
* @throws \ByJG\ApiTools\Exception\PathNotFoundException
*/
public function testMatchResponseBodyWithRefInsteadOfContent()
{
$openApiSchema = self::openApiSchema5();

$body = [
"param_response_1" => "example1",
"param_response_2" => "example2"
];

$responseParameter = $openApiSchema->getResponseParameters('/v1/test', 'post', 201);
$this->assertTrue($responseParameter->match($body));
}

/**
* @expectedException \ByJG\ApiTools\Exception\NotMatchedException
* @expectedExceptionMessage Value 'notfound' in 'status' not matched in ENUM
Expand Down
101 changes: 101 additions & 0 deletions tests/example/openapi5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
{
"openapi": "3.0.0",
"info": {
"description": "API test",
"version": "0.0.1",
"title": "API-test"
},
"servers": [
{
"url": "https://127.0.0.1",
"description": "Instance of the API-test"
}
],
"paths": {
"/v1/test": {
"post": {
"security": [
{
"bearer": []
}
],
"summary": "Test endpoint",
"requestBody": {
"$ref": "#/components/requestBodies/TestBodyRequest"
},
"responses": {
"201": {
"$ref": "#/components/responses/TestBodyResponse"
}
}
}
}
},
"components": {
"requestBodies": {
"TestBodyRequest": {
"description": "Test body",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TestSchema"
}
}
}
}
},
"responses": {
"TestBodyResponse": {
"description": "Test body response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Success"
}
}
}
}
},
"schemas": {
"TestSchema": {
"type": "object",
"required": [
"param1",
"param2",
"param3"
],
"properties": {
"param1": {
"type": "string",
"description": "Param 1 description"
},
"param2": {
"type": "string",
"description": "Param 2 description"
},
"param3": {
"type": "string",
"description": "Param 3 description"
}
}
},
"Success": {
"type": "object",
"required": [
"param_response_1",
"param_response_2"
],
"properties": {
"param_response_1": {
"type": "string",
"description": "param_response_1 description"
},
"param_response_2": {
"type": "string",
"description": "param_response_2 description"
}
}
}
}
}
}

0 comments on commit e2c0b34

Please sign in to comment.