Skip to content

Allow path-based config overrides - #162

Merged
exoego merged 5 commits into
exoego:masterfrom
jamerine:feat-allow-path-based-config-overrides
Jan 5, 2024
Merged

Allow path-based config overrides#162
exoego merged 5 commits into
exoego:masterfrom
jamerine:feat-allow-path-based-config-overrides

Conversation

@cyrusg

@cyrusg cyrusg commented Dec 13, 2023

Copy link
Copy Markdown
Contributor

Closes #161

Complex API's often use different server paths and have specific title and other information for different services. The purpose of this PR is to allow for such configurations to be overridden within each path defined in RSpec::OpenAPI.path. The PR also allows for the naming of the config file (defaulting to rspec_openapi.rb).

While the current initializer can define all the paths and common settings like the request and response headers, security schemes, and others, the path-based configuration files can be used to define server and other path/service-specific information.

@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)
eval(File.read(config_file)) if File.exist?(config_file)

Check notice

Code scanning / Rubocop

The use of eval represents a serious security risk.

Security/Eval: The use of `eval` is a serious security risk.
@codecov

codecov Bot commented Dec 13, 2023

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (1d3dccc) 96.79% compared to head (ef2b8d9) 96.82%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #162      +/-   ##
==========================================
+ Coverage   96.79%   96.82%   +0.02%     
==========================================
  Files          14       14              
  Lines         437      441       +4     
  Branches       97       98       +1     
==========================================
+ Hits          423      427       +4     
  Misses         14       14              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@cyrusg
cyrusg marked this pull request as ready for review December 13, 2023 21:48
@exoego

exoego commented Dec 14, 2023

Copy link
Copy Markdown
Owner

Complex API's often use different server paths and have specific title and other information for different services.

Could you let me know?
Do you want to use multiple config filed so you can create multiple OpenAPI files based on paths, something like:

# open_api_v1.rb
RSpec::OpenAPI.title = 'API v1 (legacy, unmaintained)'
RSpec::OpenAPI.path = 'doc/schema_v1.yaml'


# open_api_v2.rb
RSpec::OpenAPI.title = 'API v2'
RSpec::OpenAPI.path = 'doc/schema_v2.yaml'

If so, RSpec::OpenAPI.path allows splitting the files based on paths.

RSpec::OpenAPI.path = -> (example) {
  case example.file_path
  when %r[spec/requests/api/v1/] then 'doc/openapi/v1.yaml'
  when %r[spec/requests/api/v2/] then 'doc/openapi/v2.yaml'
  else 'doc/openapi.yaml'
  end
}

@cyrusg cyrusg changed the title Feat allow path based config overrides Allow path-based config overrides Dec 14, 2023
@cyrusg

cyrusg commented Dec 14, 2023

Copy link
Copy Markdown
Contributor Author

Could you let me know? Do you want to use multiple config filed so you can create multiple OpenAPI files based on paths, something like:

# open_api_v1.rb
RSpec::OpenAPI.title = 'API v1 (legacy, unmaintained)'
RSpec::OpenAPI.path = 'doc/schema_v1.yaml'


# open_api_v2.rb
RSpec::OpenAPI.title = 'API v2'
RSpec::OpenAPI.path = 'doc/schema_v2.yaml'

If so, RSpec::OpenAPI.path allows splitting the files based on paths.

RSpec::OpenAPI.path = -> (example) {
  case example.file_path
  when %r[spec/requests/api/v1/] then 'doc/openapi/v1.yaml'
  when %r[spec/requests/api/v2/] then 'doc/openapi/v2.yaml'
  else 'doc/openapi.yaml'
  end
}

Basically yes, that's what we're looking for. But my understanding is that path splitting just tells the gem where to dump the schemas for each API. We need to be able to change the title, server locations, etc. I think this is also what ipepe is asking for in #161.

A little more context: We are a benefits company and have several API's such as enrollments, mobile, etc. They all live in the same base repo, so we somehow have to be able to generate documentation with the right titles, etc. for each of them.

Right now, we have a config file in our config/initializers folder that has most of the common setting across our API's. But each API has a different title, description, and server path(s). And that's the impetus for this PR.

@exoego
exoego merged commit 8f3bb94 into exoego:master Jan 5, 2024
@exoego

exoego commented Jan 5, 2024

Copy link
Copy Markdown
Owner

@cyrusg Thanks.
I've added some commits in #165 and merged your PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using RSpec::OpenAPI.path = -> but for different audiences

3 participants