strapi-to-typescript
strapi-to-typescript copied to clipboard
Array type relations and components are always generated as mandatory
Array type relations and components are always generated as mandatory, irrespective if they're marked as required or not.
have you tried the option -u collectionCanBeUndefined ?
Missed this configuration option. It's a pretty tricky thing, as by the way it's generated by default, optional/mandatory is following response structure (where id and array collections are always non-nullish), but when making a POST/PUT request, id has to not exist and array collections then need to follow required flag. So I think the current behavior is right and I gave a false alarm.
The collectionCanBeUndefined flag documentation seems to be wrong, it says about all collections, but I think it should say only about array collections, as now only array collections are generated as mandatory by default (which is correct behavior and should stay as is). And beyond that, I don't see uses of that flag, as if targeting for POST/PUT request, it would make array collections optional, but id would still stay mandatory.
just for information, the only use of this flag in the base code:
const required = !a.required && !(!this.config.collectionCanBeUndefined && (a.collection || a.repeatable)) && a.type !== 'dynamiczone' ? '?' : '';
a.collection || a.repeatable - this seems to be a double bug, because it looks like it tries to generate mandatory fields for all collections and repeatables (a bug in what it is trying to do), but it happens only for repeatables (a bug again, as what it tries does not happen), and double bug resolves to correct behavior. Mandatory should be generated only for repeatables, as only for repeatables (arrays) Strapi is automatically adding empty brackets in responses.
It's not clear to me but it doesn't really matter. I no longer actively use Strapi, so if you have a fix, which does not change the current behavior, maybe with new options, feel free to create a pull request.