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

Cannot have body parameter in controller defined in separate file

Open ZPiDER666 opened this issue 2 years ago • 2 comments

i am trying to define a controller in its own file with a body param like this:

import { http, HttpBody, JSONResponse } from "@deepkit/http"
export class TestAPI2 {
	@http.PUT('/testParams')
	async testParams(body: HttpBody<any>) {
		const r = new JSONResponse({ body })
		r.header('content-type', 'application/json')
		return r
	}
}

but i get a runtime error when calling this endpoint.

looking into the reasons, i found this running compiled code:

if (!parameterResolverFound_6) parameters.body = _injector.get(_context.classType_6);

this line runs into the error, because _context == {} it looks like the controllers originating in a separate file are initialized with an empty context. unfortunately i cant really figure out where this code originates, as my prime suspect router.ts does not seem to contain any code ending up in this line.

note that when i define this class in the same file as my App.ts i do not run into this issue (but of course for my complex controllers this is not an option). what is even more confusing is that this has been working previously and i suspect a version problem.

my dependencies are:

"@deepkit/framework": "^1.0.1-alpha.85",
"@deepkit/http": "^1.0.1-alpha.85",
"@deepkit/type": "^1.0.1-alpha.85",
"@deepkit/type-compiler": "^1.0.1-alpha.83",

my @deepkit/http is at 1.0.1-alpha.85 (i tried newer versions with the similar results).

ZPiDER666 avatar Jun 24 '23 21:06 ZPiDER666

I added a test covering your code to make sure this (HttpBody<any>) actually works. I recommend upgrading to the newest version and see if it goes away since it works fine in master. See https://github.com/deepkit/deepkit-framework/commit/24d7c9c52c8e27047f38efc51f62ac6d2cd6d194

marcj avatar Oct 04 '23 12:10 marcj

Note that your title

Cannot have body parameter in controller defined in separate file

does not match your posted reproduction code. To make sure this is addressed, please provide failing code

marcj avatar Oct 04 '23 12:10 marcj