Skip to content

Commit 5ce5ec3

Browse files
committed
feat(openapi): allow optional request body content
1 parent e867d07 commit 5ce5ec3

File tree

3 files changed

+212
-169
lines changed

3 files changed

+212
-169
lines changed

src/OpenApi/Factory/OpenApiFactory.php

Lines changed: 27 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -393,18 +393,26 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
393393
'The "openapiContext" option is deprecated, use "openapi" instead.'
394394
);
395395
$openapiOperation = $openapiOperation->withRequestBody(new RequestBody($contextRequestBody['description'] ?? '', new \ArrayObject($contextRequestBody['content']), $contextRequestBody['required'] ?? false));
396-
} elseif (
397-
null === $openapiOperation->getRequestBody() && \in_array($method, ['PATCH', 'PUT', 'POST'], true)
396+
} else if (
397+
\in_array($method, ['PATCH', 'PUT', 'POST'], true)
398398
&& !(false === ($input = $operation->getInput()) || (\is_array($input) && null === $input['class']))
399399
) {
400-
$operationInputSchemas = [];
401-
foreach ($requestMimeTypes as $operationFormat) {
402-
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);
403-
$operationInputSchemas[$operationFormat] = $operationInputSchema;
404-
$this->appendSchemaDefinitions($schemas, $operationInputSchema->getDefinitions());
400+
$content = $openapiOperation->getRequestBody()?->getContent();
401+
if (null === $content) {
402+
$operationInputSchemas = [];
403+
foreach ($requestMimeTypes as $operationFormat) {
404+
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);
405+
$operationInputSchemas[$operationFormat] = $operationInputSchema;
406+
$this->appendSchemaDefinitions($schemas, $operationInputSchema->getDefinitions());
407+
}
408+
$content = $this->buildContent($requestMimeTypes, $operationInputSchemas);
405409
}
406410

407-
$openapiOperation = $openapiOperation->withRequestBody(new RequestBody(sprintf('The %s %s resource', 'POST' === $method ? 'new' : 'updated', $resourceShortName), $this->buildContent($requestMimeTypes, $operationInputSchemas), true));
411+
$openapiOperation = $openapiOperation->withRequestBody(new RequestBody(
412+
description: $openapiOperation->getRequestBody()?->getDescription() ?? sprintf('The %s %s resource', 'POST' === $method ? 'new' : 'updated', $resourceShortName),
413+
content: $content,
414+
required: $openapiOperation->getRequestBody()?->getRequired() ?? true,
415+
));
408416
}
409417

410418
// TODO Remove in 4.0
@@ -758,17 +766,17 @@ private function hasParameter(Model\Operation $operation, Parameter $parameter):
758766
private function mergeParameter(Parameter $actual, Parameter $defined): Parameter
759767
{
760768
foreach ([
761-
'name',
762-
'in',
763-
'description',
764-
'required',
765-
'deprecated',
766-
'allowEmptyValue',
767-
'style',
768-
'explode',
769-
'allowReserved',
770-
'example',
771-
] as $method) {
769+
'name',
770+
'in',
771+
'description',
772+
'required',
773+
'deprecated',
774+
'allowEmptyValue',
775+
'style',
776+
'explode',
777+
'allowReserved',
778+
'example',
779+
] as $method) {
772780
$newValue = $defined->{"get$method"}();
773781
if (null !== $newValue && $actual->{"get$method"}() !== $newValue) {
774782
$actual = $actual->{"with$method"}($newValue);

src/OpenApi/Model/RequestBody.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@ final class RequestBody
1717
{
1818
use ExtensionTrait;
1919

20-
public function __construct(private string $description = '', private ?\ArrayObject $content = null, private bool $required = false)
20+
public function __construct(private ?string $description = null, private ?\ArrayObject $content = null, private bool $required = false)
2121
{
2222
}
2323

24-
public function getDescription(): string
24+
public function getDescription(): ?string
2525
{
2626
return $this->description;
2727
}
2828

29-
public function getContent(): \ArrayObject
29+
public function getContent(): ?\ArrayObject
3030
{
3131
return $this->content;
3232
}

0 commit comments

Comments
 (0)