Skip to content

Commit 9f4f989

Browse files
committed
Compile endpoints on first call or when API compile!
1 parent bf90e95 commit 9f4f989

File tree

5 files changed

+19
-32
lines changed

5 files changed

+19
-32
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/api/instance.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def initialize
104104
add_head_not_allowed_methods_and_options_methods
105105
self.class.endpoints.each do |endpoint|
106106
endpoint.mount_in(@router)
107+
endpoint.compile!
107108
end
108109

109110
@router.compile!

lib/grape/endpoint.rb

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,14 @@ 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
8279
@source = self.class.block_to_unbound_method(block)
8380
@before_filter_passed = false
81+
@compiled = false
8482
end
8583

8684
# Update our settings from a given set of stackable parameters. Used when
@@ -124,7 +122,7 @@ def namespace
124122
end
125123

126124
def call(env)
127-
lazy_initialize!
125+
compile!
128126
dup.call!(env)
129127
end
130128

@@ -155,6 +153,14 @@ def inspect
155153
"#{self.class} in '#{route.origin}' endpoint"
156154
end
157155

156+
def compile!
157+
return if @compiled
158+
159+
@app = options[:app] || build_stack
160+
@helpers = build_helpers
161+
@compiled = true
162+
end
163+
158164
protected
159165

160166
def run
@@ -203,18 +209,6 @@ def execute
203209
end
204210
end
205211

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-
218212
def run_validators(validators, request)
219213
validation_errors = []
220214

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)