Add template overrides from files in new config
Closes #607.
This PR introduces template file overrides from filenames. Overrides listed in output-options.user-template-files will just override whatever is given by output-options.user-templates, so this doesn't break any existing configuration files.
Example usage:
package: mypackage
generate:
models: true
client: true
output:
test.gen.go
output-options:
user-template-files:
client.tmpl: ./templates/my-client.tmpl
client-with-responses.tmpl: ./templates/my-client-with-responses.tmpl
Replacing the templates in the startup seems to break the template functions for me. I've tested overriding the templates in pkg/codegen/codegen.go instead, and that seems to work properly. I've tested this in my fork, and it works correctly. I'd suggest updating your PR to move the config overrides into that file, or if you'd prefer, I can make a PR for my fork.
@Celant Interesting, do you know why moving the overrides to codegen.go changes the behaviour? As far as I can tell, if you have old-config-style set to false it should be the same.
In any case I think your solution is better, feel free to open a PR for your fork. I'll close mine once yours is up.
@Celant hey mate, can you make a release for your branch, please? I heavily relied on template overriding in my project. Thanks!
any plans on moving this PR forward?
Liking this functionality, allows for individual templates to be picked from different directories or file names.
That said its still not as easy to use as -templates <dir> on the command line, which I suspect is actually the most command and useful case.
I just submitted https://github.com/deepmap/oapi-codegen/pull/852 as having to have go .tmpl files in a .yaml file really isn't usable. It restores compatibility with the old command switches which still have their place.
With all this in mind would you consider adding a user-template-dir to this PR?
In the latest commits I've combined ideas from https://github.com/deepmap/oapi-codegen/compare/master...Celant:oapi-codegen:feat/user-template-files together with https://github.com/deepmap/oapi-codegen/pull/707.
Template overrides can now be specified by any of the following, in order of lowest to highest precedence:
-
user-templates-dir -
user-template-files -
user-templates
Hey @jamietanna @deepmap-marcinr, are there any plans to move forward with this PR or to provide custom template functionality some other way? As it is right now custom templates are basically unusable without either providing them inline in the config file or writing a custom wrapper around codegen.
This PR is incredibly useful. Thanks @jacklee1792
Hey @jamietanna @deepmap-marcinr, are there any plans to move forward with this PR or to provide custom template functionality some other way? As it is right now custom templates are basically unusable without either providing them inline in the config file or writing a custom wrapper around
codegen.
I would love to see some love or activity on this PR. It is a pretty useful feature.
Apologies on the delay with this one folks! Will try and get a look at it for the next release - would it be possible to get this branch up-to-date with the current state of the repo? :pray:
Apologies on the delay with this one folks! Will try and get a look at it for the next release - would it be possible to get this branch up-to-date with the current state of the repo? 🙏
@jamietanna In rebasing I found https://github.com/deepmap/oapi-codegen/pull/968 already allows loading from files, this PR doesn't do much more than adding another option to load from a directory instead of specifying the file mapping one-by-one. So feel free to close!
Interesting, thanks for letting me know! Appreciate the work (and apologies for the delay) with this one.