Symfony4 JMS\Serializer\SerializedName will be ignored on deserialize() - symfony

I have a JSON that will be deserialize() with JMS.
The JSON looks like
{
"creator": [
{
"value": 234,
"label": "Hello"
},
{
"value": 223,
"label": "World"
}
]
}
The JSON will be deserialized with my created Model
$this->serializer->deserialize($json, Model::class, 'json');
and my Model class has:
/**
* #Serializer\Type("array")
*/
private $creator;
This works perfectly fine, the deserializer convert the JSON into my Model and I receive creator with array-items.
I would like to change the variable name from creator to customer in my model. I was thinking it works with the annotation #Serializer\SerializedName().
But when im using this
/**
* #Serializer\Type("array")
* #Serializer\SerializedName("customer")
*/
private $creator;
the model will be not filled at all.
Do I fundamentally misunderstand this function?

Related

How can i write this JSON swagger-php below with annotations?

I'm using OpenAPI 3.0 to document my Symfony API. this is the JSON code that authenticates the user to send requests:
"securitySchemes": {
"Bearer": {
"type": "http",
"description": "Entrer le token JST",
"scheme": "bearer",
"bearerFormat": "JWT"
}
}
},
"security": [
{
"Bearer": []
}
]
How can i write this with annotations in controller ?
thanks
You almost can; currently swagger-php does not support the bearerFormat property.
=> https://github.com/zircote/swagger-php/issues/1258
The rest would look something like this globally.
/**
* #OA\SecurityScheme(
* securityScheme="bearerAuth",
* type="http",
* scheme="bearer",
* description="Entrer le token JST"
* )
*/
For a controller to require security you'd add this:
/**
* #OA\Get(
* path="/api/endpoint",
* ...
* security={{ "bearerAuth": {} }}
* )
*/

Save entity with subresource into redis with Symfony & Api Platform

I have 2 entities, Event & User. So basically A user can have multiple event.
I have an EventItemDataProvider and in the getItem method I did something like that:
public function getItem(string $resourceClass, $id, string $operationName = null, array $context = []): ?Event
{
if ($event = $this->cache->get($id, Event::class)) {
return $event;
}
$event = $this->em->getRepository(Event::class)->find($id);
if($event) {
$this->cache->set($id, $event, 120);
}
return $event;
}
When the data is returned without the cache I have this result that is correct with the right username:
{
"#context": "/contexts/Event",
"#id": "/events/20",
"#type": "Event",
"title": "new global event 3",
"description": "A big global event 3",
"createdAt": "2019-07-05T09:20:48+00:00",
"owner": {
"#id": "/users/3",
"#type": "User",
"username": "test3"
}
}
But if I hit the same method a second time, it will retrieve the data from the cache (redis) but this time username will be empty like that:
{
"#context": "/contexts/Event",
"#id": "/events/20",
"#type": "Event",
"title": "new global event 3",
"description": "A big global event 3",
"createdAt": "2019-07-05T09:20:48+00:00",
"owner": {
"#id": "/users/3",
"#type": "User",
"username": ""
}
}
Why is my username empty when I retrieve it from redis?
Basically my cache class has these methods:
public function get(string $uniq, string $className)
{
$value = $this->cache->getItem('test_key')->get();
return $value;
}
public function set(string $uniq, object $entity, int $expires = 60)
{
$save = $this->cache->getItem('test_key');
$save->set($entity);
$save->expiresAfter($expires);
$saved = $this->cache->save($save);
return $saved;
}
The owner field in my Event entity:
/**
* #ORM\ManyToOne(targetEntity="App\Entity\User", inversedBy="events")
* #ORM\JoinColumn(nullable=false)
* #Groups({"event:read", "event:write", "event:update"})
* #Assert\Valid()
*/
private $owner;
The username field in my User entity:
/**
* #ORM\Column(type="string", length=25, unique=true)
* #Groups({"user:read", "user:write", "user:update", "event:item:get", "event:update"})
* #Assert\NotBlank(message="Please provide a username")
*/
private $username;
My redis cache configuration:
cache:
app: cache.adapter.redis
default_redis_provider: "redis://redis"
pools:
custom.cache.entity:
adapter: cache.app
default_lifetime: 120
Why my username is filled when I retrieve it from Doctrine Repository but empty when I retrieve it from Redis?
With Xdebug I can see that if I retrieve the data from Doctrine I have something like that:
But if I retrieve the data from the cache I have something like that:
In this second case, initializer -> this seems infinite, so I think that the problem is probably here, but what should I do to solve this issue?

Symfony + FOSRestBundle - How to allow NULL value to a field configured with a custom form type?

