Open DDtMM opened 4 years ago
I've been working around this issue by manually overriting in my main file. But this ain't pretty and other devs not aware of the issue get bitten when a new array is added:
import { Seeds, Species } from '@services/api.ts' // api.ts is generated by NSwag
// Monkeypatching to work around multipart/form-data issue with NSwag
Seeds.prototype.toString = function() {
return JSON.stringify(this.toJSON())
}
Species.prototype.toString = function() {
return JSON.stringify(this.toJSON())
}
According to swagger.io:
Arrays
Arrays only encode properly for Files. There is a specific branch of logic if file is detected and each item is appended one by one. This behavior should be the same for all array fields.
Objects
Because of the use of
.toString()
, all child objects are encoded as [object Object] when sent as form data.Complications
This behavior for arrays and objects can be overriden by setting explode to false in the spec.
Suggestion
toString()
).I'm not too familiar with LiquidTemplates, but in the following I normalize the field value as an array, and then call the encoding logic inside the
forEach()
. It seems like efficient output is preferred over a simpler template, but the normalizing as an array was done as an attempt to avoid repeating the encoding logic.