1

I have little problem with FOSRestBundle in Symfony 3.2. First, let me show configs and action:

FOSRest conf:

fos_rest:
    versioning: true
    body_converter:
        enabled: true
        validate: true
        validation_errors_argument: validationErrors
    view:
        mime_types:
            json: ['application/vnd.service_name+json; charset=UTF-8', 'application/json; charset=UTF-8', 'application/json']
    param_fetcher_listener: true
    body_listener:
        decoders:
            json: fos_rest.decoder.json
    routing_loader:
        default_format: json
    format_listener:
        enabled: true
    serializer:
        serialize_null: true

Routing:

api_panel_external_address_services_get:
    path: /addresses/{addressId}/services
    methods: GET
    defaults:
        _controller: AppBundle:Api/ManagementPanel/External/Address:getServices
        _format: json
    requirements:
        addressId: \d+

Action:

 /**
 * @ApiDoc(
 *  section="Panel/External",
 *  resource=true,
 *  description="Get external services",
 *  requirements={
 *    {"name"="addressId", "dataType"="string", "description"="External address id"}
 *  },
 *  statusCodes={
 *      200="Returned when success",
 *      400="Returned when data has errors (did not pass validation)",
 *      404="Returned when address was not found",
 *  }
 * )
 *
 * @ParamConverter("externalAddress", class="Company\Model\ExternalAddress", options={"mapping"={"addressId":"id"}})
 */
public function getServicesAction(ExternalAddress $externalAddress, ConstraintViolationListInterface $validationErrors): Response
{
    if ($validationErrors->count())
    {
        $view = $this->view($validationErrors, Response::HTTP_BAD_REQUEST);

        return $this->handleView($view);
    }

    $externalServices = $externalAddress->getExternalServices();

    $services = [];
    foreach ($externalServices as $externalService)
    {
        $services[] = [
            'id'                  => $externalService->getId(),
            'name'                => $externalService->getName(),
        ];
    }

    return $this->handleView($this->view($services, Response::HTTP_OK));
}

Problem: when I go to route /api/v1/panel/external/addresses/801/services I get an different errors in different environments.

POSTMAN: (code: 400): Could not decode JSON, syntax error - malformed JSON.

Browser: "code":415,"message":"Unsupported Media Type","exception":[{"message":"The format \"\" is not supported for deserialization."

But when I delete , ConstraintViolationListInterface $validationErrors and

    if ($validationErrors->count())
    {
        $view = $this->view($validationErrors, Response::HTTP_BAD_REQUEST);

        return $this->handleView($view);
    }

from Action, everything is OK and i get the JSON Response.

Am I doing sth wrong? I want to have this $validationErrors variable as it is in FOSRestBundle docs.

Hope I get some help here.

Ok guys, I found the answer in code. This is http-method GET and when I'm not providing any content body (json) there is an error. When you have get methods you should delete $validationErrors from action. IMO there should be check in FOSRestBundle if $method === 'GET'. Without this its confusing, but still make perfect sense - you don't need to validate body when your method is GET.

Hope this will save someone's hours of searching.

pejkosz94
  • 11
  • 3

0 Answers0