Skip to content

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

Open
@jlefebvre1997

Description

@jlefebvre1997

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 

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: fixIssues describing a broken feature.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions