mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Fixed problem that all properties in snake_case style were considered readOnly by API Platform
This commit is contained in:
parent
f285061a76
commit
ccb94c8a13
2 changed files with 74 additions and 0 deletions
|
@ -22,6 +22,7 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Entity\Base;
|
namespace App\Entity\Base;
|
||||||
|
|
||||||
|
use ApiPlatform\Metadata\ApiProperty;
|
||||||
use Doctrine\DBAL\Types\Types;
|
use Doctrine\DBAL\Types\Types;
|
||||||
use DateTime;
|
use DateTime;
|
||||||
use Doctrine\ORM\Mapping as ORM;
|
use Doctrine\ORM\Mapping as ORM;
|
||||||
|
@ -36,6 +37,7 @@ trait TimestampTrait
|
||||||
* @var \DateTimeInterface|null the date when this element was modified the last time
|
* @var \DateTimeInterface|null the date when this element was modified the last time
|
||||||
*/
|
*/
|
||||||
#[Groups(['extended', 'full'])]
|
#[Groups(['extended', 'full'])]
|
||||||
|
#[ApiProperty(writable: false)]
|
||||||
#[ORM\Column(name: 'last_modified', type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
|
#[ORM\Column(name: 'last_modified', type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
|
||||||
protected ?\DateTimeInterface $lastModified = null;
|
protected ?\DateTimeInterface $lastModified = null;
|
||||||
|
|
||||||
|
@ -43,6 +45,7 @@ trait TimestampTrait
|
||||||
* @var \DateTimeInterface|null the date when this element was created
|
* @var \DateTimeInterface|null the date when this element was created
|
||||||
*/
|
*/
|
||||||
#[Groups(['extended', 'full'])]
|
#[Groups(['extended', 'full'])]
|
||||||
|
#[ApiProperty(writable: false)]
|
||||||
#[ORM\Column(name: 'datetime_added', type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
|
#[ORM\Column(name: 'datetime_added', type: Types::DATETIME_MUTABLE, options: ['default' => 'CURRENT_TIMESTAMP'])]
|
||||||
protected ?\DateTimeInterface $addedDate = null;
|
protected ?\DateTimeInterface $addedDate = null;
|
||||||
|
|
||||||
|
|
71
src/Services/SnakeCasePropertyAccessExtractor.php
Normal file
71
src/Services/SnakeCasePropertyAccessExtractor.php
Normal file
|
@ -0,0 +1,71 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace App\Services;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\AsTaggedItem;
|
||||||
|
use Symfony\Component\DependencyInjection\Attribute\Autowire;
|
||||||
|
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
|
||||||
|
use Symfony\Component\PropertyInfo\PropertyAccessExtractorInterface;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Workaround for using snake_case properties with ReflectionExtractor until this PR is merged:
|
||||||
|
* https://github.com/symfony/symfony/pull/51697
|
||||||
|
*/
|
||||||
|
#[AsTaggedItem('property_info.access_extractor', priority: 0)]
|
||||||
|
class SnakeCasePropertyAccessExtractor implements PropertyAccessExtractorInterface
|
||||||
|
{
|
||||||
|
|
||||||
|
public function __construct(#[Autowire(service: 'property_info.reflection_extractor')]
|
||||||
|
private readonly PropertyAccessExtractorInterface $reflectionExtractor)
|
||||||
|
{
|
||||||
|
//$this->reflectionExtractor = new ReflectionExtractor();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function isReadable(string $class, string $property, array $context = [])
|
||||||
|
{
|
||||||
|
//Null means skip this extractor
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Camelizes a given string.
|
||||||
|
*/
|
||||||
|
private function camelize(string $string): string
|
||||||
|
{
|
||||||
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $string)));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public function isWritable(string $class, string $property, array $context = [])
|
||||||
|
{
|
||||||
|
//Check writeablity using a camelized property name
|
||||||
|
$isWriteable = $this->reflectionExtractor->isWritable($class, $this->camelize($property), $context);
|
||||||
|
//If we found a writeable property that way, return true
|
||||||
|
if ($isWriteable === true) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
//Otherwise skip this extractor
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue