typeorm-routing-controllers-extensions
typeorm-routing-controllers-extensions copied to clipboard
String parameters cause ParameterParseJsonError in routing-controllers when using EntityFromParam.
Using a parameter with a string as a value when using EntityFromParameter causes a ParameterParseJsonError to be thrown in routing-controllers.
I've dug down and found out this is because routing-controllers sees the parameters type as the type of entity you're trying to pull back and sets isTargetObject to true in ActionParameterHandler. Causing it to be parsed as a JSON object.
I've made a fix that resolves the issue by setting explicitType to "string" in the decorator, which forces the value to be parsed as a string. This seems to work for both string and number types as the parameter.
Error
at new HttpError (routing-controllers-id-bug/dist/routing-controllers/src/http-error/HttpError.js:16:22)
at new BadRequestError (routing-controllers-id-bug/dist/routing-controllers/src/http-error/BadRequestError.js:9:9)
at new ParameterParseJsonError (routing-controllers-id-bug/dist/routing-controllers/src/error/ParameterParseJsonError.js:9:9)
at ActionParameterHandler.parseValue (routing-controllers-id-bug/dist/routing-controllers/src/ActionParameterHandler.js:127:23)
at ActionParameterHandler.normalizeParamValue (routing-controllers-id-bug/dist/routing-controllers/src/ActionParameterHandler.js:111:34)
at ActionParameterHandler.handle (routing-controllers-id-bug/dist/routing-controllers/src/ActionParameterHandler.js:35:28)
at actionMetadata.params.sort.map.param (routing-controllers-id-bug/dist/routing-controllers/src/RoutingControllers.js:87:49)
at Array.map (<anonymous>)
at RoutingControllers.executeAction (routing-controllers-id-bug/dist/routing-controllers/src/RoutingControllers.js:87:14)
at driver.registerAction (routing-controllers-id-bug/dist/routing-controllers/src/RoutingControllers.js:59:33)
Having this issue as well :/.
Is there no way to force no parsing?
Well, the same issue is described here #22, would be great to fix it.
@LogansUA I have a branch at #12 that should fix this issue. Could you give it a try and see if it works for you?
@c0n5pir4cy Ok, I'll check it tomorrow, thank you
If anybody interesting I made up with this solution
import { createParamDecorator } from "routing-controllers";
import { Action } from "routing-controllers/Action";
import { EntityParamOptions } from "typeorm-routing-controllers-extensions";
import { entityTransform } from "typeorm-routing-controllers-extensions/util/Utils";
export function EntityFromParams(paramName: string = "id", options?: EntityParamOptions): (object: object, method: string, index: number) => void {
return createParamDecorator({
required: Boolean(options && options.required),
value(action: Action): Promise<any> {
const value = action.request.params[paramName];
return entityTransform(value, (this as any).targetType, false, options);
},
});
}
I faced this problem (