api-platform / core

The server component of API Platform: hypermedia and GraphQL APIs in minutes
https://api-platform.com
MIT License
2.38k stars 846 forks source link

fix(jsonschema): make all required properties optional in PATCH operation with 'json' format #6394

Closed ttskch closed 23 hours ago

ttskch commented 1 month ago

[!NOTE] Is this a new feature? If so, we can close this PR and go to #6395.

Q A
Branch? 3.3
Tickets N/A
License MIT
Doc PR N/A

In the schema for the request body of application/merge-patch+json PATCH operation, all required properties should be output as optional.

See here: https://datatracker.ietf.org/doc/html/rfc7386#section-3

This PR fixes the SchemaFactory for the 'json' format to output required properties as optional only for input context of PATCH operations.

For example, for the following entity, the output of the OpenAPI document will change as follows:

#[ORM\Entity(repositoryClass: TodoRepository::class)]
#[ApiResource]
class Todo
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    private ?int $id = null;

    #[ORM\Column(length: 255)]
    #[Assert\NotBlank]
    #[Assert\Length(max: 255)]
    private ?string $title = null;

    #[ORM\Column(type: Types::TEXT, nullable: true)]
    private ?string $description = null;

    #[ORM\Column]
    #[Assert\NotNull]
    private ?bool $done = null;
Before After
PATCH Operation image image
Schemas image
soyuka commented 1 month ago

I think that we can make this a bug fix as it won't break any current implementations