PerlDancer / PerlDancer/Dancer2

Some JSON requests are modified by Hash::MultiValue

Open
#1,240 3 comments 0 reactions 1 assignee View on GitHub

@SysPete is already working on this.

Since Sep 2, 2016.

Bug
Dominant language
Perl
Stars
604
Forks
288
Avg merge
1d 5h
Merged PRs (30d)
5

Description

I noticed that for some JSON body parameter schemas, arrays are sometimes simply removed when deserialized and stored in request->body_request()
For example, this JSON body parameter:

{
  "foo": [ { "bar": 1 } ],
  "baz": { "foobar": [ { "blah": 2 } ] }
}

will be parsed into this in a route:

$VAR1 = {
          'foo' => {
                     'bar' => 1
                   }
          'baz' => {
                     'foobar' => [
                                   {
                                     'blah' => 2
                                   }
                                 ]
                   },
        };

Notice that the "foo" field is no longer an array after the deserialization, but the "foobar" field is as expected. I can reproduce this in both 0.202000 and 0.203000.

I tracked it down to the use of Hash::MultiValue->from_mixed(), and with this naive patch it works as expected for me:

--- Dancer2/Core/Request.pm.orig    2016-09-02 11:05:27.042773871 +0200
+++ Dancer2/Core/Request.pm 2016-09-02 11:05:35.714634529 +0200
@@ -213,8 +213,8 @@

     # Set body parameters (HMV)
     # Not happy with fiddling with Plack::Request internals -- veryrusty Aug 2016.
     $self->env->{'plack.request.body'} =
-        Hash::MultiValue->from_mixed( ref $data eq 'HASH' ? %$data : () );
+        Hash::MultiValue->new( ref $data eq 'HASH' ? %$data : () );

     return $data;
 }

I expect my "solution" to break more than it fixes, but I hope that you experts can come up with a better solution.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.