Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file upload example problem with integer input value #1443

Open
pinksnotdead opened this issue Oct 13, 2021 · 2 comments
Open

file upload example problem with integer input value #1443

pinksnotdead opened this issue Oct 13, 2021 · 2 comments

Comments

@pinksnotdead
Copy link

All i've done was from this example
https://api-platform.com/docs/core/file-upload/#uploading-to-an-existing-resource-with-its-fields

I've got problem with posting integer value. Response: "The type of the "size" attribute must be "int", "string" given."

In my MongoDB Document is a simple integer field and new file field:

// ...
#[ApiResource(
    collectionOperations: [
        'get',
        'post' => [
            'input_formats' => [
                'multipart' => ['multipart/form-data'],
            ],
            "openapi_context" => [
                "requestBody" => [
                    "content" => [
                        "multipart/form-data" => [
                            "schema" => [
                                "type" => "object",
                                "properties" => [
                                    "size" => [
                                        "type" => "integer",
                                        "format" => "int32"
                                    ],
                                    "file" => [
                                        "type" => "string",
                                        "format" => "binary"
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]

        ],
    ],
)]
// ...
    /**
     * @ODM\Field(type="integer")
     */
    public $size;

// ...

    public function getSize(): ?int
    {
        return $this->size;
    }

    public function setSize(int $size): void
    {
        $this->size = $size;
    }

// ...
curl -X 'POST' \
  'http://localhost/api/endpoint_name' \
  -H 'accept: application/ld+json' \
  -H 'Content-Type: multipart/form-data' \
  -F 'size=123' \
  -F '[email protected];type=image/jpeg'

I think there is a problem with sending data via curl -F. Symfony or api-platform auto-cast data to string. Sending using Content-Type: application/ld+json works correctly, because swagger use object -d flag.

My workaround was to modify MultipartDecoder class from example by casting possible integer coming from request:

// src/Encoder/MultipartDecoder.php

// ...
        return array_map(static function (string $element) {
            // Multipart form values will be encoded in JSON.
            $decoded = json_decode($element, true);

            // this avoid converting int to string
            if (is_numeric($element)) {
                $element = (int)$element;
            }

            return \is_array($decoded) ? $decoded : $element;
        }, $request->request->all()) + $request->files->all();
// ...

so... am i right and docs should be updated or an error is somewhere else?

@pinksnotdead
Copy link
Author

the same error with MySQL, so it's related with serializer and exacly with this example

@YannLe
Copy link

YannLe commented Dec 10, 2022

Had the same problem. I think a better fix would be something like this :

return \is_array($decoded) | is_int($decoded) ? $decoded : $element;

I suspect booleans can also be an issue but I did not test it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants