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

Bug: service.delete() for relationships removes unrelated entities from the result of parent's service.all() #262

Open
ivelichko opened this issue Jan 27, 2020 · 2 comments

Comments

@ivelichko
Copy link

ivelichko commented Jan 27, 2020

Let's have an Author entity which has one-to-many relationship with Book entity. Let's have Author1 with Book1 and Book2, and Author2.

let authors = await authorsService.all({ include: ['books'] }).toPromise(); // will return Author1 with Book1 and Author2
console.log(authors.data); // will show both Author1 and Author2
await booksService.delete('Book1').toPromise(); // will delete Book1
console.log(authors.data); // will show only Author1, Author2 is missing!!!

My guess is that something is wrong with CacheMemory.
The bug is reproducible at least on 2.1.12 and later (I haven't tried earlier versions).


I created a failing test case for service.spec.ts:


describe('service.delete()', () => {
    let core: Core;
    let authorsService: AuthorsService;
    let booksService: BooksService;
    beforeEach(async () => {
        core = new Core(
            new JsonapiConfig(),
            new JsonapiStore(),
            new JsonapiHttpImported(new HttpClient(new HttpHandlerMock()), new JsonapiConfig())
        );
        authorsService = new AuthorsService();
        authorsService.register();
        booksService = new BooksService();
        booksService.register();
        await authorsService.clearCache();
        await booksService.clearCache();
        test_response_subject.complete();
        test_response_subject = new BehaviorSubject(new HttpResponse());
    });

    it(`.delete() for relationship does not remove entities from parent's .all()`, async () => {
        // given
        test_response_subject.next(new HttpResponse({ body: TestFactory.getCollectionDocumentData(Author) }));
        let authors = await authorsService.all({ include: ['books'] }).toPromise();
        test_response_subject.complete();

        let author1 = authors.data[0];
        let book = author1.relationships.books.data[0];
        let author2 = authors.data[1];

        expect(author2).toBeTruthy();
        expect(authors.data).toEqual(arrayContaining([author1, author2]));

        // when
        test_response_subject = new BehaviorSubject(new HttpResponse());
        test_response_subject.next(new HttpResponse());
        await booksService.delete(book.id).toPromise();
        test_response_subject.complete();

        // then
        expect(authors.data).toEqual(arrayContaining([author1, author2]));
    });
});

@ivelichko
Copy link
Author

any updates?

@iruizr7
Copy link

iruizr7 commented Jan 22, 2022

I am hitting this too.

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