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

feat(openapi): allow optional request body content #6374

Merged
merged 1 commit into from
May 24, 2024

Conversation

monitaurus
Copy link
Contributor

Q A
Branch? main
Tickets Closes #2680
License MIT
Doc PR already existing (see doc)

Allow to generate a default request content or description if not defined in the extension of OpenApi using Model\RequestBody.

For example, on a POST /greetings/test operation on a Greeting entity with a id and a name, the following:

#[ApiResource(
    mercure: true,
    operations: [
        new Post(
            uriTemplate: '/greetings/test',
            openapi: new Operation(
                requestBody: new RequestBody(
                    description: 'Extended description',
                ),
            ),
        ),
    ],
)]

Will generate the following OpenApi, with the default generated content:
image

@monitaurus monitaurus force-pushed the override-openapi-request-body branch from 0eb4e2b to 5ce5ec3 Compare May 18, 2024 22:01
@monitaurus monitaurus force-pushed the override-openapi-request-body branch from 5ce5ec3 to 8b57125 Compare May 18, 2024 22:06
@monitaurus monitaurus marked this pull request as ready for review May 18, 2024 22:21
$openapiOperation = $openapiOperation->withRequestBody(new RequestBody(
description: $openapiOperation->getRequestBody()?->getDescription() ?? sprintf('The %s %s resource', 'POST' === $method ? 'new' : 'updated', $resourceShortName),
content: $content,
required: $openapiOperation->getRequestBody()?->getRequired() ?? true,
Copy link
Member

@soyuka soyuka May 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to understand this correctly the main change is that true is now the default value when there's a request body ?

I think that early on we used false as a default value to comply with: https://github.com/OAI/OpenAPI-Specification/blob/main/versions/3.1.0.md#request-body-object I agree that this is better thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to understand this correctly the main change is that true is now the default value when there's a request body ?

In the current code, the constructor set Model\RequestBody's required to false by default. But the OpenFactory force the required to be true if there were no RequestBody and if this was a write operation.

Here the PR allow the developer to fine-tune the values as he/she wants.
So it's even possible now to set a required to false even in a write operation.

@@ -17,16 +17,16 @@ final class RequestBody
{
use ExtensionTrait;

public function __construct(private string $description = '', private ?\ArrayObject $content = null, private bool $required = false)
public function __construct(private ?string $description = null, private ?\ArrayObject $content = null, private bool $required = false)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR's originated where I wanted to edit the description of an endpoint, but by doing so, I needed to rewrite the whole content.

The PR code make the Model\RequestBody itself differs from OpenAPI own specification.
But the whole OpenApiFactory will bring back the compliance while the specification by overwriting the content if it's set to null.

{
return $this->description;
}

public function getContent(): \ArrayObject
public function getContent(): ?\ArrayObject
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see my previous comment this was correct but the constructor is not

@monitaurus monitaurus requested a review from soyuka May 21, 2024 12:36
@soyuka soyuka merged commit 65296ea into api-platform:main May 24, 2024
74 of 76 checks passed
@monitaurus monitaurus deleted the override-openapi-request-body branch May 24, 2024 19:33
soyuka pushed a commit to soyuka/core that referenced this pull request Jun 13, 2024
soyuka pushed a commit to DartCZ/core that referenced this pull request Jun 27, 2024
soyuka pushed a commit to soyuka/core that referenced this pull request Jul 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants