Skip to content

Commit

Permalink
Merge pull request #2457 from vibe-d/fix_web_struct_array
Browse files Browse the repository at this point in the history
Fix parsing of arrays of structs in the web interface generator.
  • Loading branch information
Leonid Kramer committed Jul 6, 2020
2 parents c5f6927 + 151bb85 commit 72f5453
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions web/vibe/web/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -725,10 +725,10 @@ package ParamResult readFormParamRec(T)(scope HTTPServerRequest req, ref T dst,
alias EL = typeof(T.init[0]);
enum isSimpleElement = !(isDynamicArray!EL && !isSomeString!(OriginalType!EL)) &&
!isStaticArray!EL &&
!(is(T == struct) &&
!is(typeof(T.fromString(string.init))) &&
!is(typeof(T.fromStringValidate(string.init, null))) &&
!is(typeof(T.fromISOExtString(string.init))));
!(is(EL == struct) &&
!is(typeof(EL.fromString(string.init))) &&
!is(typeof(EL.fromStringValidate(string.init, null))) &&
!is(typeof(EL.fromISOExtString(string.init))));

static if (isStaticArray!T)
{
Expand Down Expand Up @@ -958,6 +958,30 @@ unittest {
assert(arr.length == 0);
}

unittest { // complex array parameters
import vibe.http.server;
import vibe.inet.url;

static struct S {
int a, b;
}

S[] arr;
ParamError err;

// d style
auto req = createTestHTTPServerRequest(URL("http://localhost/route?arr[0].a=1&arr[0].b=2"));
auto result = req.readFormParamRec(arr, "arr", false, NestedNameStyle.d, err);
assert(result == ParamResult.ok);
assert(arr == [S(1, 2)]);

// underscore style
req = createTestHTTPServerRequest(URL("http://localhost/route?arr_0_a=1&arr_0_b=2"));
result = req.readFormParamRec(arr, "arr", false, NestedNameStyle.underscore, err);
assert(result == ParamResult.ok);
assert(arr == [S(1, 2)]);
}

package bool webConvTo(T)(string str, ref T dst, ref ParamError err)
nothrow {
import std.conv;
Expand Down

0 comments on commit 72f5453

Please sign in to comment.