Skip to content

Commit 28b048c

Browse files
committed
Add compile! in mount_in function.
1 parent bf90e95 commit 28b048c

File tree

4 files changed

+18
-33
lines changed

4 files changed

+18
-33
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
* [#2643](https://github.com/ruby-grape/grape/pull/2638): Remove `try` method in codebase - [@ericproulx](https://github.com/ericproulx).
1313
* [#2646](https://github.com/ruby-grape/grape/pull/2646): Call `valid_encoding?` before scrub - [@ericproulx](https://github.com/ericproulx).
1414
* [#2644](https://github.com/ruby-grape/grape/pull/2644): Clean useless/not valuable dependencies - [@ericproulx](https://github.com/ericproulx).
15+
* [#2645](https://github.com/ruby-grape/grape/pull/2645): Endpoints are compiled when API is compiled - [@ericproulx](https://github.com/ericproulx).
16+
1517
* Your contribution here.
1618

1719
#### Fixes

lib/grape/endpoint.rb

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -71,11 +71,8 @@ def initialize(new_settings, **options, &block)
7171

7272
@options[:path] = Array(options[:path])
7373
@options[:path] << '/' if options[:path].empty?
74-
7574
@options[:method] = Array(options[:method])
7675

77-
@lazy_initialize_lock = Mutex.new
78-
@lazy_initialized = nil
7976
@status = nil
8077
@stream = nil
8178
@body = nil
@@ -105,9 +102,13 @@ def reset_routes!
105102
end
106103

107104
def mount_in(router)
108-
return endpoints.each { |e| e.mount_in(router) } if endpoints
105+
if endpoints
106+
compile!
107+
return endpoints.each { |e| e.mount_in(router) }
108+
end
109109

110110
reset_routes!
111+
compile!
111112
routes.each do |route|
112113
router.append(route.apply(self))
113114
next unless !inheritable_setting.namespace_inheritable[:do_not_route_head] && route.request_method == Rack::GET
@@ -124,7 +125,6 @@ def namespace
124125
end
125126

126127
def call(env)
127-
lazy_initialize!
128128
dup.call!(env)
129129
end
130130

@@ -203,18 +203,6 @@ def execute
203203
end
204204
end
205205

206-
def lazy_initialize!
207-
return true if @lazy_initialized
208-
209-
@lazy_initialize_lock.synchronize do
210-
return true if @lazy_initialized
211-
212-
@app = options[:app] || build_stack
213-
@helpers = build_helpers
214-
@lazy_initialized = true
215-
end
216-
end
217-
218206
def run_validators(validators, request)
219207
validation_errors = []
220208

@@ -266,6 +254,11 @@ def options?
266254

267255
attr_reader :before_filter_passed
268256

257+
def compile!
258+
@app = options[:app] || build_stack
259+
@helpers = build_helpers
260+
end
261+
269262
def to_routes
270263
route_options = options[:route_options]
271264
default_route_options = prepare_default_route_attributes(route_options)

lib/grape/router.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
module Grape
44
class Router
5-
attr_reader :map, :compiled
6-
75
# Taken from Rails
86
# normalize_path("/foo") # => "/foo"
97
# normalize_path("/foo/") # => "/foo"
@@ -39,22 +37,22 @@ def initialize
3937
end
4038

4139
def compile!
42-
return if compiled
40+
return if @compiled
4341

4442
@union = Regexp.union(@neutral_regexes)
4543
@neutral_regexes = nil
4644
(Grape::HTTP_SUPPORTED_METHODS + ['*']).each do |method|
47-
next unless map.key?(method)
45+
next unless @map.key?(method)
4846

49-
routes = map[method]
47+
routes = @map[method]
5048
optimized_map = routes.map.with_index { |route, index| route.to_regexp(index) }
5149
@optimized_map[method] = Regexp.union(optimized_map)
5250
end
5351
@compiled = true
5452
end
5553

5654
def append(route)
57-
map[route.request_method] << route
55+
@map[route.request_method] << route
5856
end
5957

6058
def associate_routes(greedy_route)
@@ -91,7 +89,7 @@ def identity(input, method, env)
9189

9290
def rotation(input, method, env, exact_route)
9391
response = nil
94-
map[method].each do |route|
92+
@map[method].each do |route|
9593
next if exact_route == route
9694
next unless route.match?(input)
9795

@@ -143,7 +141,7 @@ def process_route(route, input, env, include_allow_header: false)
143141
end
144142

145143
def with_optimization
146-
compile! unless compiled
144+
compile!
147145
yield || default_response
148146
end
149147

spec/grape/api/defines_boolean_in_params_spec.rb

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,5 @@
2222
expect(last_response.status).to eq(201)
2323
expect(last_response.body).to eq expected_body
2424
end
25-
26-
context 'Params endpoint type' do
27-
subject { app.new.router.map[Rack::POST].first.options[:params]['message'][:type] }
28-
29-
it 'params type is a boolean' do
30-
expect(subject).to eq 'Grape::API::Boolean'
31-
end
32-
end
3325
end
3426
end

0 commit comments

Comments
 (0)