Skip to content

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.

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.

}

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

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