aj-foster / open-api-generator

Open API code generator for Elixir
MIT License
97 stars 13 forks source link

Add @callback definition on generated functions. #21

Open xadhoom opened 1 year ago

xadhoom commented 1 year ago

Right now applications that use a lib generated with oapi_generator has hard times in testing because there's coupling with the generated functions and client.

Obviously you can pass a custom client just for testing, but this means that the caller must "mock" the http calls, which is not correct because is an implementation detail of the generated library.

The approach is to use behaviours which allows to easily mock with tools like Mox, Hammox, etc so the user of the generated library can create a fake module implementing the very same api interface as the generated operations.

This PR just adds @callback along to docs and specs which allows to use the generated operation module also as a behaviour for testing.

(I've also experimented in generating a separated behaviour module and make the operation module an impl of it, which works ok but maybe is a bit too much and not really needed, after all this will be used only for testing)

aj-foster commented 10 months ago

Hi @xadhoom, with the 1.0.0 line of the code (release candidate available now), I took a different approach to enable things like this. Anyone can implement changes to the rendered code using the new plugin system.

With that said, I think adding @callback attributes might be popular enough to include as an option (default off) in the default implementation of the renderer. If you agree, we could do this in a separate branch (due to the large number of code changes).

xadhoom commented 10 months ago

Hi, thanks for the heads up. Never add the time to check the latest changes, but the plugins system seems great, will check it out soon. For the @callback attribute well yes I agree that having as default, without having to add a custom plugin, is reasonable. Do you want me to tackle it in the current main branch? I'll need some time to grok all the new changes.