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

Fix empty array encoding crash #44

Merged
merged 3 commits into from
Jul 12, 2018

Conversation

FredericRuaudel
Copy link
Contributor

Hi!

I recently stubble upon a crash when creating a SQLite schema model with an Array type member like so:

struct MyModel: SQLiteModel {
    var id: Int?
    var values: [String]
}

if I submit a JSON with an empty array, I got a crash back here.
So I update my Fluent* libs to the last rc4 version and investigate for this bug.

The reason of the crash is that when the _UnkeyedEncodingContainer get an empty array, none of its methods get called and as a consequence, no data is returned to the encoder and it doesn't throw any _DoJSONError() either.

I'm not sure this is the right way to fix this, but after adding some test to check the encoding/decoding of an empty and non empty array, I've added a guard let statement on the encoder.data value and throw the _DoJSONError in case it's nil
But maybe it would be better to move the fix directly inside the _UnkeyedEncodingContainer to be sure we are dealing with an array but I didn't find a clean solution at this location.

To reproduce the bug from a real vapor project, run this sample project:

bugEmptyArray.zip

Then run this cUrl command:

curl -X "POST" "http://localhost:8080/todos" \
     -H 'Content-Type: application/json; charset=utf-8' \
     -d $'{
  "titles": []
}'

And you should got the crash with the last SQLite release.

Hope it will help!

Have a great day!

in case of an empty container, the encoder returns nil so we need to check
for that and throw a _DoJSONError to fall back to JSON encoding like with
non empty arrays
@tanner0101
Copy link
Member

Great, thanks!

@tanner0101 tanner0101 added the bug Something isn't working label Jul 12, 2018
@tanner0101 tanner0101 self-assigned this Jul 12, 2018
@tanner0101 tanner0101 merged commit 386a6ed into vapor:master Jul 12, 2018
@penny-coin
Copy link

Hey @FredericRuaudel, you just merged a pull request, have a coin!

You now have 1 coins.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants