diff --git a/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts b/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts index eb896011..fb4f2f01 100644 --- a/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts +++ b/projects/angular2-jsonapi/src/services/json-api-datastore.service.spec.ts @@ -16,6 +16,7 @@ import { Thing } from '../../test/models/thing'; import { getSampleThing } from '../../test/fixtures/thing.fixture'; import { ModelConfig } from '../interfaces/model-config.interface'; import { JsonApiQueryData } from '../models/json-api-query-data'; +import { getSampleChapter } from 'projects/angular2-jsonapi/test/fixtures/chapter.fixture'; let datastore: Datastore; let datastoreWithConfig: DatastoreWithConfig; @@ -208,6 +209,29 @@ describe('JsonApiDatastore', () => { queryRequest.flush(getSampleThing()); }); + it('should handle related resource objects that are listed in primary data', () => { + const expectedUrl = encodeURI(`${BASE_URL}/${API_VERSION}/chapters`); + + datastore.findAll(Chapter).subscribe((document) => { + expect(document).toBeDefined(); + const chapters = document.getModels(); + expect(chapters[0].relatesTo).toBeDefined(); + expect(chapters[0].relatesTo.id).toBe('b'); + expect(chapters[1].related).toBeDefined(); + expect(chapters[1].related.length).toBe(2); + expect(chapters[0].relatesTo.related[0].id).toBe('a'); + }); + + const queryRequest = httpMock.expectOne(expectedUrl); + queryRequest.flush({ + data: [ + getSampleChapter(1, 'a', 'Chapter A', null, 'b'), + getSampleChapter(1, 'b', 'Chapter B', ['a', 'c'], null), + getSampleChapter(1, 'c', 'Chapter C', null, 'b'), + ], + }); + }); + it('should fire error', () => { const expectedUrl = `${BASE_URL}/${API_VERSION}/authors`; const dummyResponse = { diff --git a/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts b/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts index e059719b..cc396e70 100644 --- a/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts +++ b/projects/angular2-jsonapi/src/services/json-api-datastore.service.ts @@ -288,7 +288,7 @@ export class JsonApiDatastore { }; } } else if (data[key] === null) { - const entity = belongsToMetadata.find((entity: any) => entity.propertyName === key); + const entity = belongsToMetadata.find((anEntity: any) => anEntity.propertyName === key); if (entity) { relationships = relationships || {}; @@ -340,14 +340,14 @@ export class JsonApiDatastore { const body: any = response.body; const models: T[] = []; + const resourceObjects = [...body.data, ...(body.included || [])]; + body.data.forEach((data: any) => { const model: T = this.deserializeModel(modelType, data); this.addToStore(model); - if (body.included) { - model.syncRelationships(data, body.included.concat(data)); - this.addToStore(model); - } + model.syncRelationships(data, resourceObjects); + this.addToStore(model); models.push(model); }); diff --git a/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts b/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts index 27126ec1..90f432e9 100644 --- a/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts +++ b/projects/angular2-jsonapi/test/fixtures/chapter.fixture.ts @@ -1,5 +1,10 @@ -export function getSampleChapter(i: number, chapterId: string, chapterTitle: string = 'Dummy title') { - return { +export function getSampleChapter( + i: number, chapterId: string, + chapterTitle: string = 'Dummy title', + related?: string[], + relatesTo?: string +) { + const response: any = { id: chapterId, type: 'chapters', attributes: { @@ -30,4 +35,15 @@ export function getSampleChapter(i: number, chapterId: string, chapterTitle: str self: '/v1/authors/288' } }; + if (related) { + response.relationships.related = { + data: related.map(id => ({id, type: 'chapters'})), + }; + } + if (relatesTo) { + response.relationships.relatesTo = { + data: {id: relatesTo, type: 'chapters'} + }; + } + return response; } diff --git a/projects/angular2-jsonapi/test/models/chapter.model.ts b/projects/angular2-jsonapi/test/models/chapter.model.ts index 21e82e20..d7011ceb 100644 --- a/projects/angular2-jsonapi/test/models/chapter.model.ts +++ b/projects/angular2-jsonapi/test/models/chapter.model.ts @@ -4,6 +4,7 @@ import { JsonApiModelConfig } from '../../src/decorators/json-api-model-config.d import { JsonApiModel } from '../../src/models/json-api.model'; import { Attribute } from '../../src/decorators/attribute.decorator'; import { BelongsTo } from '../../src/decorators/belongs-to.decorator'; +import { HasMany } from '../../src/decorators/has-many.decorator'; import { Section } from './section.model'; @JsonApiModelConfig({ @@ -28,4 +29,10 @@ export class Chapter extends JsonApiModel { @BelongsTo() firstSection: Section; + + @HasMany() + related: Chapter[]; + + @BelongsTo() + relatesTo: Chapter; }