cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Why does json-api trimming spaces in my attributes when updating? #626

Closed sunnydesign closed 2 years ago

sunnydesign commented 2 years ago

Hi!

I send PATCH request with this data (value contains a space character at the end):

{
    "data": {
        "type": "parsers",
        "id": "7",
        "attributes": {
            "myAttribute": "myValue ",
        }
    }
}

But space characters are trimmed before validating in my ParserRequest and I can't influence this in any way. Therefore the value is saved to the DB without a space! And I get response without a space in myAttribute.

{
    "data": {
        "type": "parsers",
        "id": "7",
        "attributes": {
            "myAttribute": "myValue",
        }
    }
}

How can I avoid this? I want to save the data as I sent it.

lindyhopchris commented 2 years ago

Trimming strings is done by the Laravel TrimStrings middleware. In a default Laravel app install, that's in your middleware stack here: https://github.com/laravel/laravel/blob/196a27593b288f7e4d3ce30585ebd881933726b4/app/Http/Kernel.php#L22

If you remove that from your middleware stack, you should find the values are not trimmed.

sunnydesign commented 2 years ago

Thank you very much! I wish you many years of life dear friend!

lindyhopchris commented 2 years ago

aha, thank you!!