I want to receive the following json:
{
"flat": "string",
"object": {
"key": "string"
}
}
I created DtoInput class
public $flat;
/**
* @var ObjectModel
*/
public $object;
public function setFlat(string $flat) { ... }
public function setObject(ObjectModel $flat) { ... }
and class ObjectModel
public $key;
public function setKey(string $key) { ... }
but on link /api i can see model
{
"flat": "string",
"object": "string",
}
I want to see
{
"flat": "string",
"object": {
"key": "string"
}
}
I was tried to use @Groups from Symfony\Component\Serializer\Annotation\Groups and denormalizationContext but without success. I think problem can be connected with DTO because of without DTO I configured denormalizationContext correctly.
I created repository with exemplary code.
https://github.com/gustawdaniel/api-dto-subresource
Most interesting are README.md and this commit
https://github.com/gustawdaniel/api-dto-subresource/commit/5a8214ac2ae4c4341452b2234be20cd1adcff538
This code is written for output, not input DTO but this is the same case - types from getters / setters are changed to string if objects are given.
I changed paths in config to paths: ['%kernel.project_dir%/src/Entity', '%kernel.project_dir%/src/Model'] and added normalizationContext={"groups"={"read"}} to Book, and
@Groups({"read"}) to any interesting property, then @ApiResource() to Attribute and @ApiProperty(identifier=true) to key in Attribute. Then model was fixed but i have now not needed endpoints.
When I removed @ApiResource() from Attribute or use
/**
* @ApiResource(
* itemOperations={},
* collectionOperations={}
* )
*/
then I lost model shape that I want to have
Same here.
Most helpful comment
I changed paths in config to
paths: ['%kernel.project_dir%/src/Entity', '%kernel.project_dir%/src/Model']and addednormalizationContext={"groups"={"read"}}to Book, and@Groups({"read"})to any interesting property, then@ApiResource()to Attribute and@ApiProperty(identifier=true)to key in Attribute. Then model was fixed but i have now not needed endpoints.When I removed
@ApiResource()from Attribute or usethen I lost model shape that I want to have