ruby-grape / grape-swagger

Add OAPI/swagger v2.0 compliant documentation to your grape API
MIT License
1.09k stars 472 forks source link

enhancemnt to "add_root" feature #781

Open urkle opened 4 years ago

urkle commented 4 years ago

The new add_root feature is extremely nice and something I've been waiting for in this gem for a while.

One small issue with it is that the way it handles dynamically building the 'root' name is solely based on the response model class name. Where as, ideally what would be nice is to "detect" if said response model is a grape-entity class and query the "Root" configuration from that class.

I have several GrapeEntity classes that change the "root" to be someone slightly different than the internal class name (e.g. OrganizationMember -> 'members'/'member') so having the swagger gem consult the entity class. (e.g. simply calling OrganizationMember.root_element(root_type) where root_type is :collection_root or :root ).

urkle commented 4 years ago

This has further been made harder with the upgrade to Grape swagger 1.1.0 as now the full class-name hierarchy is used as the entity name, thus if I have things in V2::Entities::MyModel the resulting calculated "root" is v2/entities/my_model which is very incorrect. I'll be making a PR to try and see how I can best inject querying the entity model for the "root" and see how that goes.

urkle commented 4 years ago

@LeFnord Do you have any thoughts on the best way to address this issue? Initial looking at the code shows that the method that determine this doesn't have the entity class but rather only the resulting string from the entity_name calculation. So to allow my proposal here would require some refactoring of the internals to pass enough context around.