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

Generate code from custom templates

Open sagikazarmark opened this issue 1 year ago • 4 comments

I'd like to integrate models and types with a custom (in-house built) framework.

In order to do that, I'd like to generate custom marshaler/unmarshaler functions.

It would be nice if I could add arbitrary templates to the code generation process.

One thing I tried is overriding templates for a server that I don't use. To complicate things, we still use the Chi integration, so I actually need to support two servers at the same time...which is not allowed at the moment.

As a result, I'm now overriding Chi templates by copying the original ones and extending them with my custom code which is not ideal IMO.

sagikazarmark avatar Mar 28 '24 11:03 sagikazarmark

Hey, I'm just in the middle of a complete re-write of our docs to improve this - if you see https://github.com/deepmap/oapi-codegen?tab=readme-ov-file#making-changes-to-code-generation and scroll to the code at the bottom ,it should be possible to specify your own (local/remote) templates for generation - does that work for you or have I misunderstood?

jamietanna avatar Mar 29 '24 15:03 jamietanna

What I’d like to do is basically add custom templates that contain additional code (for example: mapping to internal types).

My understanding is that the current template config only allows me to override templates already defined. Is that correct?

sagikazarmark avatar Mar 30 '24 10:03 sagikazarmark

It will pull in any templates provided in the user-templates object. Unfortunately, I did not set it up to have a way to call those templates.

As such, you need to call those templates somewhere that already exists, which will require over-riding at-least one existing template.

deefdragon avatar Apr 11 '24 03:04 deefdragon

Made the PR ready for review. Could use a check on if the documentation I put in the readme makes sense @sagikazarmark

deefdragon avatar Apr 27 '24 20:04 deefdragon