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

Generate headers and servers block #30

Merged
merged 3 commits into from
Aug 23, 2021
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 @@ -114,6 +114,14 @@ RSpec::OpenAPI.enable_example = false
# Change `info.version`
RSpec::OpenAPI.application_version = '1.0.0'

# Set `headers` - generate parameters with headers for request
RSpec::OpenAPI.headers = %w[X-Authorization-Token]

# Set `server_urls` - generate servers of a schema file
RSpec::OpenAPI.server_urls = %w[
http://localhost:3000
]

# Generate a comment on top of a schema file
RSpec::OpenAPI.comment = <<~EOS
This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi
Expand Down
4 changes: 3 additions & 1 deletion lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ module RSpec::OpenAPI
@enable_example = true
@description_builder = -> (example) { example.description }
@application_version = '1.0.0'
@headers = []
@server_urls = []

class << self
attr_accessor :path, :comment, :enable_example, :description_builder, :application_version
attr_accessor :path, :comment, :enable_example, :description_builder, :application_version, :headers, :server_urls
end
end
1 change: 1 addition & 0 deletions lib/rspec/openapi/default_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ def build(title)
title: title,
version: RSpec::OpenAPI.application_version,
},
servers: RSpec::OpenAPI.server_urls.map { |url| { url: url } } || [],
paths: {},
}.freeze
end
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:query_params, # @param [Hash] - {:query=>"string"}
:request_params, # @param [Hash] - {:request=>"body"}
:request_content_type, # @param [String] - "application/json"
:request_headers, # @param [Array] - [["header_key1", "header_value1"], ["header_key2", "header_value2"]]
:summary, # @param [String] - "v1/statuses #show"
:tags, # @param [Array] - ["Status"]
:description, # @param [String] - "returns a status"
Expand Down
7 changes: 7 additions & 0 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,20 @@ def build(context, example:)
nil
end

request_headers = RSpec::OpenAPI.headers.each_with_object([]) do |header, headers_arr|
header_key = header.gsub(/-/, '_').upcase
header_value = request.get_header(['HTTP', header_key].join('_')) || request.get_header(header_key)
headers_arr << [header, header_value] if header_value
end

RSpec::OpenAPI::Record.new(
method: request.request_method,
path: path,
path_params: raw_path_params(request),
query_params: request.query_parameters,
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),
Expand Down
10 changes: 10 additions & 0 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ def build_parameters(record)
}.compact
end

record.request_headers.each do |key, value|
parameters << {
name: build_parameter_name(key, value),
in: 'header',
required: true,
schema: build_property(try_cast(value)),
example: (try_cast(value) if example_enabled?),
}.compact
end

return nil if parameters.empty?
parameters
end
Expand Down
7 changes: 7 additions & 0 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ openapi: 3.0.3
info:
title: rspec-openapi
version: 1.0.0
servers: []
paths:
"/tables":
get:
Expand Down Expand Up @@ -42,6 +43,12 @@ paths:
type: string
example:
price: '0'
- name: X-Authorization-Token
in: header
required: true
schema:
type: string
example: token
responses:
'200':
description: returns a list of tables
Expand Down
4 changes: 3 additions & 1 deletion spec/requests/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
require File.expand_path('../rails/config/environment', __dir__)
require 'rspec/rails'

RSpec::OpenAPI.headers = %w[X-Authorization-Token]
RSpec::OpenAPI.path = File.expand_path('../rails/doc/openapi.yaml', __dir__)
RSpec::OpenAPI.comment = <<~EOS
This file is auto-generated by rspec-openapi https://github.com/k0kubun/rspec-openapi
Expand All @@ -15,7 +16,8 @@
describe '#index' do
context it 'returns a list of tables' do
it 'with flat query parameters' do
get '/tables', params: { page: '1', per: '10' }, headers: { authorization: 'k0kubun' }
get '/tables', params: { page: '1', per: '10' },
headers: { authorization: 'k0kubun', "X-Authorization-Token": 'token' }
expect(response.status).to eq(200)
end

Expand Down
1 change: 1 addition & 0 deletions spec/roda/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ openapi: 3.0.3
info:
title: rspec-openapi
version: 1.0.0
servers: []
paths:
"/roda":
post:
Expand Down