Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix resolving general component refs #1557

Merged
merged 1 commit into from
Mar 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: { }