Skip to content

PATCH is trying to create new entity #5645

Description

@FluffyDiscord

API Platform version(s) affected: x.y.z
3.1.12

Description
I have api resource entity that is only one in the database and it has ID 1. I have set up the entity like this

<?phpnamespaceApp\Entity;
useApiPlatform\Metadata\ApiResource;
useApiPlatform\Metadata\Get;
useApiPlatform\Metadata\Patch;
useApp\Repository\SettingsRepository;
useDoctrine\DBAL\Types\Types;
useDoctrine\ORM\MappingasORM;
useSymfony\Component\Serializer\Annotation\Groups;
#[ApiResource(
operations: [
newGet(
uriTemplate: "/settings.{_format}",
),
newPatch(
uriTemplate: "/settings.{_format}",
),
],
normalizationContext: [
"skip_null_values" => false,
"groups" => ["settings:read"],
],
denormalizationContext: [
"skip_null_values" => false,
"groups" => ["settings:write"],
],
)]
#[ORM\Entity(repositoryClass: SettingsRepository::class)]
class Settings
{
#[ORM\Id]
#[ORM\GeneratedValue("NONE")]
#[ORM\Column(type: Types::INTEGER)]
privateint$id = 1;
#[Groups(["settings:read", "settings:write"])]
#[ORM\Column(type: Types::TEXT, nullable: true)]
private ?string$text = null;
publicfunctiongetId(): int
{
return$this->id;
}
publicfunctiongetText(): ?string
{
return$this->text;
}
publicfunctionsetText(?string$text): self
{
$this->text = $text;
return$this;
}
}

To my surprise I can GET /api/settings the entity just fine. But the PATCH /api/settings throws errors because it's trying to insert new entity and failing because there is already entity with ID 1.

How to reproduce
Use entity from description above, try PATCHing the entity by sending 'text' field

Possible Solution

Additional Context

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions