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

Include underscore not working #67

Closed
harlancleiton opened this issue Dec 27, 2019 · 3 comments
Closed

Include underscore not working #67

harlancleiton opened this issue Dec 27, 2019 · 3 comments

Comments

@harlancleiton
Copy link

First of all, i'm not a native speaker and i used google translator to help me write this issue.
By now congratulations on the package. It is very useful and practical

I am having difficulties adding an underscore property in the available include

On my Transformer:

static get availableInclude () {
     return ['amount_used', 'administrator']
   }

includeAmountUsed (coupon) {
     const {amount_used} = coupon.toJSON ()

     return amount_used
   }

In Controller:

async show ({...}) {
...
const transformed = await transform
         .include ('amount_used, administrator')
         .item (coupon, CouponTransformer)

       return response.json (transformed)
}

JSON response example:

{
  "administrator": {
    "id": 12,
    "first_name": "Kate",
    "last_name": "Julien",
    "email": "[email protected]",
    "cpf": "58278510784",
    "date_birth": "2102-05-05T03: 00: 00.000Z"
  },
  "id": 8,
  "code": "jTh) vM5!",
  "valid_from": null,
  "valid_until": null,
  "type": "currency",
  "quantity": 995,
  "discount": "78.53"
}

Already tried a debug or add a console log in includeAmountUsed, but the function is not called. I have also tried renaming the function to includeAmount_used, includeAmount_Used.
If rename the property to amountUsed works perfectly, but I wanted JSON to be amount_used, can you help me? Thanks

@rhwilr
Copy link
Owner

rhwilr commented Dec 27, 2019

Hi @harlancleiton

This is currently not possible, and I don't think we can easily support this without braking existing functionality.

It might be possible to implement this with a custom Serializer.
You should be able to write a serializer that transforms all properties to snake_case before returning the response.

However, I do think we should support includes with underscores. I might take a look at it. I'd also happily accept a PR.

@harlancleiton
Copy link
Author

@rhwilr

I understand, I thought this note at README described this functionality

Note: If you have hyphen or underscore separated properties, you would name the include function in camelCase. The conversion is done automatically.

@rhwilr
Copy link
Owner

rhwilr commented Dec 28, 2019

I published version 2.1.0 that now supports snake_case include properties.

It should now work as you'd expect and your example from the initial post should work:

static get availableInclude () {
    return ['amount_used', 'administrator']
}

includeAmountUsed (coupon) {
    const {amount_used} = coupon.toJSON ()

    return amount_used
}

@rhwilr rhwilr closed this as completed Dec 28, 2019
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

2 participants