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

fix: Nested objects are empty in some cases when using serialization groups #1746

Open
jlefebvre1997 opened this issue Aug 20, 2024 · 7 comments
Labels
type: fix Issues describing a broken feature.

Comments

@jlefebvre1997
Copy link

Description

disclaimer : I'm using NestJS but I don't think the issue is related

When trying to serialize an object using instanceToPlain with serialization groups and a nested object, sometimes the nested object is empty

class User {
  @ManyToMany(() => Workspace, (workspace) => workspace.users)
  @Type(() => Workspace)
  @ApiProperty()
  @Expose({ groups: [GROUP_ME, GROUP_ORGANIZATION_USERS] })
  workspaces: Workspace[];
}

class Workspace {
  @PrimaryGeneratedColumn('uuid')
  @Expose({
    groups: [
      GROUP_WORKSPACE,
      GROUP_ORGANIZATION_WORKSPACES,
      GROUP_ORGANIZATION_USERS,
    ],
  })
  id: string;
}

and in the controller :

@UseInterceptors(ClassSerializerInterceptor)
@SerializeOptions({
  groups: [GROUP_ORGANIZATION_USERS],
})
async findUsers(...) {
 // fetch user object in db and return it, so it's an instance of User
}

I skipped a lot of code for the example

Expected behavior

I would expect my User object to be serialized like this :

{
    workspaces: [
        {
            id: '1'
        },
        {
            id: '2'
        }    
    ]
}

Actual behavior

The nested objects are empty :

{
    workspaces: [
        {},
        {}    
    ]
}
```ts

Note that it I don't specify any serialization groups (only using `@Expose()` on my nested object property (workspace.id for example) it works as expected... I'm out of options here, thanks in advance for your time and help 
@jlefebvre1997 jlefebvre1997 added status: needs triage Issues which needs to be reproduced to be verified report. type: fix Issues describing a broken feature. labels Aug 20, 2024
@diffy0712
Copy link

Hello,

It is hard to tell with all the nestJs decorator in place :)
I think you could check what is in your user object that you are transforming, validating that it indeed has the workspaces with correct ids.
After that I would remove the groups, to validate if there is anything different without groups.

If you could write these results I think we can figure out what might cause the problem :)

Do you know the exact options that the instanceToPlain will be called with?

@jlefebvre1997
Copy link
Author

Hello, and thanks for your answer ! I did those tests, here are the results :

I think you could check what is in your user object that you are transforming, validating that it indeed has the workspaces with correct ids.

I tried to display the user object before serializing it, and it indeed has the workspaces with the correct ids

After that I would remove the groups, to validate if there is anything different without groups.

I did, and if I remove all the groups from the Expose decorator, leaving just @Expose(), it works as expected and the workspaces are correctly serialized. I have another relationship similar to this one in my project, and I figured as long as I specify groups in the Expose decorator, this other relationship is also wrongly serialized. It's as if it didn't recognised the groups.

Do you know the exact options that the instanceToPlain will be called with?

Yes, it contains only the group(s) specified in the SerializeOptions decorator, for example here : { groups: [ 'group_organization_users' ] } (which is just the string value of my constant GROUP_ORGANIZATION_USERS)

@diffy0712
Copy link

Thank you for the checks.

Could you please try to use the same groups, but only use 'Expose()' with no groups on the Workspace.id. My guess is that the type does not forward the group correctly for nested objects.

@jlefebvre1997
Copy link
Author

I tried it just now, if I only use Expose() with no groups on the Workspace.id, the id is correctly serialized

@diffy0712
Copy link

Is it an acceptable solution for your use case?
I have to check the issues, docs and do some testing, but I think this is a bug in the group handling.

@jlefebvre1997
Copy link
Author

Hello again and sorry for answering this late 🙏

It will do for now but may I ask you to keep me informed if this bug is taken care of ? 😄

In any case, thank you for your help !

@jdflament
Copy link

I'm facing this issue too, thank you for reporting it 👍

@diffy0712 diffy0712 removed the status: needs triage Issues which needs to be reproduced to be verified report. label Sep 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: fix Issues describing a broken feature.
Development

No branches or pull requests

3 participants