swagger-angular-generator icon indicating copy to clipboard operation
swagger-angular-generator copied to clipboard

Avoid sending null values in the query params

Open jcctudela opened this issue 7 years ago • 2 comments

Leaving query params empty it send "null" to the backend, I've seen that changing process-method.ts in line 133 fix the problem:

Original line: def += ' if (value !== undefined) {\n';

Proposed line: def += ' if (value !== undefined && value !== null) {\n';

jcctudela avatar Aug 03 '18 06:08 jcctudela

I am not sure we should generalize this behavior. What if somebody wanted to distinguish/accept nulls via query params? In other words, can we be sure it's not a legitimate use case?

On the other hand, I see HttpParams via encodeURIComponent produce null'null' which I agree is not very smart. So I would prefer a systematic solution to hand over nulls in query params. Before that, you can filter it out manually.

Any suggestions/ideas?

jnwltr avatar Aug 07 '18 14:08 jnwltr

Ok, I've checked that the behavior of encodeURIComponent is correct, it has to produce 'null'

Another option in that case is to create the FormControl with { value: undefined, disabled: false } instead of undefined

I mean, instead of generate that: search: new FormControl(undefined, []),

It could be great to generate this: search: new FormControl({ value: undefined, disabled: false }, []),

In the first case, the value of the control is set to null, therefore the sentence if (value !== undefined) has no effect.

En the second case, the value of the control takes value = undefined, so the if (value !== undefined) works properly

Doing that, if a developer do nothing in one control, the service doesn't send the parameter, and if a developer set the control to null or whatever, it is going to be send to the endpoint.

What do you think about that? could it be a good solution?

In that case, I think, the change is adding this sentence after line 133 in file generate-form-service.ts initializer = {value: ${initializer}, disabled: false}; (This editor is deleting the `` chars outside the brackets)

jcctudela avatar Aug 07 '18 22:08 jcctudela