Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
PeteFromGlasgow opened this issue Feb 26, 2018 · 6 comments

Comments

@PeteFromGlasgow
Copy link

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)
@OzairP
Copy link

OzairP commented Jun 12, 2018

Having this issue as well :/.

Is there no way to force no parsing?

@LogansUA
Copy link

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

@PeteFromGlasgow
Copy link
Author

PeteFromGlasgow commented Jun 21, 2018

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

@LogansUA
Copy link

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

@LogansUA
Copy link

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

@yesworld
Copy link

I faced this problem (

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants