acacode / acacode/swagger-typescript-api

Null values in object being converted to form send "null" to server

Ouverte
#485 1 commentaire 1 réaction 0 personnes assignées Voir sur GitHub
Langage dominant
TypeScript
Étoiles
4.1k
Forks
436
Métriques de merge des PR
Aucune PR mergée en 30 j

Description

When a request that uses the content type of `ContentType.FormData` is sent with a body, any null or undefined values in that object end up being sent to the server as `'null'` or `'undefined'` strings. This seems very wrong to make the server add special handling for strings containing the words null or undefined.

The code in question is this:
```ts
[ContentType.FormData]: (input: any) =>
Object.keys(input || {}).reduce((formData, key) => {
const property = input[key];
formData.append(
key,
property instanceof Blob
? property
: typeof property === 'object' && property !== null
? JSON.stringify(property)
: `${property}` // TODO: If property is null or undefined, this will result in 'null' or 'undefined'
);
return formData;
}, new FormData()),
```

The line with the TODO shows the cause of the problem.
I see two ways of fixing this:
1. Wrap the `formData.append` call in a conditional to only add it to the FormData object if the value is not null or undefined.
2. Modify the line with the TODO so that it returns the empty string if `property` is null or undefined instead of blindly converting the value to a string.

To me, option 2 seems more correct since it's likely that purposefully setting a value to null or undefined may be a way of clearing a piece of data in the server; option 1 would not add the data to the form and thus wouldn't send the value to the server.

Guide de contribution

Aucun guide de contribution indexé pour ce dépôt

Évaluation

Cette issue n'a pas encore été évaluée.

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.