Skip to content

Commit d111e3a

Browse files
aanno2aanno
authored andcommitted
[REQ][typescript-angular]: support for object as query parameters (#4407)
* #4407 squashed from original, and better author (I failed to refer the right PR the first time) * removed isArrayLike in favour of Array.isArray * support collectionFormat the old way * fixed type and rebuild samples * revert samples/client/petstore/typescript-angular-v8-provided-in-root/builds/with-npm/package.json * reverted samples/client/petstore/typescript-angular-v8-provided-in-root/builds/with-npm/tsconfig.json * rebase-to-master-fix: isDateTime stuff is now handled within this.addToHttpParams * run bin/typescript-angular-petstore-all.sh on the rebased PR * applying proposed fix of @macjohnny * run bin/typescript-angular-petstore-all.sh to regenerate samples Co-authored-by: aanno <[email protected]>
1 parent 19a5f12 commit d111e3a

File tree

49 files changed

+1702
-72
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1702
-72
lines changed

modules/openapi-generator/src/main/resources/typescript-angular/api.service.mustache

Lines changed: 38 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,38 @@ export class {{classname}} {
151151
{{/operation}}
152152
{{/useHttpClient}}
153153

154+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
155+
if (typeof value === "object") {
156+
{{#useHttpClient}}httpParams = {{/useHttpClient}}this.addToHttpParamsRecursive(httpParams, value);
157+
} else {
158+
{{#useHttpClient}}httpParams = {{/useHttpClient}}this.addToHttpParamsRecursive(httpParams, value, key);
159+
}
160+
return httpParams;
161+
}
162+
163+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
164+
if (typeof value === "object") {
165+
if (Array.isArray(value)) {
166+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
167+
} else if (value instanceof Date) {
168+
if (key != null) {
169+
{{#useHttpClient}}httpParams = {{/useHttpClient}}httpParams.append(key,
170+
(value as Date).toISOString(){{^isDateTime}}.substr(0, 10)){{/isDateTime}};
171+
} else {
172+
throw Error("key may not be null if value is Date");
173+
}
174+
} else {
175+
Object.keys(value).forEach( k => {{#useHttpClient}}httpParams = {{/useHttpClient}}this.addToHttpParamsRecursive(
176+
httpParams, value[k], key != null ? `${key}.${k}` : k));
177+
}
178+
} else if (key != null) {
179+
{{#useHttpClient}}httpParams = {{/useHttpClient}}httpParams.append(key, value);
180+
} else {
181+
throw Error("key may not be null if value is not object or array");
182+
}
183+
return httpParams;
184+
}
185+
154186
{{#operation}}
155187
/**
156188
{{#summary}}
@@ -206,22 +238,20 @@ export class {{classname}} {
206238
if ({{paramName}}) {
207239
{{#isCollectionFormatMulti}}
208240
{{paramName}}.forEach((element) => {
209-
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.append('{{baseName}}', <any>element);
241+
{{#useHttpClient}}queryParameters = {{/useHttpClient}}this.addToHttpParams(queryParameters,
242+
<any>element, '{{baseName}}');
210243
})
211244
{{/isCollectionFormatMulti}}
212245
{{^isCollectionFormatMulti}}
213-
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.set('{{baseName}}', {{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']));
246+
{{#useHttpClient}}queryParameters = {{/useHttpClient}}this.addToHttpParams(queryParameters,
247+
{{paramName}}.join(COLLECTION_FORMATS['{{collectionFormat}}']), '{{baseName}}');
214248
{{/isCollectionFormatMulti}}
215249
}
216250
{{/isListContainer}}
217251
{{^isListContainer}}
218252
if ({{paramName}} !== undefined && {{paramName}} !== null) {
219-
{{#isDateTime}}
220-
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.set('{{baseName}}', ({{paramName}} as any instanceof Date) ? ({{paramName}} as any).toISOString(): {{paramName}});
221-
{{/isDateTime}}
222-
{{^isDateTime}}
223-
{{#useHttpClient}}queryParameters = {{/useHttpClient}}queryParameters.set('{{baseName}}', <any>{{paramName}});
224-
{{/isDateTime}}
253+
{{#useHttpClient}}queryParameters = {{/useHttpClient}}this.addToHttpParams(queryParameters,
254+
<any>{{paramName}}, '{{baseName}}');
225255
}
226256
{{/isListContainer}}
227257
{{/queryParams}}

samples/client/petstore/typescript-angular-v2/default/api/pet.service.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,38 @@ export class PetService {
204204
}
205205

206206

207+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
208+
if (typeof value === "object") {
209+
this.addToHttpParamsRecursive(httpParams, value);
210+
} else {
211+
this.addToHttpParamsRecursive(httpParams, value, key);
212+
}
213+
return httpParams;
214+
}
215+
216+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
217+
if (typeof value === "object") {
218+
if (Array.isArray(value)) {
219+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
220+
} else if (value instanceof Date) {
221+
if (key != null) {
222+
httpParams.append(key,
223+
(value as Date).toISOString().substr(0, 10));
224+
} else {
225+
throw Error("key may not be null if value is Date");
226+
}
227+
} else {
228+
Object.keys(value).forEach( k => this.addToHttpParamsRecursive(
229+
httpParams, value[k], key != null ? `${key}.${k}` : k));
230+
}
231+
} else if (key != null) {
232+
httpParams.append(key, value);
233+
} else {
234+
throw Error("key may not be null if value is not object or array");
235+
}
236+
return httpParams;
237+
}
238+
207239
/**
208240
* Add a new pet to the store
209241
* @param body Pet object that needs to be added to the store
@@ -321,7 +353,8 @@ export class PetService {
321353

322354
let queryParameters = new URLSearchParams('', this.encoder);
323355
if (status) {
324-
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
356+
this.addToHttpParams(queryParameters,
357+
status.join(COLLECTION_FORMATS['csv']), 'status');
325358
}
326359

327360
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
@@ -375,7 +408,8 @@ export class PetService {
375408

376409
let queryParameters = new URLSearchParams('', this.encoder);
377410
if (tags) {
378-
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
411+
this.addToHttpParams(queryParameters,
412+
tags.join(COLLECTION_FORMATS['csv']), 'tags');
379413
}
380414

381415
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845

samples/client/petstore/typescript-angular-v2/default/api/store.service.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,38 @@ export class StoreService {
116116
}
117117

118118

119+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
120+
if (typeof value === "object") {
121+
this.addToHttpParamsRecursive(httpParams, value);
122+
} else {
123+
this.addToHttpParamsRecursive(httpParams, value, key);
124+
}
125+
return httpParams;
126+
}
127+
128+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
129+
if (typeof value === "object") {
130+
if (Array.isArray(value)) {
131+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
132+
} else if (value instanceof Date) {
133+
if (key != null) {
134+
httpParams.append(key,
135+
(value as Date).toISOString().substr(0, 10));
136+
} else {
137+
throw Error("key may not be null if value is Date");
138+
}
139+
} else {
140+
Object.keys(value).forEach( k => this.addToHttpParamsRecursive(
141+
httpParams, value[k], key != null ? `${key}.${k}` : k));
142+
}
143+
} else if (key != null) {
144+
httpParams.append(key, value);
145+
} else {
146+
throw Error("key may not be null if value is not object or array");
147+
}
148+
return httpParams;
149+
}
150+
119151
/**
120152
* Delete purchase order by ID
121153
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors

samples/client/petstore/typescript-angular-v2/default/api/user.service.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,38 @@ export class UserService {
186186
}
187187

188188

189+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
190+
if (typeof value === "object") {
191+
this.addToHttpParamsRecursive(httpParams, value);
192+
} else {
193+
this.addToHttpParamsRecursive(httpParams, value, key);
194+
}
195+
return httpParams;
196+
}
197+
198+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
199+
if (typeof value === "object") {
200+
if (Array.isArray(value)) {
201+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
202+
} else if (value instanceof Date) {
203+
if (key != null) {
204+
httpParams.append(key,
205+
(value as Date).toISOString().substr(0, 10));
206+
} else {
207+
throw Error("key may not be null if value is Date");
208+
}
209+
} else {
210+
Object.keys(value).forEach( k => this.addToHttpParamsRecursive(
211+
httpParams, value[k], key != null ? `${key}.${k}` : k));
212+
}
213+
} else if (key != null) {
214+
httpParams.append(key, value);
215+
} else {
216+
throw Error("key may not be null if value is not object or array");
217+
}
218+
return httpParams;
219+
}
220+
189221
/**
190222
* Create user
191223
* This can only be done by the logged in user.
@@ -417,10 +449,12 @@ export class UserService {
417449

418450
let queryParameters = new URLSearchParams('', this.encoder);
419451
if (username !== undefined && username !== null) {
420-
queryParameters.set('username', <any>username);
452+
this.addToHttpParams(queryParameters,
453+
<any>username, 'username');
421454
}
422455
if (password !== undefined && password !== null) {
423-
queryParameters.set('password', <any>password);
456+
this.addToHttpParams(queryParameters,
457+
<any>password, 'password');
424458
}
425459

426460
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845

samples/client/petstore/typescript-angular-v2/npm/api/pet.service.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,38 @@ export class PetService {
204204
}
205205

206206

207+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
208+
if (typeof value === "object") {
209+
this.addToHttpParamsRecursive(httpParams, value);
210+
} else {
211+
this.addToHttpParamsRecursive(httpParams, value, key);
212+
}
213+
return httpParams;
214+
}
215+
216+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
217+
if (typeof value === "object") {
218+
if (Array.isArray(value)) {
219+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
220+
} else if (value instanceof Date) {
221+
if (key != null) {
222+
httpParams.append(key,
223+
(value as Date).toISOString().substr(0, 10));
224+
} else {
225+
throw Error("key may not be null if value is Date");
226+
}
227+
} else {
228+
Object.keys(value).forEach( k => this.addToHttpParamsRecursive(
229+
httpParams, value[k], key != null ? `${key}.${k}` : k));
230+
}
231+
} else if (key != null) {
232+
httpParams.append(key, value);
233+
} else {
234+
throw Error("key may not be null if value is not object or array");
235+
}
236+
return httpParams;
237+
}
238+
207239
/**
208240
* Add a new pet to the store
209241
* @param body Pet object that needs to be added to the store
@@ -321,7 +353,8 @@ export class PetService {
321353

322354
let queryParameters = new URLSearchParams('', this.encoder);
323355
if (status) {
324-
queryParameters.set('status', status.join(COLLECTION_FORMATS['csv']));
356+
this.addToHttpParams(queryParameters,
357+
status.join(COLLECTION_FORMATS['csv']), 'status');
325358
}
326359

327360
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845
@@ -375,7 +408,8 @@ export class PetService {
375408

376409
let queryParameters = new URLSearchParams('', this.encoder);
377410
if (tags) {
378-
queryParameters.set('tags', tags.join(COLLECTION_FORMATS['csv']));
411+
this.addToHttpParams(queryParameters,
412+
tags.join(COLLECTION_FORMATS['csv']), 'tags');
379413
}
380414

381415
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845

samples/client/petstore/typescript-angular-v2/npm/api/store.service.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,38 @@ export class StoreService {
116116
}
117117

118118

119+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
120+
if (typeof value === "object") {
121+
this.addToHttpParamsRecursive(httpParams, value);
122+
} else {
123+
this.addToHttpParamsRecursive(httpParams, value, key);
124+
}
125+
return httpParams;
126+
}
127+
128+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
129+
if (typeof value === "object") {
130+
if (Array.isArray(value)) {
131+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
132+
} else if (value instanceof Date) {
133+
if (key != null) {
134+
httpParams.append(key,
135+
(value as Date).toISOString().substr(0, 10));
136+
} else {
137+
throw Error("key may not be null if value is Date");
138+
}
139+
} else {
140+
Object.keys(value).forEach( k => this.addToHttpParamsRecursive(
141+
httpParams, value[k], key != null ? `${key}.${k}` : k));
142+
}
143+
} else if (key != null) {
144+
httpParams.append(key, value);
145+
} else {
146+
throw Error("key may not be null if value is not object or array");
147+
}
148+
return httpParams;
149+
}
150+
119151
/**
120152
* Delete purchase order by ID
121153
* For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors

samples/client/petstore/typescript-angular-v2/npm/api/user.service.ts

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,38 @@ export class UserService {
186186
}
187187

188188

189+
private addToHttpParams(httpParams: HttpParams, value: any, key?: string): HttpParams {
190+
if (typeof value === "object") {
191+
this.addToHttpParamsRecursive(httpParams, value);
192+
} else {
193+
this.addToHttpParamsRecursive(httpParams, value, key);
194+
}
195+
return httpParams;
196+
}
197+
198+
private addToHttpParamsRecursive(httpParams: HttpParams, value: any, key?: string): HttpParams {
199+
if (typeof value === "object") {
200+
if (Array.isArray(value)) {
201+
(value as any[]).forEach( elem => httpParams = this.addToHttpParamsRecursive(httpParams, elem, key));
202+
} else if (value instanceof Date) {
203+
if (key != null) {
204+
httpParams.append(key,
205+
(value as Date).toISOString().substr(0, 10));
206+
} else {
207+
throw Error("key may not be null if value is Date");
208+
}
209+
} else {
210+
Object.keys(value).forEach( k => this.addToHttpParamsRecursive(
211+
httpParams, value[k], key != null ? `${key}.${k}` : k));
212+
}
213+
} else if (key != null) {
214+
httpParams.append(key, value);
215+
} else {
216+
throw Error("key may not be null if value is not object or array");
217+
}
218+
return httpParams;
219+
}
220+
189221
/**
190222
* Create user
191223
* This can only be done by the logged in user.
@@ -417,10 +449,12 @@ export class UserService {
417449

418450
let queryParameters = new URLSearchParams('', this.encoder);
419451
if (username !== undefined && username !== null) {
420-
queryParameters.set('username', <any>username);
452+
this.addToHttpParams(queryParameters,
453+
<any>username, 'username');
421454
}
422455
if (password !== undefined && password !== null) {
423-
queryParameters.set('password', <any>password);
456+
this.addToHttpParams(queryParameters,
457+
<any>password, 'password');
424458
}
425459

426460
let headers = new Headers(this.defaultHeaders.toJSON()); // https://github.com/angular/angular/issues/6845

0 commit comments

Comments
 (0)