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

useful error when data is not an object #1313

Merged
merged 4 commits into from
Apr 25, 2021
Merged

Conversation

dbu
Copy link
Contributor

@dbu dbu commented Apr 21, 2021

Q A
Bug fix? (yes)
New feature? no
Doc updated -
BC breaks? no
Deprecations? no
Tests pass? see ci
Fixed tickets -
License MIT

I was spending some time trying to figure out why i get:

TypeError: "Argument 2 passed to JMS\Serializer\JsonSerializationVisitor::startVisitingObject() must be an object, array given

Turns out i misconfigured a property that was supposed to be an array of objects as an object with the JMS annotations. From the error it is impossible to know what is wrong. With this exception, the path to the offending property is printed and we say what is actually expected.

@@ -234,6 +235,10 @@ public function accept($data, ?array $type = null)
throw new ExcludedClassException();
}

if (!is_object($data)) {
throw new InvalidArgumentException('Value at '.$this->context->getPath().' is expected to be an object of class '.$type['name'].' but is of type '.gettype($data));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getPath only gives the model class but not the current property. do we know the name of the property we are currently looking at?

@goetas goetas merged commit b4b7fa9 into schmittjoh:master Apr 25, 2021
@goetas
Copy link
Collaborator

goetas commented Apr 25, 2021

thanks!

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

Successfully merging this pull request may close these issues.

2 participants