Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional nested fields of request body are converted to mandatory by the mustache template #9

Open
xadil opened this issue Jun 5, 2022 · 0 comments

Comments

@xadil
Copy link
Contributor

xadil commented Jun 5, 2022

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

{{name}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};

Suggested fix at above line:
{{name}}{{^required}}?{{/required}}: {{#isEnum}}{{{datatypeWithEnum}}}{{/isEnum}}{{^isEnum}}{{{datatype}}}{{/isEnum}};

I have raised a PR for this. Hopefully this helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant