Hi, friends, being AI enthusiast, I'm an MBA, CEO and CPO who loves building products. I share my insights on leadership, tech, innovation and management.
I started a new repo with some test-commands. Test 1, 2, 4 are working, 3 fails, 5 I still have to build. Test 6 probably is a test for trying to fix the name-converter without a decorator. (or maybe perhaps with an decorator)
Hi, friends, being AI enthusiast, I'm an MBA, CEO and CPO who loves building products. I share my insights on leadership, tech, innovation and management.
Nice! Will have a closer look at it this weekend. But already love the nested object implementation. The posts are not converted into objects, not sure if the transformer is actually triggered.
Hi, friends, being AI enthusiast, I'm an MBA, CEO and CPO who loves building products. I share my insights on leadership, tech, innovation and management.
I tried to use the snake_case to CamelCase converter in Symfony 8.0 but I'm not able to make it work. You already tried this? Only way I get to function snake_case combined with CamelCase is by using the
#[Map(source: 'snake_case')]
public string $snakeCase,
Hi, friends, being AI enthusiast, I'm an MBA, CEO and CPO who loves building products. I share my insights on leadership, tech, innovation and management.
All works fine)) Clean Symfony 8.0 installed just for you))
class ApiUserDto
{
public function __construct(
#[Map(source: 'user_id')]
public int $userId,
#[Map(source: 'first_name')]
public string $firstName,
#[Map(source: 'last_name')]
public string $lastName
) {}
}
Thanks for your effort! Sorry for the confusion.. That actually works great and that's the way I'm doing it right now. However, I'm trying to implement this name-converter:
And I can't get it to work.. Not sure what I'm missing.. Probably it moved to another location (config/packages/serializer.yaml) but that's in my opinion about serializing and not about object mapping.. So I'm a little confused.
But when I get this name-converter to work that will save me a great amount of time :)
Hi, friends, being AI enthusiast, I'm an MBA, CEO and CPO who loves building products. I share my insights on leadership, tech, innovation and management.
Yeah that's also a good option! But that draws me to the conclusion that this configuration via the framework.yaml file with the name_converter isn't working as we expected. Because I would love the implementation. And I tried it in the serializer.yaml file but that also didn't work out.
Back again. I thought I had some working code, also for relations. However I made an mistake with git repos and lost track... How would you suggest to handle IRI's in the DTO structure?
I thought ApiPlatform deserialization already included IRI to object conversion. However my code doesn't seem to understand this.
###ActivityApiResource
#[ApiResource(
shortName: 'Activity',
stateOptions: new Options(entityClass: ActivityEntity::class),
)]
#[Post(
uriTemplate: '/activitities'
input: ActivityRequestDto::class,
output: ActivityResponseDto::class,
processor: Processor::class,
//map: false, /** default true -> forces RequestDto to execute IriToObject */
)]
###ActivityRequestDto.php
#[Map(target: Comment::class)]
class ActivityRequestDto implements RequestDto
{
#[Assert\Length(min: 3, max: 1024)]
public string $activity;
#[Assert\Regex(RegexValidations::IRI)]
#[Map(transform: [IriToObject::class])]
public string $user;
}
###IriToObject.php
public function __invoke(
mixed $value,
object $source,
?object $target,
): object {
return (is_string($value))
? $this->iriConverter->getResourceFromIri($value)
: throw new Exception('Unable to convert IRI into object')
;
}
However getResourceFromIri does not return the object. It returns the UserResponseDto which ofcourse is not part of the relation. That's the User entity. So mapper concludes:
Expected argument of type "App\Entity\User", \"App\Dto\User\Response\UserResponseDto" given at property path "user".
I could improve the IriToObject script. However would love to rely on ApiPlatform logic...
We're a place where coders share, stay up-to-date and grow their careers.
And another issue popped up; I'm trying to map an array of objects.
Documentation states: (documentation)
However I'm not able to make this happen, did you?
Weekend project for me) Turning this article into a GitHub repo.
I started a new repo with some test-commands. Test 1, 2, 4 are working, 3 fails, 5 I still have to build. Test 6 probably is a test for trying to fix the name-converter without a decorator. (or maybe perhaps with an decorator)
repo
It's done) github.com/mattleads/SymfonyObject...
Nice! Will have a closer look at it this weekend. But already love the nested object implementation. The posts are not converted into objects, not sure if the transformer is actually triggered.
Updated. Check once more.
Hi @mattleads ,
I tried to use the snake_case to CamelCase converter in Symfony 8.0 but I'm not able to make it work. You already tried this? Only way I get to function snake_case combined with CamelCase is by using the
#[Map(source: 'snake_case')]public string $snakeCase,
Thanks in advance! Regards Arthur
All works fine)) Clean Symfony 8.0 installed just for you))
Thanks for your effort! Sorry for the confusion.. That actually works great and that's the way I'm doing it right now. However, I'm trying to implement this name-converter:
serializer.name_converter.snake_case_to_camel_caseAnd I can't get it to work.. Not sure what I'm missing.. Probably it moved to another location (
config/packages/serializer.yaml) but that's in my opinion about serializing and not about object mapping.. So I'm a little confused.But when I get this name-converter to work that will save me a great amount of time :)
I'd suggest to use Decorator))
And no more #Map needed))
Yeah that's also a good option! But that draws me to the conclusion that this configuration via the framework.yaml file with the name_converter isn't working as we expected. Because I would love the implementation. And I tried it in the serializer.yaml file but that also didn't work out.
So a few months later, I still could not let this rest.. And found the real solution without any code: api-platform.com/docs/v3.4/core/se... :)
Back again. I thought I had some working code, also for relations. However I made an mistake with git repos and lost track... How would you suggest to handle IRI's in the DTO structure?
I thought ApiPlatform deserialization already included IRI to object conversion. However my code doesn't seem to understand this.
However getResourceFromIri does not return the object. It returns the UserResponseDto which ofcourse is not part of the relation. That's the User entity. So mapper concludes:
Expected argument of type "App\Entity\User", \"App\Dto\User\Response\UserResponseDto" given at property path "user".
I could improve the IriToObject script. However would love to rely on ApiPlatform logic...