Azure / autorest.typescript

Extension for AutoRest (https://github.com/Azure/autorest) that generates TypeScript code. The transpiled javascript code is isomorphic. It can be run in browser and in node.js environment.
MIT License
179 stars 75 forks source link

[Modular] Unnecessary undefined insertions for optional properties #1990

Open deyaaeldeen opened 1 year ago

deyaaeldeen commented 1 year ago

The deserialization logic generated for modular libraries inserts undefined when an optional property is absent. This doesn't seem necessary and can be confusing. See https://github.com/Azure/autorest.typescript/blob/d350fb2ecc83b6302edf9bb55c577809865bca51/packages/typespec-test/test/openai_modular/generated/typespec-ts/src/api/operations.ts#L133-L135

Here is an example of code that doesn't do this insertion:

return {
...,
... !p.content_filter_results ? {} : { contentFilterResults: ... }
}
qiaozha commented 1 year ago

For simple types like string/boolean/number, it seems we can't do this way ?

qiaozha commented 1 year ago

How about having a function to unset all those undefined value keys ?

deyaaeldeen commented 1 year ago

For simple types like string/boolean/number, it seems we can't do this way ?

This issue is about how to set properties inside the result object and nested objects. Are you asking what to do if the result is of a primitive type?