From 428e0dbb7abd06078ee2937bf2895f8cdbd7e98d Mon Sep 17 00:00:00 2001 From: nselof Date: Mon, 30 Sep 2019 14:45:22 -0500 Subject: [PATCH] Fixes for Fields in get_params - correctly handle multiple Fields collections - only add Fields to get_params once --- src/services/path-builder.spec.ts | 3 ++- src/services/path-builder.ts | 5 ++--- src/services/path-collection-builder.spec.ts | 5 ++++- src/services/path-collection-builder.ts | 7 ------- 4 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/services/path-builder.spec.ts b/src/services/path-builder.spec.ts index 45b192c0..f155a8c1 100644 --- a/src/services/path-builder.spec.ts +++ b/src/services/path-builder.spec.ts @@ -54,8 +54,9 @@ describe('Path Builder', () => { expect(path_builder.includes).toEqual(['include']); }); it('applyParams method should add fields to get_params if they are included in the request', () => { - path_builder.applyParams(testService, { fields: { test: ['test_attribute'] } }); + path_builder.applyParams(testService, { fields: { test: ['test_attribute'], test2: ['test2_attribute'] } }); expect((path_builder as any).get_params.indexOf('fields[test]=test_attribute')).toBeGreaterThan(-1); + expect((path_builder as any).get_params.indexOf('fields[test2]=test2_attribute')).toBeGreaterThan(-1); }); it('appendPath method should add passed value to paths array (only if value is not an empty string)', () => { path_builder.paths = []; diff --git a/src/services/path-builder.ts b/src/services/path-builder.ts index 887b5e8a..efd5ac99 100644 --- a/src/services/path-builder.ts +++ b/src/services/path-builder.ts @@ -17,11 +17,10 @@ export class PathBuilder { this.setInclude(params.include); } if (params.fields && Object.keys(params.fields).length > 0) { - let fields_param: string = ''; for (let resource_type in params.fields) { - fields_param += `fields[${resource_type}]=${params.fields[resource_type].join(',')}`; + let fields_param = `fields[${resource_type}]=${params.fields[resource_type].join(',')}`; + this.get_params.push(fields_param); } - this.get_params.push(fields_param); } } diff --git a/src/services/path-collection-builder.spec.ts b/src/services/path-collection-builder.spec.ts index 76a1815a..17b2eb5c 100644 --- a/src/services/path-collection-builder.spec.ts +++ b/src/services/path-collection-builder.spec.ts @@ -64,8 +64,11 @@ describe('Path Builder', () => { it('if fields are provided, they should be formatted and included in get_params', () => { let addParam_parent_spy = spyOn(path_collection_builder, 'addParam').and.callThrough(); - path_collection_builder.applyParams(testService, { fields: { test: ['test_attribute', 'other_test_attribute'] } }); + path_collection_builder.applyParams(testService, { fields: + { test: ['test_attribute', 'other_test_attribute'], test2: ['test2_attribute'] } + }); expect((path_collection_builder as any).get_params.indexOf('fields[test]=test_attribute,other_test_attribute')).toBeGreaterThan(-1); + expect((path_collection_builder as any).get_params.indexOf('fields[test2]=test2_attribute')).toBeGreaterThan(-1); }); it('if page params are provided, applyParams should call addParam one or two times with the page number and size', () => { diff --git a/src/services/path-collection-builder.ts b/src/services/path-collection-builder.ts index 8db95b7d..ae9a48ab 100644 --- a/src/services/path-collection-builder.ts +++ b/src/services/path-collection-builder.ts @@ -26,13 +26,6 @@ export class PathCollectionBuilder extends PathBuilder { if (params.sort && params.sort.length) { this.addParam('sort=' + params.sort.join(',')); } - if (params.fields && Object.keys(params.fields).length > 0) { - let fields_param: string = ''; - for (let resource_type in params.fields) { - fields_param += `fields[${resource_type}]=${params.fields[resource_type].join(',')}`; - } - this.addParam(fields_param); - } } protected addParam(param: string): void {