typeorm-routing-controllers-extensions icon indicating copy to clipboard operation
typeorm-routing-controllers-extensions copied to clipboard

String parameters cause ParameterParseJsonError in routing-controllers when using EntityFromParam.

Open PeteFromGlasgow opened this issue 6 years ago • 6 comments

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)

PeteFromGlasgow avatar Feb 26 '18 16:02 PeteFromGlasgow

Having this issue as well :/.

Is there no way to force no parsing?

OzairP avatar Jun 12 '18 19:06 OzairP

Well, the same issue is described here #22, would be great to fix it.

LogansUA avatar Jun 21 '18 08:06 LogansUA

@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?

PeteFromGlasgow avatar Jun 21 '18 14:06 PeteFromGlasgow

@c0n5pir4cy Ok, I'll check it tomorrow, thank you

LogansUA avatar Jun 21 '18 14:06 LogansUA

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);
        },
    });
}

LogansUA avatar Aug 29 '18 10:08 LogansUA

I faced this problem (

yesworld avatar Mar 21 '19 14:03 yesworld