Skip to content

Wrong error code when denormalizing a non existant item #6116

Description

@KDederichs

API Platform version(s) affected: 3.2.11

Description
While writing tests I noticed that when you try to denormalize a non existing item it'll throw an exception with statuscode 500.

It shouldn't error out with a 500 error since that's not the expected error code for cases like that.

How to reproduce

#[ApiResource]
class Foo {
   prive ?Bar $bar = null;
}
#[ApiResource]
class Bar {
}

and then try to create a Foo with a reference to a non existing Bar.
Possible Solution

There's two solutions for this:
Change the error code to 404, to match the not found paradime.

Optionally allow not found items to serialize to null so users can throw their own errors with Validations.

Additional Context

    "title": "An error occurred",
    "detail": "Item not found for \"/api/bar/018d1ca6-da89-77c2-93a9-2b3593055e42\".",
    "status": 500,
    "type": "/errors/500",
    "trace": [
        {
            "file": "/app/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php",
            "line": 860,
            "function": "denormalizeRelation",
            "class": "ApiPlatform\\Serializer\\AbstractItemNormalizer",
            "type": "->"
        },
...

Activity

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

Metadata

Metadata

Assignees

Labels

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions