@@ -16,9 +16,11 @@ export class ModelsSerializer implements IModelsSerializer {
1616 protected propertiesMapper : IModelPropertiesMapper ;
1717 protected stuff : TJsonaModel | Array < TJsonaModel > ;
1818 protected includeNamesTree : TJsonaNormalizedIncludeNamesTree ;
19+ private buildIncludedIndex : number ;
1920
2021 constructor ( propertiesMapper ?: IModelPropertiesMapper ) {
2122 propertiesMapper && this . setPropertiesMapper ( propertiesMapper ) ;
23+ this . buildIncludedIndex = 0 ;
2224 }
2325
2426 setPropertiesMapper ( propertiesMapper : IModelPropertiesMapper ) {
@@ -84,21 +86,19 @@ export class ModelsSerializer implements IModelsSerializer {
8486 }
8587
8688 if ( Object . keys ( uniqueIncluded ) . length ) {
87- body [ 'included' ] = [ ] ;
88- const includeUniqueKeys = Object . keys ( uniqueIncluded ) ;
89- includeUniqueKeys . forEach ( ( k ) => {
90- body [ 'included' ] . push ( uniqueIncluded [ k ] ) ;
91- } ) ;
89+ body [ 'included' ] = Object . values ( uniqueIncluded ) ;
9290 }
9391
9492 return body ;
9593 }
9694
9795 buildDataByModel ( model : TJsonaModel | null ) : TJsonApiData {
98- const data = {
99- id : this . propertiesMapper . getId ( model ) ,
100- type : this . propertiesMapper . getType ( model ) ,
101- attributes : this . propertiesMapper . getAttributes ( model ) ,
96+ const id = this . propertiesMapper . getId ( model ) ;
97+ const type = this . propertiesMapper . getType ( model ) ;
98+ const attributes = this . propertiesMapper . getAttributes ( model ) ;
99+ const data = { type,
100+ ...( typeof id !== 'undefined' ? { id } : { } ) ,
101+ ...( typeof attributes !== 'undefined' ? { attributes } : { } ) ,
102102 } ;
103103
104104 if ( typeof data . type !== 'string' || ! data . type ) {
@@ -115,6 +115,16 @@ export class ModelsSerializer implements IModelsSerializer {
115115 return data ;
116116 }
117117
118+ buildResourceObjectPart ( relation : TJsonaModel ) {
119+ const id = this . propertiesMapper . getId ( relation ) ;
120+ const type = this . propertiesMapper . getType ( relation ) ;
121+
122+ return {
123+ type,
124+ ...( typeof id === 'undefined' ? { } : { id } ) ,
125+ } ;
126+ }
127+
118128 buildRelationshipsByModel ( model : TJsonaModel ) {
119129 const relations = this . propertiesMapper . getRelationships ( model ) ;
120130
@@ -129,21 +139,17 @@ export class ModelsSerializer implements IModelsSerializer {
129139
130140 if ( Array . isArray ( relation ) ) {
131141 const relationshipData = [ ] ;
132- const relationLength = relation . length ;
133142
134- for ( let i = 0 ; i < relationLength ; i ++ ) {
135- const item = {
136- id : this . propertiesMapper . getId ( relation [ i ] ) ,
137- type : this . propertiesMapper . getType ( relation [ i ] )
138- } ;
143+ for ( const relationItem of relation ) {
144+ const relationshipDataItem = this . buildResourceObjectPart ( relationItem ) ;
139145
140- if ( item . id && item . type ) {
141- relationshipData . push ( item ) ;
146+ if ( 'type' in relationshipDataItem ) {
147+ relationshipData . push ( relationshipDataItem ) ;
142148 } else {
143149 console . error (
144- `Can't create data item[ ${ i } ] for relationship ${ k } ,
150+ `Can't create data item for relationship ${ k } ,
145151 it doesn't have 'id' or 'type', it was skipped` ,
146- relation [ i ]
152+ relationItem
147153 ) ;
148154 }
149155 }
@@ -152,12 +158,9 @@ export class ModelsSerializer implements IModelsSerializer {
152158 data : relationshipData
153159 } ;
154160 } else if ( relation ) {
155- const item = {
156- id : this . propertiesMapper . getId ( relation ) ,
157- type : this . propertiesMapper . getType ( relation )
158- } ;
161+ const item = this . buildResourceObjectPart ( relation ) ;
159162
160- if ( item . type ) {
163+ if ( ' type' in item ) {
161164 relationships [ k ] = {
162165 data : item
163166 } ;
@@ -218,10 +221,16 @@ export class ModelsSerializer implements IModelsSerializer {
218221 subIncludeTree : TJsonaNormalizedIncludeNamesTree ,
219222 builtIncluded : TJsonaUniqueIncluded
220223 ) {
221- const includeKey = this . propertiesMapper . getType ( relationModel ) + this . propertiesMapper . getId ( relationModel ) ;
224+ const id = this . propertiesMapper . getId ( relationModel ) ;
225+ const type = this . propertiesMapper . getType ( relationModel ) ;
226+ let includeKey = type + id ;
222227
223- if ( ! builtIncluded [ includeKey ] ) {
228+ if ( ! id || ! builtIncluded [ includeKey ] ) {
224229 // create data by current entity if such included is not yet created
230+ if ( includeKey in builtIncluded ) {
231+ includeKey += this . buildIncludedIndex ;
232+ this . buildIncludedIndex += 1 ;
233+ }
225234 builtIncluded [ includeKey ] = this . buildDataByModel ( relationModel ) ;
226235
227236 if ( subIncludeTree ) {
0 commit comments