Skip to content

Commit 30f3f35

Browse files
priyadisoyuka
andauthored
fix(openapi): skip requestBody if input is false (#6163)
* fix(openapi): skip requestBody if input is false * cs fix * fix phpstan error * cs fix * merge condition --------- Co-authored-by: soyuka <[email protected]>
1 parent 2999d9e commit 30f3f35

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/OpenApi/Factory/OpenApiFactory.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,10 @@ private function collectPaths(ApiResource $resource, ResourceMetadataCollection
347347
'The "openapiContext" option is deprecated, use "openapi" instead.'
348348
);
349349
$openapiOperation = $openapiOperation->withRequestBody(new RequestBody($contextRequestBody['description'] ?? '', new \ArrayObject($contextRequestBody['content']), $contextRequestBody['required'] ?? false));
350-
} elseif (null === $openapiOperation->getRequestBody() && \in_array($method, ['PATCH', 'PUT', 'POST'], true)) {
350+
} elseif (
351+
null === $openapiOperation->getRequestBody() && \in_array($method, ['PATCH', 'PUT', 'POST'], true)
352+
&& !(false === ($input = $operation->getInput()) || (\is_array($input) && null === $input['class']))
353+
) {
351354
$operationInputSchemas = [];
352355
foreach ($requestMimeTypes as $operationFormat) {
353356
$operationInputSchema = $this->jsonSchemaFactory->buildSchema($resourceClass, $operationFormat, Schema::TYPE_INPUT, $operation, $schema, null, $forceSchemaCollection);

src/OpenApi/Tests/Factory/OpenApiFactoryTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ public function testInvoke(): void
239239
),
240240
],
241241
)),
242+
'postDummyItemWithoutInput' => (new Post())->withUriTemplate('/dummyitem/noinput')->withOperation($baseOperation)->withInput(false),
242243
])
243244
);
244245

@@ -927,5 +928,28 @@ public function testInvoke(): void
927928
]),
928929
]
929930
), $dummyItemPath->getGet());
931+
932+
$emptyRequestBodyPath = $paths->getPath('/dummyitem/noinput');
933+
$this->assertEquals(new Operation(
934+
'postDummyItemWithoutInput',
935+
['Dummy'],
936+
[
937+
'201' => new Response(
938+
'Dummy resource created',
939+
new \ArrayObject([
940+
'application/ld+json' => new MediaType(new \ArrayObject(new \ArrayObject(['$ref' => '#/components/schemas/Dummy.OutputDto']))),
941+
]),
942+
null,
943+
new \ArrayObject(['getDummyItem' => new Model\Link('getDummyItem', new \ArrayObject(['id' => '$response.body#/id']), null, 'This is a dummy')])
944+
),
945+
'400' => new Response('Invalid input'),
946+
'422' => new Response('Unprocessable entity'),
947+
],
948+
'Creates a Dummy resource.',
949+
'Creates a Dummy resource.',
950+
null,
951+
[],
952+
null
953+
), $emptyRequestBodyPath->getPost());
930954
}
931955
}

0 commit comments

Comments
 (0)