diff --git a/README.md b/README.md index 811ef203..41f892be 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/rspec/openapi.rb b/lib/rspec/openapi.rb index 66b40078..41afbcd7 100644 --- a/lib/rspec/openapi.rb +++ b/lib/rspec/openapi.rb @@ -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 diff --git a/lib/rspec/openapi/default_schema.rb b/lib/rspec/openapi/default_schema.rb index accfaa2a..40262d89 100644 --- a/lib/rspec/openapi/default_schema.rb +++ b/lib/rspec/openapi/default_schema.rb @@ -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 diff --git a/lib/rspec/openapi/record.rb b/lib/rspec/openapi/record.rb index a7d19578..f04743b7 100644 --- a/lib/rspec/openapi/record.rb +++ b/lib/rspec/openapi/record.rb @@ -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" diff --git a/lib/rspec/openapi/record_builder.rb b/lib/rspec/openapi/record_builder.rb index c882d3af..90809e57 100644 --- a/lib/rspec/openapi/record_builder.rb +++ b/lib/rspec/openapi/record_builder.rb @@ -34,6 +34,12 @@ 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, @@ -41,6 +47,7 @@ def build(context, example:) 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), diff --git a/lib/rspec/openapi/schema_builder.rb b/lib/rspec/openapi/schema_builder.rb index 1c655d1d..9b877ec7 100644 --- a/lib/rspec/openapi/schema_builder.rb +++ b/lib/rspec/openapi/schema_builder.rb @@ -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 diff --git a/spec/rails/doc/openapi.yaml b/spec/rails/doc/openapi.yaml index 6f064264..9d2f3612 100644 --- a/spec/rails/doc/openapi.yaml +++ b/spec/rails/doc/openapi.yaml @@ -7,6 +7,7 @@ openapi: 3.0.3 info: title: rspec-openapi version: 1.0.0 +servers: [] paths: "/tables": get: @@ -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 diff --git a/spec/requests/rails_spec.rb b/spec/requests/rails_spec.rb index 80620024..e31e9fe6 100644 --- a/spec/requests/rails_spec.rb +++ b/spec/requests/rails_spec.rb @@ -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 @@ -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 diff --git a/spec/roda/doc/openapi.yaml b/spec/roda/doc/openapi.yaml index e4783380..c54d7864 100644 --- a/spec/roda/doc/openapi.yaml +++ b/spec/roda/doc/openapi.yaml @@ -3,6 +3,7 @@ openapi: 3.0.3 info: title: rspec-openapi version: 1.0.0 +servers: [] paths: "/roda": post: