deepkit-framework icon indicating copy to clipboard operation
deepkit-framework copied to clipboard

Array does not work with HttpBody

Open colorcube opened this issue 2 years ago • 2 comments

@http.POST('')
async stuff(body: HttpBody<string[]>) {

Debugger shows: This route has no body defined.

Sending data results in a Validation error:

{
    "message": "Validation error:\n(type): Not an array",
    "errors": [
        {
            "path": "",
            "code": "type",
            "message": "Not an array"
        }
    ]
}

If it's not intended to use arrays with HttpBody a remark in the docs might be good.

colorcube avatar Feb 21 '23 08:02 colorcube

What's the request's body and header?

marcj avatar Feb 21 '23 10:02 marcj

I stumbled across this problem again and found my own bugreport :-)

@http.POST('/example')
async example(exampleDtoList: HttpBody<ExampleDto[]>) {

HttpBody validation fails:

{
    "message": "Validation error:\n(type): Not an array",
    "errors": [
        {
            "path": "",
            "code": "type",
            "message": "Not an array"
        }
    ]
}

I checked and it is clearly a json array transported with Content-Type: application/json

Workaround:

@http.POST('/example')
async example(request: HttpRequest) {
    const body = await request.readBodyText()
    const exampleDtoList = deserialize<ExampleDto[]>(JSON.parse(body));

version 1.0.1-alpha.97

colorcube avatar Jun 12 '23 17:06 colorcube