Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[ObjectMapper] if 'isset' and default values not working #60848

Open
@tacman

Description

@tacman

Symfony version(s) affected

7.3.0

Description

It appears that default values don't work with the ObjectMapper.

OptionsResolver has a method to ignore missing properties, but it seems like it shouldn't need a way to ignore properties if they have a default value.

How to reproduce

        $mapper = new ObjectMapper();
        $target = 'App\Dto\MyClass';
        $obj = (object) ['id'=>'abc'];
        $entity = $mapper->map($obj, $target);
<?php

namespace App\Dto\;

class MyClass
{
    public function __construct(
        public string  $id,

        #[Map(source: 'optional', if: 'isset')]
        public ?string $optional = null,
    {

    }
}

PixieImportService.php on line 755:
ErrorException {#10250
#message: "Warning: Undefined property: stdClass::$optional"
#code: 0
#file: "
/home/tac/sites/mus/vendor
/symfony/object-mapper/
ObjectMapper.php
"
#line: 170
#severity: E_WARNING
trace: {▶}

Possible Solution

No response

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions