Class-transformer: @Expose with Different property name Not working

Created on 13 Feb 2019  Â·  6Comments  Â·  Source: typestack/class-transformer

I have created a class with private properties which property names started with '_'.
Then expose variable name without _, using @Expose annotation. But it is not working as expected.

Similar to 'id' in below example.
https://github.com/typestack/class-transformer/blob/develop/README.md#exposing-properties-with-different-names

question

Most helpful comment

The documentation is little bit vague on this. The name you put in @Expose({ name: 'foo' }) is the input name not the output name.

All 6 comments

The documentation is little bit vague on this. The name you put in @Expose({ name: 'foo' }) is the input name not the output name.

Hey @walakulu, could you provide an example of the property you're trying to expose and what you expect it to look like?

Alternatively, if @vincic's answer helped you solve this would you mind closing this issue?

Hi Alex,

I thought @expose https://github.com/expose({ name: 'foo' }) , foo as the
output name. But according to vincic's answer, it is the input parameter
name. Could I know any machanisum of returning different parameter name
from input name. Because working with different languages (front end ,
backend) have different variable naming conventions.

Thank you,
Hasitha Maduranga

On Thu, 18 Apr 2019, 06:48 Alex Plescan, notifications@github.com wrote:

Hey @walakulu https://github.com/walakulu, could you provide an example
of the property you're trying to expose and what you expect it to look like?

Alternatively, if @vincic https://github.com/vincic's answer helped you
solve this would you mind closing this issue?

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/typestack/class-transformer/issues/237#issuecomment-484319013,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ADJZUDXRMCRZAQGOCPAG2CLPQ7D6TANCNFSM4GXIDA5Q
.

@walakulu, I agree that the documentation is confusing for the @Expose() method. It doesn't make it very clear that the name property is the input.

I would be happy to raise a PR to update documentation to make this clearer once this package is under active maintenance again.

To answer your question... you could disagree, but in my opinion it seems out of the scope of class-transformer to handle mapping property names (i.e. from internal backend names to public frontend names).

In our codebase, we use mapping functions to achieve this. An example is:

class User {
  _name: string
}

interface ReadUser {
  name: string
}

function mapToReadUser (user: User): ReadUser {
  return { name: user._name }
}

const json = { _name: 'Bob' }
const obj = mapToReadUser(plainToClass(User, json))
console.log(obj)
// => { name: 'Bob' }

Closing as answered.

If your issue persists please open a new issue with a minimal reproducible example. (Input values, class definition, expected output, received output.)

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.

Was this page helpful?
0 / 5 - 0 ratings