Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization in requests is not working #252

Open
vanam opened this issue Mar 25, 2021 · 2 comments
Open

Deserialization in requests is not working #252

vanam opened this issue Mar 25, 2021 · 2 comments

Comments

@vanam
Copy link
Contributor

vanam commented Mar 25, 2021

Hi,
I have trouble deserializing a datetime object from request body.

I added birthDate field to CreateUserReqDto. Should I deserialize it manually?

In a documentation I found that DTOs should inherit Apitte\Core\Mapping\Request\BasicEntity and presumably override normalize method (which is not mentioned in the documentation). But it doesn't work.

I guess the same problem is going to be with other non-primitive types.

@vanam
Copy link
Contributor Author

vanam commented Mar 25, 2021

Rubberducking at its finest :(

Solution is to use typed properties (and fix typos I made during example creation).

However, new bug appears: when I try to use typed property for nullable attribute
Typed property App\Domain\Api\Request\CreateUserReqDto::$password must not be accessed before initialization caused by JsonDispatcher.

And the question about inheritance and normalize method remains.

@vanam
Copy link
Contributor Author

vanam commented Mar 25, 2021

I tried to create a structured request and everything is an array.

App\Domain\Api\Request\CreateUsersReqDto
  users: array
    0 => array
      'email' => '[email protected]'
      'name' => 'Karel'
      'surname' => 'Vommáčka'
      'username' => 'vomajz'
      'birthDate' => '2020-01'

Now the normalize method would be useful. I could hide \Nette\Utils\Arrays::toObject there. And some sort of validation would be nice.

I guess the only way to do it automatically would be using some sort of annotation at the attribute since arrays in PHP have no type constraints. This is enough for OpenApi definition generator so it could be used for deserialization.

Edit: \Nette\Utils\Arrays::toObject is not enough. It does not deserialize DateTime :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant