Skip to content

Commit

Permalink
Fix resolving general component refs (#1557)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Mar 15, 2024
1 parent 038da8a commit fe45a0e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ public function getRoot(): string
*/
public function isRoot(string $rootClass): bool
{
return $this->getRoot() == $rootClass;
return get_class($this) == $rootClass || $this->getRoot() == $rootClass;
}

/**
Expand Down
15 changes: 14 additions & 1 deletion src/Annotations/RequestBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class RequestBody extends AbstractAnnotation
public $ref = Generator::UNDEFINED;

/**
* Request body model name.
* The key into Components->requestBodies array.
*
* @var string
*/
Expand Down Expand Up @@ -95,4 +95,17 @@ class RequestBody extends AbstractAnnotation
MediaType::class => ['content', 'mediaType'],
Attachable::class => ['attachables'],
];

/**
* @inheritdoc
*/
#[\ReturnTypeWillChange]
public function jsonSerialize()
{
$data = parent::jsonSerialize();

unset($data->request);

return $data;
}
}
70 changes: 70 additions & 0 deletions tests/Fixtures/Scratch/RequestBody.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Tests\Fixtures\Scratch;

use OpenApi\Attributes as OAT;

#[OAT\RequestBody()]
class RequestBodyRef
{
}

#[OAT\RequestBody(request: 'foo')]
class RequestBodyRefFoo
{
}

#[OAT\Info(title: 'RequestBody', version: '1.0')]
class RequestBodyController
{
#[OAT\Get(
path: '/endpoint',
requestBody: new OAT\RequestBody(
description: 'Information about a new pet in the system',
content: new OAT\MediaType(
mediaType: 'application/json'
),
),
responses: [
new OAT\Response(
response: 200,
description: 'All good'
),
]
)]
public function post()
{
}

#[OAT\Post(
path: '/endpoint/ref',
requestBody: new OAT\RequestBody(ref: RequestBodyRef::class),
responses: [
new OAT\Response(
response: 200,
description: 'All good'
),
]
)]
public function postRef()
{
}

#[OAT\Post(
path: '/endpoint/ref-foo',
requestBody: new OAT\RequestBody(ref: RequestBodyRefFoo::class),
responses: [
new OAT\Response(
response: 200,
description: 'All good'
),
]
)]
public function postRefFoo()
{
}
}
35 changes: 35 additions & 0 deletions tests/Fixtures/Scratch/RequestBody.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.0
info:
title: RequestBody
version: '1.0'
paths:
/endpoint:
get:
operationId: 30597cf43b480393042f6b01a9d7980c
requestBody:
description: 'Information about a new pet in the system'
content:
application/json: { }
responses:
'200':
description: 'All good'
/endpoint/ref:
post:
operationId: 4250dd87e4e3872a8f2e481532cbc245
requestBody:
$ref: '#/components/requestBodies/RequestBodyRef'
responses:
'200':
description: 'All good'
/endpoint/ref-foo:
post:
operationId: 344406e28927343e4e9e4f39bd6c385b
requestBody:
$ref: '#/components/requestBodies/foo'
responses:
'200':
description: 'All good'
components:
requestBodies:
RequestBodyRef: { }
foo: { }

0 comments on commit fe45a0e

Please sign in to comment.