Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remountable APIs, allows to re-mounting all APIs #1803

Merged
merged 17 commits into from
Oct 27, 2018

Conversation

myxoh
Copy link
Member

@myxoh myxoh commented Oct 19, 2018

Addresses this feature request: #570

A lot of times (specially when using versioning) we want to re-mount an endpoint in a different namespace, perhaps with some tiny bit of different configuration each time.

On our project, for example, we have a massive API using Grape, and while we want to introduce versioning we don't want to:
1- Have to copy & paste the whole project (nor re-write the whole project at once),
2- Have to re-write our project to use one of the proposed meta-programming approaches.

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes! This looks great. Minor changes in text and what not please?

@namusyaka Can I have a second pair of eyes when you have a chance?

README.md Outdated
You can mount the same endpoints in two different locations

```ruby
class Voting::API < Grape::GrapeAPI
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grape::API

README.md Outdated
end

class Post::API < Grape::API
mount Voting::API, with: {votable: 'posts'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rubocop, add spaces between { votable: 'posts' }.

root_api
end

describe 'mounted RemountableAPI' do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We no longer have RemountableAPI.

@dblock
Copy link
Member

dblock commented Oct 19, 2018

Other than our own fear, uncertainty and doubt, what are the downsides?

Specs pass. We trust the specs.

I will do a thorough code review. In the meantime do consider if any parts of the remountable implementation can be simplified by moving them into the api instance class itself.

module Grape
# The API Instance class, is the engine behind Grape::API. Each class that inherits
# from this will represent a different API
class APIInstance
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better called Grape::API::Instance? Or Grape::SomethingSomething::API (to match how we have Grape::DSL::API)? Just thinking out loud, no strong feelings.

The naming could be important because if someone is having trouble with the new Grape::API they could just inherit from Grape::API::Instance, making the diff prettier ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm happy with that naming. Regarding the solution if someone's having trouble: Should I add an entry that says something among those lines in updating.md ?

require 'grape/router'

module Grape
# The API Instance class, is the engine behind Grape::API. Each class that inherits
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.. represent a different API instance?

@@ -77,9 +77,12 @@ def do_not_route_options!
namespace_inheritable(:do_not_route_options, true)
end

def mount(mounts)
def mount(mounts, with: {})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we use named parameters elsewhere. I know we no longer support older Rubies, but if we don't maybe make this consistent as opts = {} and opts[:with] || {}?

mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
mounts.each_pair do |app, path|
if app.respond_to?(:mount_instance)
return mount(app.mount_instance(configuration: with) => path)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This return seems odd, can you explain? What if we have more mounts? I think it should be a if ... else for whatever follows?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the point of this return is not to execute the rest of the code as part of a Grape::API, only as part of a Grape::API::Instance. However, I do agree that return is the wrong approach, I just need to call next.
I'm surprised no test complained. Perhaps we don't have test coverage for mounting multiple apps, path on a single call?
Can you help me write a failing spec to make sure I better understand what the use case of this each_pair?

Do we support something like:

mount(API1 => '/api_1', API2 => '/api_2'})

?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're definitely missing tests.

I didn't know that we support this kind of mounting, but it looks like! Thanks for looking into it.

@@ -0,0 +1,85 @@
require 'spec_helper'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More naming, this is just about remounting, so maybe api_remount_spec.rb? NBD

@myxoh
Copy link
Member Author

myxoh commented Oct 19, 2018

No good reason, it's mainly because this is my first contribution to Grape, and due to the potential impact I wanted to avoid modifying the core behaviour so drastically. However, if you are happy with giving it a large look, I'm happy with implementing and supporting this changes.
I'll address the naming in a little moment. Should we close the other PR?

mounts = { mounts => '/' } unless mounts.respond_to?(:each_pair)
mounts.each_pair do |app, path|
if app.respond_to?(:mount_instance)
return mount(app.mount_instance(configuration: opts[:with] || {}) => path)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The return issue is still not addressed, I still need a failing spec to see whether next is appropiate, or whether this is better handled by a refactor

@myxoh
Copy link
Member Author

myxoh commented Oct 19, 2018

Also waiting on what you think we should do on the updating (as technically no change is required, but might be helpful to have information explaining how to fallback)

@dblock
Copy link
Member

dblock commented Oct 19, 2018

No good reason, it's mainly because this is my first contribution to Grape, and due to the potential impact I wanted to avoid modifying the core behaviour so drastically. However, if you are happy with giving it a large look, I'm happy with implementing and supporting this changes.

What could go wrong :)

I'll address the naming in a little moment. Should we close the other PR?

Yes, go for it.

@dblock
Copy link
Member

dblock commented Oct 19, 2018

Also waiting on what you think we should do on the updating (as technically no change is required, but might be helpful to have information explaining how to fallback)

You should write a paragraph in UPGRADING.

@myxoh myxoh changed the title Remountable APIs, allows to re-mounting all APIs - Alternative approach Remountable APIs, allows to re-mounting all APIs Oct 19, 2018
@myxoh
Copy link
Member Author

myxoh commented Oct 20, 2018

Also waiting on what you think we should do on the updating (as technically no change is required, but might be helpful to have information explaining how to fallback)

You should write a paragraph in UPGRADING.

Done. The only point is that I've added the upgrading to upgrading to >= 1.1.1, not sure whether this PR should go in a point release. Of course that's completely your decision 👍 .

Let me know anything else you need to get this over the line 😄

@myxoh
Copy link
Member Author

myxoh commented Oct 22, 2018

By trying it out on a big project, I've faced a bunch of breaking issues with no easy solution

  • Grape swagger seemed completely broken
  • Referencing a constant without the namespace is broken.

I'm back to the drawing board, and adding specs to represent these issues later today

@myxoh
Copy link
Member Author

myxoh commented Oct 22, 2018

So. After a lot of investigation, issues were rooted in 2 main causes.
1- Autoloading in rails is ffed up. I would probably put a warning in the upgrading.md, but there's no easy solution. There was not a problem with the namespaces as I feared.
2- Grape-Swagger is opening and closing Grape::API but it should be opening and closing Grape::API::Instance (And also modifying the reference in GrapeSwagger#create_documentation_class to Grape::API::Instance)

After fixing these 2 things, I've got my project back working.

I'm waiting on your thoughts and what's next then

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should open an issue in Grape::Swagger related to this, we'll want a proper fix there before we release Grape 1.2 with this change since that gem is very popular. Maybe you can attempt it too? ;)

README.md Outdated
@@ -366,6 +368,59 @@ class Twitter::API < Grape::API
end
```

