MasoniteFramework / masonite

The Modern And Developer Centric Python Web Framework. Be sure to read the documentation and join the Discord channel for questions: https://discord.gg/TwKeFahmPZ
http://docs.masoniteproject.com
MIT License
2.21k stars 126 forks source link

Invalid Request Parsing #746

Closed yubarajshrestha closed 1 year ago

yubarajshrestha commented 1 year ago

Describe the bug

When there's a single item in an array or dictionary, masonite converts it into normal form like string or number. Take a look at the following example of a POST request.

For eg:

{
    "name": "Family",
    "contacts": [ "User One" ]
}

The above request gives an invalid record in the controller. Take a look at the response:

# controller
def my_controller(self, request: Request):
    data = request.all()

The above data has the following data in it, which is wrong because the user is expecting an array there.

{
    "name": "Family",
    "contacts": "User One"
}

Expected behaviour

The correct response should be something like the below:

{
    "name": "Family",
    "contacts": [
        "User One"
    ]
}

Steps to reproduce the bug

  1. Post the following data in your post route:
    {
    "name": "Family",
    "contacts": ["User One"]
    }
  2. Check the response, you'll get the wrong response, which should look like this:
    {
    "name": "Family",
    "contacts": "User One"
    }
  3. But the response should be like this:
    {
    "name": "Family",
    "contacts": ["User One"]
    }

Screenshots

No response

OS

macOS

OS version

Ventura 13.2.1

Browser

No response

Masonite Version

4.17.4

Anything else ?

Yes, the above issue persists in all data types. I mean if there's an array of dictionaries and has only one item in it then: it will return one dictionary only instead of an array of a dictionary.

Request example:

{
    "name": "Family",
    "contacts": [{
        "name": "User One"
    }]
}

Current Response

{
    "name": "Family",
    "contacts": {
        "name": "User One"
    }
}

Expected Response

{
    "name": "Family",
    "contacts": [
        {
            "name": "User One"
        }
    ]
}