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

Add ability to define custom summary and tags builders #148

Merged
merged 2 commits into from
Dec 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,14 @@ EOS
# Generate a custom description, given an RSpec example
RSpec::OpenAPI.description_builder = -> (example) { example.description }

# Generate a custom summary, given an RSpec example
# This example uses the summary from the example_group.
RSpec::OpenAPI.summary_builder = ->(example) { example.metadata.dig(:example_group, :openapi, :summary) }

# Generate a custom tags, given an RSpec example
# This example uses the tags from the parent_example_group
RSpec::OpenAPI.tags_builder = -> (example) { example.metadata.dig(:example_group, :parent_example_group, :openapi, :tags) }

# Change the example type(s) that will generate schema
RSpec::OpenAPI.example_types = %i[request]

Expand Down
4 changes: 4 additions & 0 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ module RSpec::OpenAPI
@comment = nil
@enable_example = true
@description_builder = ->(example) { example.description }
@summary_builder = ->(example) { example.metadata[:summary] }
@tags_builder = ->(example) { example.metadata[:tags] }
@info = {}
@application_version = '1.0.0'
@request_headers = []
Expand All @@ -35,6 +37,8 @@ class << self
:comment,
:enable_example,
:description_builder,
:summary_builder,
:tags_builder,
:info,
:application_version,
:request_headers,
Expand Down
4 changes: 2 additions & 2 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ def extract_headers(request, response)

def extract_request_attributes(request, example)
metadata = example.metadata[:openapi] || {}
summary = metadata[:summary]
tags = metadata[:tags]
summary = metadata[:summary] || RSpec::OpenAPI.summary_builder.call(example)
tags = metadata[:tags] || RSpec::OpenAPI.tags_builder.call(example)
operation_id = metadata[:operation_id]
required_request_params = metadata[:required_request_params] || []
security = metadata[:security]
Expand Down