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

Conditional Logic in Mustache Template #308

Open
ankitjain8959 opened this issue Sep 27, 2024 · 0 comments
Open

Conditional Logic in Mustache Template #308

ankitjain8959 opened this issue Sep 27, 2024 · 0 comments

Comments

@ankitjain8959
Copy link

ankitjain8959 commented Sep 27, 2024

I have defined custom Mustache templates to generate a Java application using a swagger file and by extending the AbstractJavaCodegen class in a custom Codegen generator.

I aim to generate a custom ServiceImpl.java file and include conditional logic within the method body based on the operationId.
The operationId could be following: "createOrder", "patchOrder", "deleteOrder", "retrieveOrder", or "listOrder" defined in a swagger specification file (all the above operationId's are present in the swagger json).

I would like the corresponding logic for each operation to be included, while skipping irrelevant logic for other operations.

For example, here is the structure of my template:

{{#operations}}
public class {{classname}}ServiceImpl implements {{classname}}Service {

{{#operation}}
@ Override
public ResponseEntity<{{>returnTypes}}> {{operationId}}({{#allParams}}{{>optionalDataType}} {{paramName}}{{^-last}}, {{/-last}}{{/allParams}}) {

 {{#create-operation}}
 // Logic for createOrder operation and skip other operation's logic
 {{/create-operation}}

 {{#patch-operation}}
 // Logic for patchOrder operation and skip other operation's logic
 {{/patch-operation}}

 {{#delete-operation}}
 // Logic for deleteOrder operation and skip other operation's logic
 {{/delete-operation}}

 {{#retrieve-operation}}
 // Logic for retrieveOrder operation and skip other operation's logic
 {{/retrieve-operation}}

 {{#list-operation}}
 // Logic for listOrder operation and skip other operation's logic
 {{/list-operation}}

return new ResponseEntity<>(HttpStatus.OK);
}
  
{{/operation}}

I have tried to preprocess the data in custom codegen before passing it to the Mustache template, but it did not work as per my above requirement.

I believe this is not achievable using conditional logic in the Mustache template, specifically to ensure that only the relevant operation logic is included for each method based on the operationId

It would be great, if there is a way to do it.

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