oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Add template overrides from files in new config

Open jacklee1792 opened this issue 3 years ago • 8 comments

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

jacklee1792 avatar Jul 01 '22 22:07 jacklee1792

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 avatar Aug 01 '22 09:08 Celant

@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.

jacklee1792 avatar Aug 01 '22 16:08 jacklee1792

@Celant hey mate, can you make a release for your branch, please? I heavily relied on template overriding in my project. Thanks!

w32blaster avatar Aug 05 '22 16:08 w32blaster

any plans on moving this PR forward?

jeroendk avatar Oct 27 '22 09:10 jeroendk

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?

stevenh avatar Nov 12 '22 00:11 stevenh

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

jacklee1792 avatar Nov 24 '22 01:11 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.

jacklee1792 avatar Dec 05 '22 15:12 jacklee1792

This PR is incredibly useful. Thanks @jacklee1792

debuggerpk avatar Dec 05 '22 16:12 debuggerpk

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.

kNoAPP avatar Mar 30 '23 01:03 kNoAPP

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:

jamietanna avatar Sep 02 '23 15:09 jamietanna

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!

jacklee1792 avatar Sep 07 '23 16:09 jacklee1792

Interesting, thanks for letting me know! Appreciate the work (and apologies for the delay) with this one.

jamietanna avatar Jan 25 '24 14:01 jamietanna