Skip to content

Commit

Permalink
Customizations via metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattia Roccoberton committed Mar 16, 2022
1 parent 4a1cf47 commit b9c9cfc
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,22 @@ RSpec.describe '/resources', type: :request do
end
```

## Customizations

Some examples' attributes can be overwritten via RSpec metadata options. Example:

```rb
describe 'GET /api/v1/posts', openapi: {
summary: 'list all posts',
description: 'list all posts ordered by pub_date',
tags: %w[v1 posts],
} do
# ...
end
```

**NOTE**: `description` key will override also the one provided by `RSpec::OpenAPI.description_builder` method.

## Links

Existing RSpec plugins which have OpenAPI integration:
Expand Down
8 changes: 5 additions & 3 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ def build(context, example:)
headers_arr << [header, header_value] if header_value
end

metadata_options = example.metadata[:openapi] || {}

RSpec::OpenAPI::Record.new(
method: request.request_method,
path: path,
Expand All @@ -48,9 +50,9 @@ def build(context, example:)
request_params: raw_request_params(request),
request_content_type: request.media_type,
request_headers: request_headers,
summary: summary,
tags: tags,
description: RSpec::OpenAPI.description_builder.call(example),
summary: metadata_options[:summary] || summary,
tags: metadata_options[:tags] || tags,
description: metadata_options[:description] || RSpec::OpenAPI.description_builder.call(example),
status: response.status,
response_body: response_body,
response_content_type: response.media_type,
Expand Down

0 comments on commit b9c9cfc

Please sign in to comment.