Skip to content

Commit 12a34a1

Browse files
authored
Merge pull request #359 from ahx/load-performance
Speedup loading very large OADs by deferring Schema instances
2 parents 83e23da + 1c574f0 commit 12a34a1

8 files changed

Lines changed: 10310 additions & 11 deletions

File tree

CHANGELOG.md

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

33
## Unreleased
44

5+
## 2.7.1
6+
7+
- Speedup loading very large OADs by deferring creation of JSONSchemer::Schema instances.
8+
59
## 2.7.0
610

711
- Allow to override path for schema matching with `config.path = ->(request) { '/prefix' + request.path } ` (https://github.com/ahx/openapi_first/issues/349)

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: .
33
specs:
4-
openapi_first (2.7.0)
4+
openapi_first (2.7.1)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.3.3, < 2.0)
@@ -80,7 +80,7 @@ GEM
8080
prism (1.4.0)
8181
racc (1.8.1)
8282
rack (3.1.13)
83-
rack-session (2.1.0)
83+
rack-session (2.1.1)
8484
base64 (>= 0.1.0)
8585
rack (>= 3.0.0)
8686
rack-test (2.2.0)
@@ -106,11 +106,11 @@ GEM
106106
rspec-expectations (3.13.4)
107107
diff-lcs (>= 1.2.0, < 2.0)
108108
rspec-support (~> 3.13.0)
109-
rspec-mocks (3.13.3)
109+
rspec-mocks (3.13.4)
110110
diff-lcs (>= 1.2.0, < 2.0)
111111
rspec-support (~> 3.13.0)
112112
rspec-support (3.13.3)
113-
rubocop (1.75.4)
113+
rubocop (1.75.5)
114114
json (~> 2.3)
115115
language_server-protocol (~> 3.17.0.2)
116116
lint_roller (~> 1.1.0)

benchmarks/Gemfile.lock

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ..
33
specs:
4-
openapi_first (2.7.0)
4+
openapi_first (2.7.1)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.3.3, < 2.0)
@@ -46,7 +46,7 @@ GEM
4646
base64 (>= 0.1.0)
4747
logger (>= 1.6.0)
4848
rack (>= 3.0.0, < 4)
49-
rack-session (2.1.0)
49+
rack-session (2.1.1)
5050
base64 (>= 0.1.0)
5151
rack (>= 3.0.0)
5252
regexp_parser (2.10.0)

examples/rails_app/Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PATH
22
remote: ../..
33
specs:
4-
openapi_first (2.7.0)
4+
openapi_first (2.7.1)
55
hana (~> 1.3)
66
json_schemer (>= 2.1, < 3.0)
77
openapi_parameters (>= 0.3.3, < 2.0)

lib/openapi_first/ref_resolver.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,31 @@ def each
127127
# You have to pass configuration or ref_resolver
128128
def schema(options)
129129
base_uri = URI::File.build({ path: "#{dir}/" })
130-
root = JSONSchemer::Schema.new(context, base_uri:, **options)
131-
# binding.irb if value['maxItems'] == 4
132-
JSONSchemer::Schema.new(value, nil, root, base_uri:, **options)
130+
Schema.new(value:, context:, base_uri:, options:)
131+
end
132+
end
133+
134+
# @visibility private
135+
# Defers initialization JSONSchemer::Schema, because that takes time.
136+
class Schema
137+
extend Forwardable
138+
139+
def initialize(value:, context:, base_uri:, options:)
140+
@value = value
141+
@context = context
142+
@base_uri = base_uri
143+
@options = options
144+
end
145+
146+
attr_reader :value, :context, :base_uri, :options
147+
148+
def_delegators :schema, :validate, :valid?
149+
150+
def schema
151+
@schema ||= begin
152+
root_schema = JSONSchemer::Schema.new(context, base_uri:, **options)
153+
JSONSchemer::Schema.new(value, nil, root_schema, base_uri:, **options)
154+
end
133155
end
134156
end
135157

lib/openapi_first/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module OpenapiFirst
4-
VERSION = '2.7.0'
4+
VERSION = '2.7.1'
55
end

spec/data/large.yaml

Lines changed: 10263 additions & 0 deletions
Large diffs are not rendered by default.

spec/openapi_first_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@
6464
expect(returned_definition).to be(original_definition)
6565
end
6666

67+
require 'benchmark'
68+
it 'works with a large document' do
69+
time = Benchmark.realtime do
70+
Timeout.timeout(2) do
71+
OpenapiFirst.load('./spec/data/large.yaml')
72+
end
73+
end
74+
expect(time).to be < 1
75+
end
76+
6777
describe 'only option' do
6878
specify 'with empty filter' do
6979
definition = OpenapiFirst.load(spec_path, only: nil)

0 commit comments

Comments
 (0)