I'm using API platform for my booking application.
Booking has many to 1 relationship with a department.
When I create a new booking by sending a POST request with an empty string as a value for the department I am expecting a validation error but instead, I got 'invalid IRI'
Booking entity
/** * @ApiResource( * itemOperations={ * "get"={ * "normalization_context"={ * "groups"={"read-item"} * } * }, * "put"={ * "denormalization_context"={ * "groups"={"put"} * } * } * } * @ORM\Entity(repositoryClass="App\Repository\BookingRepository")class Booking{ /** * @ORM\ManyToOne(targetEntity="App\Entity\Department", inversedBy="bookings") * @ORM\JoinColumn(nullable=false) * @Assert\NotBlank * @Groups({"read-item", "put"}) */ private $department;}
Department entity
/** * @ApiResource( * collectionOperations={"get"}, * itemOperations={"get"} * ) * @ORM\Entity(repositoryClass="App\Repository\DepartmentRepository") */class Department{ /** * @ORM\OneToMany(targetEntity="App\Entity\Booking", mappedBy="department") */ private $bookings;}
e.g sample request to create a booking (POST)
{"department": ""}
Error:
{"@context": "/api/contexts/Error","@type": "hydra:Error","hydra:title": "An error occurred","hydra:description": "Invalid IRI \"\".","trace": [ {"namespace": "","short_class": "","class": "","type": "","function": "","file": "/var/www/html/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php","line": 418,"args": [] }, {"namespace": "ApiPlatform\\Core\\Serializer","short_class": "AbstractItemNormalizer","class": "ApiPlatform\\Core\\Serializer\\AbstractItemNormalizer","type": "->","function": "denormalizeRelation","file": "/var/www/html/vendor/api-platform/core/src/Serializer/AbstractItemNormalizer.php","line": 685,"args": [ ["string","department" ], ["object","ApiPlatform\\Core\\Metadata\\Property\\PropertyMetadata" ], ["string","App\\Entity\\Department" ], ["string","" ], ["string","json" ], ["array", {"operation_type": ["string","collection" ],"collection_operation_name": ["string","post" ],"api_allow_update": ["boolean", false ],"resource_class": ["string","App\\Entity\\Department" ],"input": ["null", null ],"output": ["null", null ],"request_uri": ["string","/api/bookings" ],"uri": ["string","http://localhost/api/bookings" ],"skip_null_values": ["boolean", true ],"api_denormalize": ["boolean", true ],"cache_key": ["string","db505854694e53aec6831eb427a03028" ] } ] ] }, }
Please advise if I'm missing something