ngx-jsonapi icon indicating copy to clipboard operation
ngx-jsonapi copied to clipboard

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

Open ivelichko opened this issue 5 years ago • 2 comments

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 avatar Jan 27 '20 15:01 ivelichko

any updates?

ivelichko avatar Mar 12 '21 11:03 ivelichko

I am hitting this too.

iruizr7 avatar Jan 22 '22 07:01 iruizr7