## Remounting

You can mount the same endpoints in two different locations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, we have periods at the end of sentences ;)

README.md Outdated
end
```

Assuming that the post and comment endpoints are mounted in `/posts` and `/comments`,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above.

UPGRADING.md Outdated
@@ -3,6 +3,15 @@ Upgrading Grape

### Upgrading to >= 1.1.1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably 1.2, lets increment this everywhere as part of this PR?

@dblock
Copy link
Member

dblock commented Oct 22, 2018

To summarize where we are:

  • Amazing work by @myxoh, thanks for hanging in here till we get this in.
  • Lets increment the version to 1.2 as part of this PR.
  • Lets open an issue in grape-swagger to make it compatible with Grape 1.2 automagically.
  • Waiting for another CR from another maintainer for this before merging since it's a big deal.

@myxoh
Copy link
Member Author

myxoh commented Oct 23, 2018

Bumped up the version, and opened this PR on grape-swagger: ruby-grape/grape-swagger#717 that had green specs locally (when changing the gemfile to point to the right branch)

@myxoh
Copy link
Member Author

myxoh commented Oct 25, 2018

I've managed to address the auto-loading issues, currently on the project I'm working on upgrading to this branch + upgrading swagger was done seamlessly with no failing specs, nor code-changes required.

I've updated the grape-swagger PR addressing the requested changes. I'm still waiting for the second reviewer to take a look.

Let me know if there's anything else I can do

@dblock
Copy link
Member

dblock commented Oct 25, 2018

LGTM, I'll wait on someone else to comment (@namusyaka if you have a moment?) otherwise will merge in a few.

@namusyaka
Copy link
Contributor

Sorry, I overlooked this PR.
This feature looks really promising, thanks so much @myxoh !

I'll review the code & design this weekend.

@namusyaka namusyaka self-requested a review October 25, 2018 15:59
Copy link
Contributor

@namusyaka namusyaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, nice work!

@dblock dblock merged commit ca80feb into ruby-grape:master Oct 27, 2018
@dblock
Copy link
Member

dblock commented Oct 27, 2018

I merged this.

@myxoh would you like to join the co-maintainers group and get this to release? If yes what's your rubygems email? (feel free to send it over to dblock[at]dblock[dot]org if you prefer)

@myxoh
Copy link
Member Author

myxoh commented Oct 29, 2018

Hi @dblock I've wrote you an e-mail from myxoh10[at]gmail[dot]com

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants