Description
API Platform version(s) affected: 2.6.4
Description
The POST and PUT methods in the Swagger UI docs don't use the write Schema properly, fields are missing
How to reproduce
Create an Entity like the one i got from the API-Platform course on Symfonycasts, where I first discovered the bug.
Set a read group (normalizationContext) and a write group (denormalizationContext) and set groups on field an/or a setter method (like in the example, where the write group is set on the setTextDescription-method).
<?php
namespace App\Entity;
use ApiPlatform\Core\Annotation\ApiResource;
use Carbon\Carbon;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation\Groups;
/**
* @ApiResource(
* collectionOperations={"get", "post"},
* itemOperations={
* "get"={},
* "put"
* },
* shortName="cheeses",
* normalizationContext={"groups"={"cheese_listing:read"}, "swagger_definition_name"="Read"},
* denormalizationContext={"groups"={"cheese_listing:write"}, "swagger_definition_name"="Write"}
* )
* @ORM\Entity(repositoryClass="App\Repository\CheeseListingRepository")
*/
class CheeseListing
{
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", length=255)
* @Groups({"cheese_listing:read", "cheese_listing:write"})
*/
private $title;
/**
* @ORM\Column(type="text")
* @Groups({"cheese_listing:read"})
*/
private $description;
/**
* The price of this delicious cheese, in cents
*
* @ORM\Column(type="integer")
* @Groups({"cheese_listing:read", "cheese_listing:write"})
*/
private $price;
/**
* @ORM\Column(type="datetime")
*/
private $createdAt;
/**
* @ORM\Column(type="boolean")
*/
private $isPublished = false;
public function __construct()
{
$this->createdAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(string $description): self
{
$this->description = $description;
return $this;
}
/**
* The description of the cheese as raw text.
*
* @Groups("cheese_listing:write")
*/
public function setTextDescription(string $description): self
{
$this->description = nl2br($description);
return $this;
}
public function getPrice(): ?int
{
return $this->price;
}
public function setPrice(int $price): self
{
$this->price = $price;
return $this;
}
public function getCreatedAt(): ?\DateTimeInterface
{
return $this->createdAt;
}
/**
* How long ago in text that this cheese listing was added.
*
* @Groups("cheese_listing:read")
*/
public function getCreatedAtAgo(): string
{
return Carbon::instance($this->getCreatedAt())->diffForHumans();
}
public function getIsPublished(): ?bool
{
return $this->isPublished;
}
public function setIsPublished(bool $isPublished): self
{
$this->isPublished = $isPublished;
return $this;
}
}
The write-Schema, shown in the Swagger Ui doc shows all fields/properties as intended:
cheeses-Write{
title string
minLength: 2
maxLength: 50
price* integer
description string
writeOnly: true
The description of the cheese as raw text.
}
But under the POST and PUT methods example values, the desctiption field is missing:
{
"title": "string",
"price": 0
}
And whren i click on "Schema", netxt to "Example Value", it says:
cheeses.jsonld-Read{
title string
minLength: 2
maxLength: 50
price* integer
}
First I thought that bevahiour is related to the write-group being set on a method an not on a field, but then i discovered the same behaviour on my User-Entity where I set the wirite group directlyon my password property, but the field is missing in the POST and PUT methods too (but appers in the write-Schema)
Additional Context
Excuse my english, i am not a native speaker
Activity
mxmp210 commentedon Apr 30, 2021
Related #1892
Downgrade API Platform to version 2.6.3 as it cause issues