Optional nested fields of request body are converted to mandatory by the mustache template
xadil opened this issue · comments
When there are optional fields in the post request body, the generated ts code converts these optional fields to mandatory.
e.g.
"requestBody": { "content": { "application/json": { "schema": { "type": "object", "properties": { "searchCriteria": { "$ref": "#/components/schemas/SearchLessonCriteria" } } } } } }
"SearchLessonCriteria": { "required": ["showTransient", "status"], "type": "object", "properties": { "text": { "type": "string" } } }
The generated ts code is
export interface SearchLessonCriteria { text: string }
Generating the cli code on my machine with swagger-code cli does keep it optional.
The reason seems to be missing {{^required}}?{{/required}}
after var name in file modelGeneric.mustache
Suggested fix at above line:
{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};
I have raised a PR for this. Hopefully this helps.