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

[BUG] Handlebars template engine doesn't generate SupportingFiles properly #5320

Closed
5 tasks done
lindgrenfredrik opened this issue Feb 14, 2020 · 7 comments · Fixed by #5364
Closed
5 tasks done

[BUG] Handlebars template engine doesn't generate SupportingFiles properly #5320

lindgrenfredrik opened this issue Feb 14, 2020 · 7 comments · Fixed by #5364

Comments

@lindgrenfredrik
Copy link

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
    Any spec, any language using defaultGenerator that has supporting Files
  • Have you validated the input using an OpenAPI validator (example)?
    Issue related to Template engine and not schema
  • What's the version of OpenAPI Generator used?
    945ea14 from GIT
  • Have you search for related issues/PRs?
    Searched for Handlebars, found Switching template system from Mustache to Handlebars (or something else) #510
    Not match for specific issue but understand that Handlebars suppport may not be fully in place
  • What's the actual output vs expected output?
    Actual: Supporting files don't get compiled with template variables.
    Expected: Supporting files get compiled with template
Description

Have existing Templates for Handlebars that are used together with Swagger codegen and looking to migrate to openapi codegen instead

In generateSupportingFiles in DefaultGenerator.java it looks if files end with correct template extension before compiling them as templates otherwise just writing them as files.
This works fine when mustache files registered for the different languages end with correct mustache but when switching engine it expects handlebars extension which isn't whats registered by "language" generator
The name conversion is done later in current setup (that is why it works for i.e. model files), which never gets called for Supporting files.

Any ideas on how to solve this properly?

  • Allow mustache extension for any template engine?
  • Convert names earlier?
    • Register template files in some other manner to append correct ending?
  • Run compile for all supporting files regardless of extension?
openapi-generator version

945ea14

OpenAPI declaration file content or url
Command line used for generation

generate -g aspnetcore -o tmp -i pet2.json -e handlebars -t aspnetcore3/2.1/

pet2.json is petstore swagger definition update to 2.0 format found here https://petstore.swagger.io/v2/swagger.json

templates for handlebars is existing mustache ones found in openapi repo but update for handlebars style (also available in my fork atm master...lindgrenfredrik:aspnetcore_handlebarsdiff-bd1652e990ffe227072a5c8908fd3054)

Steps to reproduce

run generate command above and check output in supporting files

Related issues/PRs

Non found

Suggest a fix

My temporary workaround is to allow mustache named targets to pass into compile section
master...lindgrenfredrik:aspnetcore_handlebarsdiff-bd1652e990ffe227072a5c8908fd3054

Don't feel that this is the correct solution to the problem

So any ideas on how to solve this properly?

  • Allow mustache extension for any template engine?
  • Convert names earlier?
    • Register template files in some other manner to append correct ending?
  • Run compile for all supporting files regardless of extension?
@auto-labeler
Copy link

auto-labeler bot commented Feb 14, 2020

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

@jimschubert
Copy link
Member

Thanks for reporting this. I know some people are using handlebars, but I guess only for model/api generation and not supporting files.

I've opened #5364 to resolve this. I've tested it against the templates in your fork and everything but build.bat work, and build.bat seems to only be an escaping issue.

@lindgrenfredrik
Copy link
Author

Thanks

Had a quick review of code in PR and it looks good.
Currently on a business trip so will see if I find time during this week to test it.

Will look into details of the build.bat issue you saw

@lindgrenfredrik
Copy link
Author

Had the chance to run the new code in PR and it works fine for generating code for supporting files.

I had no issue with the build.bat file it self, though found 2 issues where changing from -last in mustache template to @last to handlebars template doesn't produce the same behavior which is causing build fails with template from my repo. (Authentication/ApiAuthentication.handlebars & Startup.handlebars)

Need to look into why the last item statement doesn't work the same way and how restructuring of those template parts should be done in order to make it work

@jimschubert
Copy link
Member

@lindgrenfredrik looks like you have incorrect syntax for @last. See jknack/handlebars.java tests

Try changing from:

{{#@last}}
            }
{{/@last}}

To:

{{#if @last}}
            }
{{/if}}

And change things like this:

{{^@last}}
{{/@last}}

to:

{{#not @last}}
{{/not}}

I tested this locally and seems to work. I'm not familiar with the shorter syntaxes you have or where they come from. You can see the helpers available in jknack handlebars.java here: https://github.com/jknack/handlebars.java/blob/master/handlebars/src/main/java/com/github/jknack/handlebars/helper/ConditionalHelpers.java

@lindgrenfredrik
Copy link
Author

I've been mainly looking at info on swagger-codegen page for which statements that need updating between mustache and handebars.
https://github.com/swagger-api/swagger-codegen/wiki/Swagger-Codegen-migration-from-Mustache-and-Handlebars-templates.

This since both seem to use jknack for handlebars and didn't see any bad effects when using those with swagger codegen
But could be that those templates also fail but not in same obvious manner meaning code actually compiles.

Will update templates I have with above changes to make them work same way as current mustache templates

@lindgrenfredrik
Copy link
Author

Tried to change template to use the ones mentioned above related to last statements
I didn't get those to work with the input file of https://petstore.swagger.io/v2/swagger.json

@jimschubert did you try with other input file when you got it to work ?

For some reason I now get a issue with \ characters in build.bat file that I for some reason didn't get when I tested it last week
Though that seems to be related to bug in handlebars library so I opened a ticket for that there.
jknack/handlebars.java#730
Though think workaround for that can be to change from \ to / since windows seems to accept those in urls as well

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

Successfully merging a pull request may close this issue.

2 participants