NelmioApiDocBundle icon indicating copy to clipboard operation
NelmioApiDocBundle copied to clipboard

Unable to render a Form Model to show the 400 Bad Request response?

Open DavidGarciaCat opened this issue 2 years ago • 0 comments

Good day. I found this bundle quite useful, but I encountered a case where either I am unable to do it due to a lack of integration (and documentation) or due to a missing feature:

When it's time to prepare the Request Body to create a new user account, the solution seems quite simple:

#[OA\RequestBody(
    required: true,
    content: new OA\JsonContent(ref: new Nelmio\Model(type: UserRegistrationType::class))
)]
{
  "username": "string",
  "password": {
    "first": "string",
    "second": "string"
  }
}

When it's time to document the Response Body, it's very simple to render the 2xx status code too:

#[OA\Response(
    response: Response::HTTP_CREATED,
    description: 'Created',
    content: new OA\JsonContent(ref: new Nelmio\Model(type: User::class, groups: ['api']))
)]
{
  "uuid": "string",
  "email": "string"
}

However, when it's time to expose a Bad Request response (by returning the Form object) I couldn't find a way to set what type of content I need to return here, because if I set the Form Type (as I did for the Request) then I get the same JSON string

#[OA\Response(
    response: Response::HTTP_BAD_REQUEST,
    description: 'Bad Request',
    // ...
)]

The expected JSON output should be like the one we get at the time we return the Form:

{
  "code": 400,
  "message": "Validation Failed",
  "errors": {
    "children": {
      "username": {
        "errors": [
          "This value should not be blank."
        ]
      },
      "password": {
        "children": {
          "first": {
            "errors": [
              "This value should not be blank."
            ]
          },
          "second": {}
        }
      }
    }
  }
}

Note: In this specific case, the setup includes the FOS Rest Bundle + JMS Serializer Bundle + Nelmio API Doc Bundle

Does anyone know if this is a missing feature, or it's just I didn't find a way to solve it?

If this is a missing feature, then I would love know if there's any roadmap to solve this one.

Thanks all,

DavidGarciaCat avatar Jul 02 '22 11:07 DavidGarciaCat