apicollective / apibuilder

Simple, Comprehensive Tooling for Modern APIs
https://www.apibuilder.io/
MIT License
563 stars 83 forks source link

Add support for Play @routes in generated routes file #291

Open ecwall opened 9 years ago

ecwall commented 9 years ago

Controller Injection In Play, routes which start with @ are managed by play.api.GlobalSettings#getControllerInstance,

Given the following route definition:

GET / @controllers.SomeController.index() Then you can manage controller class instantiation using a DI framework by overriding getControllerInstance in your application’s Global class:

object Global extends play.api.GlobalSettings {

private val injector = SomeDependencyInjectionFramework

override def getControllerInstance[A](controllerClass: Class[A]): A = { injector.getInstance(controllerClass) } }

https://www.playframework.com/documentation/2.3.x/ScalaDependencyInjection

mbryzek commented 9 years ago

I'm not sure I understand the request - are you suggesting that we change the routes generator to always use DI? Is there a way to introduce this change programmatically?

Possible Alternatives:

ecwall commented 9 years ago

I did some researched about doing this programmatically and did not find anything for play 2.3, but it looks like it will be available for 2.4 (https://www.playframework.com/documentation/2.4.x/ScalaCompileTimeDependencyInjection). However, this approach looks like it involves manually matching routes to a controller instead of using the apidoc-generated routes file.

For api-referral we are currently manually copying the generated files into our project, but were planning on using the apidoc cli (when available) to generate our model, client and routes file automatically. I think it would be useful to be able to tell the generator (via api.json) which routes should have an @ in front of them.

mbryzek commented 9 years ago

suggestions would be great on how to do this! Note that one of our core goals of the project is to avoid adding code generator info into the spec of the service itself which might prove tricky in this case

ecwall commented 9 years ago

I thought of 3 ways to do this:

1) Adding a new optional property to api.json to enable this feature 2) Adding a toggle on the settings page (ex http://www.apidoc.me/gilt/referral/0.0.9-ewall-draft/settings) 3) Generate 2 routes files (one with the @ and one without the @)

mbryzek commented 9 years ago

Thanks! Couple comments:

1: would add code gen hints to api.json (we may get there, but for now hoping to avoid) 2: seems surprising 3: feels right

so I think this boils down to a new code generator. current routes generator is at:

https://github.com/gilt/apidoc-generator/blob/1428996a3e16deb14e8e5532f5cd16e48921fff6/generator/app/models/Play2RouteGenerator.scala

Feel free to contribute a new one! Otherwise, I'd like to wait to see how much interest there is for this feature / addition before committing to supporting it long term.

khe commented 9 years ago

+1 on this feature.

Play 2.4 is making Guice a first class citizen: https://www.playframework.com/documentation/2.4.x/ScalaDependencyInjection https://www.playframework.com/documentation/2.4.x/Migration24

Will probably contribute to the code gen

kiambogo commented 9 years ago

+1 on this.

Using Scaldi (http://scaldi.org/) for dependency injection also utilizes the @routes in Play.