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

EntityFromParam decorator error when param value is uuid #22

Closed
epfremmer opened this issue Feb 15, 2018 · 3 comments
Closed

EntityFromParam decorator error when param value is uuid #22

epfremmer opened this issue Feb 15, 2018 · 3 comments
Labels
status: invalid Issues with no action to take. type: question Questions about the usage of the library.

Comments

@epfremmer
Copy link

Summary

This is a bit of a weird one. In short when using a uuid in my request param I am not able to use the EntityFromParam decorator in my controller action for fetching entities.

After digging into this a little bit it appears that this is a result of the implementation in typestack/routing-controllers here: https://github.com/typestack/routing-controllers/blob/master/src/ActionParameterHandler.ts#L136-L139

Since the the param type is an Entity constructor there is no way to prevent typestack/routing-controllers from passing the uuid to JSON.parse resulting in an error.

There was a related issue here: typestack/routing-controllers#51, but it was closed since they were not using a constructor as the param type.

Steps to Reproduce

  1. Create typeorm entity using @PrimaryGeneratedColumn("uuid") for id
  2. Create controller action using get(@EntityFromParam('id') entity: Entity) {}
  3. Attempt to fetch entity http://localhost:3000/entity/:id where :id is the uuid of the entity

Error Message

Error
    at ParameterParseJsonError.HttpError [as constructor] (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/http-error/HttpError.ts:19:22)
    at ParameterParseJsonError.BadRequestError [as constructor] (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/http-error/BadRequestError.ts:10:9)
    at new ParameterParseJsonError (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/error/ParameterParseJsonError.ts:10:9)
    at ActionParameterHandler.parseValue (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/ActionParameterHandler.ts:153:23)
    at ActionParameterHandler.normalizeParamValue (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/ActionParameterHandler.ts:137:34)
    at ActionParameterHandler.handle (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/ActionParameterHandler.ts:44:28)
    at /Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/RoutingControllers.ts:116:49
    at Array.map (<anonymous>)
    at RoutingControllers.executeAction (/Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/RoutingControllers.ts:116:14)
    at /Users/edwardpfremmer/Projects/pfrembot/react-relationships/src/RoutingControllers.ts:83:33

There are a couple of workarounds that I have tested, but they feel less elegant:

  • Wrap uuid in quotes in URL: Surprisingly enough this actually worked causing JSON.parse to parse it as a serialized string
  • Use regular @param instead: Get the route param and handle fetching the typeorm entity by hand
  • Implement a custom decorator: Since custom decorators get set as type "custom-converter" preventing parsing the uuid

Sample Decorator

import { createParamDecorator } from "routing-controllers";
import { Action } from "routing-controllers/Action";
import { entityTransform } from "typeorm-routing-controllers-extensions/util/Utils";

export function MyCustomDecorator(paramName: string, options?: { required?: boolean }) {
  return createParamDecorator({
    required: Boolean(options && options.required),

    // note: do not change to fat-arrow function (this bound as ParamMetadata)
    value: function (action: Action) {
      const value = action.request.params[paramName];

      return entityTransform(value, this.targetType, false);
    }
  });
}

I apologize that I am creating this bug on here instead of typestack/routing-controllers, but it felt like most of the context of the problem was centered around @EntityFromParam.

Also I absolutely love this project! Keep up the amazing work!!!

@LogansUA
Copy link

Hello. I have absolutely the same issue @pleerock @19majkel94 any thoughts?

@NoNameProvided
Copy link
Member

Wrong repo, @EntityFromParam is not part of this package.

@NoNameProvided NoNameProvided added the type: question Questions about the usage of the library. label Jan 15, 2021
@NoNameProvided NoNameProvided added the status: invalid Issues with no action to take. label Jan 15, 2021
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 15, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
status: invalid Issues with no action to take. type: question Questions about the usage of the library.
Development

No branches or pull requests

3 participants