I have a simple Symfony API which uses FOSRestBundle. I have an Exercise entity which contains a field sentences. This field is of type json #ORM\Column(type="json") and is stuffed with some nested json. The entity is persisted in a MySQL database.
I use Symfony forms to validate incoming data from a SPA. Here's the data the SPA sends on the endpoint /exercise:
{
"name": "HEP9H",
"sentences": [
{
"name": "Sentence",
"tirettes": [
{
"chain": null
},
{
"chain": {
"name": "Chain 1"
}
}
]
}
]
}
Once persisted, the API then returns the entity as JSON. It should look exactly the same, except that it has an ID. The issue is that I get this piece of JSON in return:
{
"id": 21,
"name": "HEP9H",
"sentences": [
{
"name": "Sentence",
"tirettes": [
{
"chain": {
"name": null
}
},
{
"chain": {
"name": "Chaîne 1"
}
}
]
}
]
}
As you can see, the problem is that my property "chain": null becomes "chain": {"name": null}. I guess this is due to a bad form type configuration. The data structure changes right after I validate my form and before I persist the entity for the first time.
Here's TiretteType:
class TiretteType extends AbstractType {
public function buildForm ( FormBuilderInterface $builder, array $options ) {
$builder
->add ( 'chain', ChainType::class, [
"required" => false
] );
}
}
And here's ChainType:
class ChainType extends AbstractType {
public function buildForm ( FormBuilderInterface $builder, array $options ) {
$builder->add ( 'name', TextType::class );
}
}
I have no underlying data class and no underlying entity (except the root entity Exercise).
What I've tried so far:
adding "required" => false to the 'chain' field, it doesn't change anything
setting "empty_data" => NULL to the 'chain' field, this also doesn't work and overrides any data to NULL
Am I completely missing something?
Thanks!
I found the answer to my issue. Since my field chain had no underlying data class, the form would simply give me an array with default values if it had a null value as input.
The solution is to use a data transformer (https://symfony.com/doc/current/form/data_transformers.html). I had to check for such an empty structure and if found, return back null instead of the given value.
$builder->get ( 'chain' )->addModelTransformer ( new CallbackTransformer(
function ( $originalInput ) {
return $originalInput;
},
function ( $submittedValue ) {
return $submittedValue["name"] === null ? $submittedValue : null;
}
) );
I don't think checking for null properties is the cleanest way to do this but my case is very simple so I won't spend more time on this one.
Hope this helps someone.

How to have absolute url with Hateoas Bundle on Symfony2

I just fresh installed the bundle from Willdurant's github and I got relative url like this:
"_links": {
"self": {
"href": "/1.0/users/?page=1&limit=10"
},
"first": {
"href": "/1.0/users/?page=1&limit=10"
},
"last": {
"href": "/1.0/users/?page=2&limit=10"
},
"next": {
"href": "/1.0/users/?page=2&limit=10"
}
}
For my Hateoas url, I really prefer absolute url but I don't find anything on google to change that. 2 hours of search, trying multiple keywords and nothing...
Thanks for your help.
You can use it many ways. All depends on the way of generating links.
If you use #Route annotation to generate link, it has a parameter absolute that need to be set to true:
/**
* #Hateoas\Relation(
* name = "self",
* href = #Hateoas\Route(
* "user_get",
* parameters = { "id" = "expr(object.getId())" },
* absolute = true
* )
* )
*/
If you use expression language to generate link you can pass true as third parameter to the link() function:
/**
* #Hateoas\Relation(
* "user",
* href = "expr(link(object.getUser(), 'self', true))"
* )
*/

JMS #Discriminator filed doesn't appear if specific group is serializing

I'm using Symfony 2.8, FOSRestBundle and JMSSerializerBundle.
Problem
Discriminator field type of Document entity doesn't apear in serialized model when I serialize specific group ("api" group in folowing example) of entity Citizen.
Doctrine Entities
Document:
namespace MyBundle\Entity;
use JMS\Serializer\Annotation as JMS;
…
/**
* #JMS\Discriminator(field = "type", map = {
* "doc1" = "MyBundle\Entity\Document1",
* "doc2" = "MyBundle\Entity\Document2"
* })
*/
class Document
…
Citizen:
class Citizen
{
…
/**
* #var ArrayCollection
*
* #ORM\OneToMany(
* targetEntity="MyBundle\Entity\Document",
* cascade={ "PERSIST", "REMOVE" },
* orphanRemoval=true,
* mappedBy="citizen"
* )
*
* #JMS\Groups({"api"})
*/
private $documents;
…
What I get
{
…
"documents": [
{
"number": "000000",
"date": "01.01.1970",
"serial": "0000",
"place": ""
}
],
…
}
What I need
{
…
"documents": [
{
"type": "doc1",
"number": "000000",
"date": "01.01.1970",
"serial": "0000",
"place": ""
}
],
…
}
If I remove specific serialization group, then type field is present in serialized output.
Thanks in advance
Just found issue on github.
Seems for now, workaround with Default group is needed, see lordelph's comment

Resources