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

Entity relations are not serialized properly when calling toJSON on flushed entities after being created with EntityRepository.create #784

Closed
charmpitz opened this issue Aug 26, 2020 · 6 comments
Assignees
Labels
bug Something isn't working

Comments

@charmpitz
Copy link
Contributor

charmpitz commented Aug 26, 2020

Describe the bug
When using toJSON on an Entity after it is being created, persisted and flushed via the create and persistAndFlush methods on the specific repository, its related entities are not being serialized properly. Instead of getting the result of toJSON being called on them we get a number representing their id.

Stack trace

function create(input) {
    ...

    let articleEntity: ArticleEntity = this.articleRepository.create({
        ...input,
        user: userEntity,
        tags,
    });

    await this.articleRepository.persistAndFlush(articleEntity);
    console.log(articleEntity.toJSON())
    /* Wrong Output:
    {
        id: 9,
        title: 'Some Article',
        tags: [ 1 ],
        user: 1
    }
    */

    articleEntity = await this.articleRepository.findOne(articleEntity.id)
    console.log(articleEntity.toJSON())
    /* Right Output:
    {
        id: 9,
        title: 'Some Article',
        tags: [
            {
                id: 1,
                name: 'tag1',
                user: [Object],
                ...
            }
        ],
        user: {
            id: 1
            username: 'someusername'
            ...
        }
    }
    */

    return articleEntity.toJSON() as Article;
}

To Reproduce
See above.

Expected behavior
I'm expecting for the articleEntity.toJSON() to return

    {
        id: 9,
        title: 'Some Article',
        tags: [
            {
                id: 1,
                name: 'tag1',
                user: [Object],
                ...
            }
        ],
        user: {
            id: 1,
            username: 'someusername',
            ...
        }
    }

without the need of calling findOne after flushing.

Additional context

Versions

Dependency Version
node 14.7.0
@mikro-orm/cli ^4.0.0-rc.4
@mikro-orm/core ^4.0.0-rc.4
@mikro-orm/migrations ^4.0.0-rc.4
@mikro-orm/nestjs ^4.0.0-alpha.3
@mikro-orm/postgresql ^4.0.0-rc.4
@mikro-orm/reflection ^4.0.0-rc.4
typescript ^3.7.4
@charmpitz charmpitz added the bug Something isn't working label Aug 26, 2020
@B4nan B4nan closed this as completed in 5f7fb8f Sep 2, 2020
@B4nan
Copy link
Member

B4nan commented Sep 2, 2020

This is actually quite huge breaking change, so will make this hidden behind a config toggle, keeping the default behaviour as is.

B4nan added a commit that referenced this issue Sep 2, 2020
This reverts unwanted breaking change done via 5f7fb8f, where the serialized
API response from endpoints where we create new entities would return different
response.

This behaviour is now hidden behind the new `populateAfterFlush` flag, which
defaults to `false`.

Related: #784
@B4nan
Copy link
Member

B4nan commented Sep 2, 2020

In rc.6 you can set populateAfterFlush: true to enable this behaviour.

@B4nan B4nan mentioned this issue Sep 2, 2020
46 tasks
@charmpitz
Copy link
Contributor Author

Sounds good, thank you for implementing it! ✌️

@SpencerKaiser
Copy link
Contributor

@B4nan I just stumbled across this issue after commenting on #1861 (comment).

I have populateAfterFlush set to false (and it seems that's the default anyways) but my entity is still populating after flush... any idea what I'm doing wrong?

@B4nan
Copy link
Member

B4nan commented Oct 26, 2021

I have populateAfterFlush set to false (and it seems that's the default anyways) but my entity is still populating after flush... any idea what I'm doing wrong?

No idea, provide a reproduction if you need help, as you might be facing some other problem (and yes, populateAfterFlush defaults to false)

@B4nan
Copy link
Member

B4nan commented Dec 18, 2021

The default will change to true in v5

B4nan added a commit that referenced this issue Dec 18, 2021
Related: #784

BREAKING CHANGE:
After flushing a new entity, all relations are marked as populated,
just like if the entity was loaded from the db. This aligns the serialized
output of `e.toJSON()` of a loaded entity and just-inserted one.

In v4 this behaviour was disabled by default, so even after the new entity was
flushed, the serialized form contained only FKs for its relations. We can opt in
to this old behaviour via `populateAfterFlush: false`.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants