From 18660a0e03951a6510ead2c01e4eb8345e6b38d3 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Tue, 31 Mar 2026 17:44:52 -0400 Subject: [PATCH 1/3] Cache files in FileLoader --- lib/openapi_first/file_loader.rb | 27 +++++++++++++++++++++------ 1 file changed, 21 insertions(+), 6 deletions(-) diff --git a/lib/openapi_first/file_loader.rb b/lib/openapi_first/file_loader.rb index 45082412..d987281b 100644 --- a/lib/openapi_first/file_loader.rb +++ b/lib/openapi_first/file_loader.rb @@ -6,17 +6,32 @@ module OpenapiFirst # @!visibility private module FileLoader + @cache = {} + @mutex = Mutex.new + module_function def load(file_path) - raise FileNotFoundError, "File not found #{file_path.inspect}" unless File.exist?(file_path) + @cache[file_path] || @mutex.synchronize do + @cache[file_path] ||= begin + raise FileNotFoundError, "File not found #{file_path.inspect}" unless File.exist?(file_path) + + body = File.read(file_path) + extname = File.extname(file_path) - body = File.read(file_path) - extname = File.extname(file_path) - return ::JSON.parse(body) if extname == '.json' - return YAML.unsafe_load(body) if ['.yaml', '.yml'].include?(extname) + if extname == '.json' + ::JSON.parse(body) + elsif ['.yaml', '.yml'].include?(extname) + YAML.unsafe_load(body) + else + body + end + end + end + end - body + def clear_cache + @mutex.synchronize { @cache.clear } end end end From 7305904a1c7df1ffdf70c957cc82a5e292c28485 Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Wed, 1 Apr 2026 12:41:12 -0400 Subject: [PATCH 2/3] Rename clear_cache! method --- lib/openapi_first/file_loader.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/openapi_first/file_loader.rb b/lib/openapi_first/file_loader.rb index d987281b..8dc4dcd1 100644 --- a/lib/openapi_first/file_loader.rb +++ b/lib/openapi_first/file_loader.rb @@ -30,7 +30,7 @@ def load(file_path) end end - def clear_cache + def clear_cache! @mutex.synchronize { @cache.clear } end end From 6cb1dc67f83d01be406bb81303227c6e26d86a2b Mon Sep 17 00:00:00 2001 From: Michael Oberegger Date: Wed, 1 Apr 2026 12:41:21 -0400 Subject: [PATCH 3/3] Add to release notes --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7eb17cd1..1f9539c5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +- `OpenapiFirst::FileLoader` will now cache the contents of files that have been loaded. This cache + is keyed on the file's path. If you need to reload your OpenAPI definition for tests or server hot + reloading, you can run `OpenapiFirst::FileLoader.clear_cache!`. + ## 3.2.1 - Don't raise `UnknownQueryParameterError` if request is ignored in tests. Fixes [#441](https://github.com/ahx/openapi_first/issues/441).