Skip to content

[Bug]: Switching from application/x-www-form-urlencoded to application/json when request body has an integer property shows wrong object #2533

@esorinas

Description

@esorinas

Version

5.5.0

Description

Demo

Attached with this issue there is a screen recording of the issue; it's a little hard to explain in words but very easy to understand when observed.

Steps to reproduce:

  1. Make a DTO with an integer property (OA\Property (#[OA\Property(type: 'integer')])
  2. Make a POST/PUT route with a RequestBody where the content contains (at least) two MediaType objects: one should be of type application/x-www-form-urlencoded and the other application/json. Both should use the DTO defined in 1.
  3. Go to /api/doc and find the call you defined.
  4. Click on "Try out" making sure that application/x-www-form-urlencoded is selected in the dropdown
  5. Switch the dropdown to 'application/json`

Expected result:

  • The content is changed to a JSON with the integer field containing 0. For example: { "myIntegerVar": 0 }.

Result:

  • The content is changed to a JSON where each key is an object of the form { "myIntegerVar": { "value": "0", "errors": [] } }.

Code example

With a fresh symfony and nelmio/api-doc-bundle installation, the following code shows the error.
If anyone would find it useful, I can provide the full project.

Controller

<?php
// src/Controller/DummyController.php

namespace App\Controller;

use App\Model\DummyRequestModel;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Nelmio\ApiDocBundle\Attribute\Model;
use OpenApi\Attributes as OA;
use OpenApi\Attributes\RequestBody;
use OpenApi\Attributes\Schema;
use Symfony\Component\Routing\Annotation\Route;

class DummyController extends AbstractController
{
    #[Route('/api/dummy', methods: ['POST'])]
    #[RequestBody(content: [
        new OA\MediaType(
            mediaType: 'application/x-www-form-urlencoded',
            schema: new Schema(ref: new Model(type: DummyRequestModel::class))
        ),
        new OA\MediaType(
            mediaType: 'application/json',
            schema: new Schema(ref: new Model(type: DummyRequestModel::class))
        ),
    ])]
    public function dummyAction()
    {
        return $this->json(new DummyRequestModel());
    }
}

DTO

<?php
// src/Model/DummyRequestModel.php

namespace App\Model;

use OpenApi\Attributes as OA;

class DummyRequestModel
{
    #[OA\Property(type: 'integer')]
    public int $foo;
}

Thanks in advance for all the thoughts and help :)

JSON OpenApi

JSON OpenApi
{
    "openapi": "3.0.0",
    "info": {
        "title": "My App",
        "description": "This is an awesome app!",
        "version": "1.0.0"
    },
    "paths": {
        "/api/dummy": {
            "post": {
                "operationId": "post_app_dummy_dummy",
                "requestBody": {
                    "content": {
                        "application/x-www-form-urlencoded": {
                            "schema": {
                                "$ref": "#/components/schemas/DummyRequestModel"
                            }
                        },
                        "application/json": {
                            "schema": {
                                "$ref": "#/components/schemas/DummyRequestModel"
                            }
                        }
                    }
                },
                "responses": {
                    "default": {
                        "description": ""
                    }
                }
            }
        }
    },
    "components": {
        "schemas": {
            "DummyRequestModel": {
                "required": [
                    "foo"
                ],
                "properties": {
                    "foo": {
                        "title": "Get the value of foo",
                        "type": "integer"
                    }
                },
                "type": "object"
            }
        }
    }
}

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions