Symfony Api Platform Normalization Context Groups datetime property issue

I’m struggle with strange issue when using normalizer with groups. It look like there is a problem with datetime properties.

Issue occurs when using normalizationContext on ApiResource and/or normalization_context on operation.

<?php

namespace AppEntity;

use ApiPlatformCoreAnnotationApiResource;
use DoctrineORMMapping as ORM;
use SymfonyComponentSerializerAnnotationGroups;

/**
 * @ApiResource(
 *     collectionOperations={
 *          "get"={
 *              "normalization_context"={
 *                  "datetime_format"="Y-m-dTH:i:sP",
 *                  "groups"={"bookmarkdata:collection:read"}
 *              }
 *          }
 *     },
 *     itemOperations={
 *          "get"
 *     },
 *     normalizationContext={"groups"={"bookmarkdata:read"}}
 * )
 * @ORMEntity(repositoryClass="AppRepositoryBookmarkDataRepository")
 */
class BookmarkData
{
    /**
     * @ORMId()
     * @ORMGeneratedValue()
     * @ORMColumn(type="integer")
     * @Groups({"bookmarkdata:read", "bookmarkdata:collection:read"})
     */
    private $id;

    /**
     * @ORMColumn(type="string", length=2048, nullable=true)
     * @Groups({"bookmarkdata:read", "bookmarkdata:collection:read"})
     */
    private $screenshot;

    /**
     * @ORMColumn(type="string", length=2048)
     * @Groups({"bookmarkdata:read", "bookmarkdata:collection:read"})
     */
    private $dump;

    /**
     * @ORMColumn(type="datetime")
     * @Groups({"bookmarkdata:read", "bookmarkdata:collection:read"})
     */
    private $created_at;

    /**
     * @ORMColumn(type="datetime", nullable=true)
     */
    private $updated_at;

    public function getId(): ?int
    {
        return $this->id;
    }

    public function getScreenshot(): ?string
    {
        return $this->screenshot;
    }

    public function setScreenshot(?string $screenshot): self
    {
        $this->screenshot = $screenshot;

        return $this;
    }

    public function getDump(): ?string
    {
        return $this->dump;
    }

    public function setDump(string $dump): self
    {
        $this->dump = $dump;

        return $this;
    }

    public function getCreatedAt(): ?DateTimeInterface
    {
        return $this->created_at;
    }

    public function setCreatedAt(DateTimeInterface $created_at): self
    {
        $this->created_at = $created_at;

        return $this;
    }

    public function getUpdatedAt(): ?DateTimeInterface
    {
        return $this->updated_at;
    }

    public function setUpdatedAt(?DateTimeInterface $updated_at): self
    {
        $this->updated_at = $updated_at;

        return $this;
    }
}

In result property created_at ignored.

{
    "@context": "/api/contexts/BookmarkData",
    "@id": "/api/bookmark_datas",
    "@type": "hydra:Collection",
    "hydra:member": [
        {
            "@id": "/api/bookmark_datas/1",
            "@type": "BookmarkData",
            "id": 1,
            "screenshot": "This is screen",
            "dump": "This is dump"
        }
    ],
    "hydra:totalItems": 1
}

I’m using Docker.
My dependencies.

{
    "type": "project",
    "license": "proprietary",
    "require": {
        "php": "^7.1.3",
        "ext-ctype": "*",
        "ext-iconv": "*",
        "api-platform/api-pack": "^1.2",
        "doctrine/doctrine-migrations-bundle": "^2.1",
        "easycorp/easyadmin-bundle": "^2.3",
        "lexik/jwt-authentication-bundle": "^2.6",
        "nesbot/carbon": "^2.32",
        "symfony/console": "4.4.*",
        "symfony/dotenv": "4.4.*",
        "symfony/flex": "^1.3.1",
        "symfony/framework-bundle": "4.4.*",
        "symfony/security-bundle": "4.4.*",
        "symfony/yaml": "4.4.*"
    },
    "require-dev": {
        "doctrine/doctrine-fixtures-bundle": "^3.3",
        "fzaninotto/faker": "^1.9",
        "symfony/maker-bundle": "^1.15",
        "symfony/profiler-pack": "^1.0"
    },
    "config": {
        "preferred-install": {
            "*": "dist"
        },
        "sort-packages": true
    },
    "autoload": {
        "psr-4": {
            "App": "src/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "AppTests": "tests/"
        }
    },
    "replace": {
        "paragonie/random_compat": "2.*",
        "symfony/polyfill-ctype": "*",
        "symfony/polyfill-iconv": "*",
        "symfony/polyfill-php71": "*",
        "symfony/polyfill-php70": "*",
        "symfony/polyfill-php56": "*"
    },
    "scripts": {
        "auto-scripts": {
            "cache:clear": "symfony-cmd",
            "assets:install %PUBLIC_DIR%": "symfony-cmd"
        },
        "post-install-cmd": [
            "@auto-scripts"
        ],
        "post-update-cmd": [
            "@auto-scripts"
        ]
    },
    "conflict": {
        "symfony/symfony": "*"
    },
    "extra": {
        "symfony": {
            "allow-contrib": false,
            "require": "4.4.*"
        }
    }
}

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 1

Your email address will not be published. Required fields are marked *


Didier

Didier

I have the same problem with Symfony 5 and API-Platform 2.5…
Do you find a solution ?