Skip to content

Commit

Permalink
feat: Add support for @OA\JsonContent in Parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
Bob Fanger committed Jan 22, 2020
1 parent c86386b commit 52728ae
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Examples/misc/misc-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
/**
* @OA\Get(
* path="/api/endpoint",
* @OA\Parameter(name="filter",in="query", @OA\JsonContent(
* @OA\Property(property="type", type="string"),
* @OA\Property(property="color", type="string"),
* )),
* @OA\Response(response=200, description="Success")
* )
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public function merge($annotations, $ignore = false)
foreach ($annotations as $annotation) {
$found = false;
foreach (static::$_nested as $class => $property) {
if ($annotation instanceof $class) {
if (get_class($annotation) === $class) {

This comment has been minimized.

Copy link
@krrico

krrico Jun 3, 2020

This breaks backwards compatibility at least for my use case: I use lots of custom Annotation classes that inherit from OpenApi\Annotations\Parameter class that now fail to get merged, so I have to add all of them to the $_nested array in every single one of my other classes that inherit from the Operation class.

if (is_array($property)) { // Append to an array?
$property = $property[0];
if ($this->$property === UNDEFINED) {
Expand Down
21 changes: 14 additions & 7 deletions src/Processors/MergeJsonContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
use OpenApi\Annotations\JsonContent;
use OpenApi\Annotations\Response;
use OpenApi\Annotations\RequestBody;
use OpenApi\Annotations\Parameter;
use OpenApi\Analysis;
use OpenApi\Context;
use OpenApi\Logger;

/**
* Split JsonContent into Schema and MediaType
Expand All @@ -22,14 +24,19 @@ public function __invoke(Analysis $analysis)
{
$annotations = $analysis->getAnnotationsOfType(JsonContent::class);
foreach ($annotations as $jsonContent) {
$response = $jsonContent->_context->nested;
if (!($response instanceof Response) && !($response instanceof RequestBody)) {
$parent = $jsonContent->_context->nested;
if (!($parent instanceof Response) && !($parent instanceof RequestBody) && !($parent instanceof Parameter)) {
if ($parent) {
Logger::notice('Unexpected '.$jsonContent->identity() .' in ' . $parent->identity() . ' in ' . $this->_context);
} else {
Logger::notice('Unexpected '.$jsonContent->identity() .' must be nested');
}
continue;
}
if ($response->content === UNDEFINED) {
$response->content = [];
if ($parent->content === UNDEFINED) {
$parent->content = [];
}
$response->content['application/json'] = new MediaType(
$parent->content['application/json'] = new MediaType(
[
'mediaType' => 'application/json',
'schema' => $jsonContent,
Expand All @@ -41,9 +48,9 @@ public function __invoke(Analysis $analysis)
$jsonContent->example = UNDEFINED;
$jsonContent->examples = UNDEFINED;

$index = array_search($jsonContent, $response->_unmerged, true);
$index = array_search($jsonContent, $parent->_unmerged, true);
if ($index !== false) {
array_splice($response->_unmerged, $index, 1);
array_splice($parent->_unmerged, $index, 1);
}
}
}
Expand Down
19 changes: 12 additions & 7 deletions src/Processors/MergeXmlContent.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ public function __invoke(Analysis $analysis)
{
$annotations = $analysis->getAnnotationsOfType(XmlContent::class);
foreach ($annotations as $xmlContent) {
$response = $xmlContent->_context->nested;
if (!($response instanceof Response) && !($response instanceof RequestBody)) {
$parent = $xmlContent->_context->nested;
if (!($parent instanceof Response) && !($parent instanceof RequestBody) && !($parent instanceof Parameter)) {
if ($parent) {
Logger::notice('Unexpected '.$xmlContent->identity() .' in ' . $parent->identity() . ' in ' . $this->_context);
} else {
Logger::notice('Unexpected '.$xmlContent->identity() .' must be nested');
}
continue;
}
if ($response->content === UNDEFINED) {
$response->content = [];
if ($parent->content === UNDEFINED) {
$parent->content = [];
}
$response->content['application/xml'] = new MediaType(
$parent->content['application/xml'] = new MediaType(
[
'mediaType' => 'application/xml',
'schema' => $xmlContent,
Expand All @@ -41,9 +46,9 @@ public function __invoke(Analysis $analysis)
$xmlContent->example = UNDEFINED;
$xmlContent->examples = UNDEFINED;

$index = array_search($xmlContent, $response->_unmerged, true);
$index = array_search($xmlContent, $parent->_unmerged, true);
if ($index !== false) {
array_splice($response->_unmerged, $index, 1);
array_splice($parent->_unmerged, $index, 1);
}
}
}
Expand Down
22 changes: 22 additions & 0 deletions tests/ExamplesOutput/misc.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,28 @@
"paths": {
"/api/endpoint": {
"get": {
"parameters": [
{
"name": "filter",
"in": "query",
"content": {
"application/json": {
"mediaType": "application/json",
"schema": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"color": {
"type": "string"
}
}
}
}
}
}
],
"responses": {
"200": {
"description": "Success"
Expand Down

0 comments on commit 52728ae

Please sign in to comment.