Skip to content

JSON:API: request documents with no data, no type or a wrong type are accepted with 201 #8703

Closed

Description

@aszenz

API Platform version(s) affected

4.4.4

Description

The JSON:API denormalizer does not check the structure of the request document. A body that is not a valid JSON:API document is accepted, and the resource is created with its default values.

The spec requires:

  • the top level of a request document is an object with a data member (document structure),
  • data is a resource object with a type member, and attributes is an object (resource objects),
  • "A server MUST return 409 Conflict when processing a POST request in which the resource object's type is not among the type(s) that constitute the collection represented by the endpoint" (creating resources).

JsonApi\Serializer\ItemNormalizerTrait::denormalize() reads $data['data']['attributes'] ?? [] and $data['data']['relationships'] ?? [], and it does not check the other members.

How to reproduce

#[ApiResource(operations: [
    new Get(provider: [self::class, 'provide']),
    new Post(processor: [self::class, 'process']),
])]
final class Novel
{
    public int $id = 1;
    public string $title = 'Dune';

    public static function provide(): self { return new self(); }
    public static function process(self $data, Operation $operation): self { return $data; }
}

Each of these POST /novels bodies answers 201 Created:

Body Problem
"Y" not an object
42 not an object
{"title":"A"} no data member
{"data":"x"} data is not a resource object
{"data":{"type":"Novel","attributes":["x"]}} attributes is not an object
{"data":{"attributes":{"title":"A"}}} no type member
{"data":{"type":"Author","attributes":{"title":"A"}}} type does not match the endpoint

Response for "Y":

{"data":{"id":"/novels/1","type":"Novel","attributes":{"_id":1,"title":"Dune"}}}

A validation constraint on a required property hides the problem in some cases, but a resource with optional properties or defaults is created from any body.

Expected:

  • A body that is not a JSON:API document, or has no data object, no type or a non-object attributes/relationships: 400 Bad Request.
  • A type that does not match the resource on POST: 409 Conflict.

Additional Context

Checked on api-platform/core v4.4.4, Symfony 7.4, PHP 8.5, in an empty Symfony app. The related code in src/JsonApi/Serializer is the same on main, except for the error status cast (#8366).

The app has only FrameworkBundle, TwigBundle and ApiPlatformBundle, with this config:

$c->extension('api_platform', [
    'mapping' => ['paths' => ['%kernel.project_dir%/src/ApiResource']],
    'formats' => ['jsonapi' => ['application/vnd.api+json']],
    'error_formats' => ['jsonapi' => ['application/vnd.api+json'], 'jsonproblem' => ['application/problem+json']],
    'doctrine' => ['enabled' => false],
]);

All requests send Accept: application/vnd.api+json and, when they have a body, Content-Type: application/vnd.api+json. The responses are from the prod environment.

Activity

  1. soyuka commented on Nov 12, 2026

    @soyuka
    Member

    Malformed documents are rejected with a 400 since #8736, and #8739 adds the opt-in jsonapi.strict_resource_type flag: a missing type returns a 400 and a type not matching the endpoint returns a 409 on POST. It's off by default as it would break clients not sending type. Closing, thanks!

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions