FriendsOfSymfony/FOSRestBundle

Unwanted magic: converter fos_rest.request_body retrive the actualy entity from database?!

gremo opened this issue · 0 comments

gremo commented

I'm spending the entire day on this.

I've configured SensioFrameworkExtraBundle to enable converters but disable auto convert feature:

sensio_framework_extra:
    request:
        converters: true
        auto_convert: false

fos_rest:
    body_converter:
        enabled: true
        validate: true

As proof this controller action will throw because Product isn't a service, and action can't be auto-wired, exactly what I want:

Cannot autowire argument $product of "App\Controller\ApiController::createProductAction()": it references class "App\Entity\Product" but no such service exists.

/**
 * @Rest\Post("/products", name="_api_post_product")
 * @Rest\View()
 */
public function createProductAction(Product $product, ConstraintViolationListInterface $validationErrors)
{
}

Now I want to use the param converter from FOSRestBundle so POST request body will be deserialized into a fresh new Product object and validated:

/**
 * @ParamConverter("product", class=Product::class, converter="fos_rest.request_body")`
 */

The problem is that the converter is fetching a valid record from the database, when POST payload is a valid id like this:

{
  "id": "id-that-exists"
}

What's going on here? I don't want to update an existing object, i just want all the values to be deserialized into a new object to be persisted.

  • friendsofsymfony/rest-bundle: 2.x
  • symfony: 4.x
jms_serializer:
    default_context:
        serialization:
            serialize_null: true
        deserialization:
            serialize_null: true

    property_naming:
        id: 'jms_serializer.identical_property_naming_strategy'