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

Allow path-based config overrides #165

Merged
merged 9 commits into from
Jan 5, 2024
5 changes: 5 additions & 0 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ module RSpec::OpenAPI
@path_records = Hash.new { |h, k| h[k] = [] }
@ignored_path_params = %i[controller action format]

# This is the configuraion override file name we look for within each path.
@config_filename = 'rspec_openapi.rb'

class << self
attr_accessor :path,
:title,
Expand All @@ -49,5 +52,7 @@ class << self
:response_headers,
:path_records,
:ignored_path_params

attr_reader :config_filename
end
end
10 changes: 9 additions & 1 deletion lib/rspec/openapi/result_recorder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@
end

def record_results!
title = RSpec::OpenAPI.title
@path_records.each do |path, records|
# Look for a path-specific config file and run it.
config_file = File.join(File.dirname(path), RSpec::OpenAPI.config_filename)
begin
require config_file if File.exist?(config_file)
rescue => e

Check notice

Code scanning / Rubocop

Avoid rescuing without specifying an error class. Note

Style/RescueStandardError: Avoid rescuing without specifying an error class.
puts "WARNING: Unable to load #{config_file}: #{e}"

Check warning on line 16 in lib/rspec/openapi/result_recorder.rb

View check run for this annotation

Codecov / codecov/patch

lib/rspec/openapi/result_recorder.rb#L16

Added line #L16 was not covered by tests
end

title = RSpec::OpenAPI.title
RSpec::OpenAPI::SchemaFile.new(path).edit do |spec|
schema = RSpec::OpenAPI::DefaultSchema.build(title)
schema[:info].merge!(RSpec::OpenAPI.info)
Expand Down
4 changes: 2 additions & 2 deletions spec/roda/doc/openapi.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"openapi": "3.0.3",
"info": {
"title": "OpenAPI Documentation",
"version": "1.0.0"
"title": "Override title",
"version": "7.7.7"
},
"servers": [

Expand Down
4 changes: 2 additions & 2 deletions spec/roda/doc/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
---
openapi: 3.0.3
info:
title: OpenAPI Documentation
version: 1.0.0
title: Override title
version: 7.7.7
servers: []
paths:
"/roda":
Expand Down
2 changes: 2 additions & 0 deletions spec/roda/doc/rspec_openapi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RSpec::OpenAPI.title = 'Override title'

Check notice

Code scanning / Rubocop

Add the frozen_string_literal comment to the top of files to help transition to frozen string literals by default. Note documentation test

Style/FrozenStringLiteralComment: Missing frozen string literal comment.
RSpec::OpenAPI.application_version = '7.7.7'