Skip to content
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
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,16 @@ RSpec::OpenAPI.response_headers = %w[X-Cursor]
# Set `servers` - generate servers of a schema file
RSpec::OpenAPI.servers = [{ url: 'http://localhost:3000' }]

# Set `security_schemes` - generate security schemes
RSpec::OpenAPI.security_schemes = {
'MyToken' => {
description: 'Authenticate API requests via a JWT',
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT',
},
}

# 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 Expand Up @@ -189,7 +199,7 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/User"
# Note) #/components/schamas is not needed to be defined.
# Note) #/components/schamas is not needed to be defined.
```

3. Then, re-run rspec-openapi. It will generate `#/components/schemas` with the referenced schema (`User` for example) newly-generated or updated.
Expand Down Expand Up @@ -298,6 +308,7 @@ Some examples' attributes can be overwritten via RSpec metadata options. Example
summary: 'list all posts',
description: 'list all posts ordered by pub_date',
tags: %w[v1 posts],
security: [{"MyToken" => []}],
} do
# ...
end
Expand Down
2 changes: 2 additions & 0 deletions lib/rspec/openapi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module RSpec::OpenAPI
@application_version = '1.0.0'
@request_headers = []
@servers = []
@security_schemes = []
@example_types = %i[request]
@response_headers = []

Expand All @@ -22,6 +23,7 @@ class << self
:application_version,
:request_headers,
:servers,
:security_schemes,
:example_types,
:response_headers
end
Expand Down
12 changes: 10 additions & 2 deletions lib/rspec/openapi/default_schema.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
class << RSpec::OpenAPI::DefaultSchema = Object.new
def build(title)
{
spec = {
openapi: '3.0.3',
info: {
title: title,
version: RSpec::OpenAPI.application_version,
},
servers: RSpec::OpenAPI.servers,
paths: {},
}.freeze
}

if RSpec::OpenAPI.security_schemes.present?
spec[:components] = {
securitySchemes: RSpec::OpenAPI.security_schemes
}
end

spec.freeze
end
end
1 change: 1 addition & 0 deletions lib/rspec/openapi/record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
:summary, # @param [String] - "v1/statuses #show"
:tags, # @param [Array] - ["Status"]
:description, # @param [String] - "returns a status"
:security, # @param [Array] - [{securityScheme1: []}]
:status, # @param [Integer] - 200
:response_body, # @param [Object] - {"status" => "ok"}
:response_headers, # @param [Array] - [["header_key1", "header_value1"], ["header_key2", "header_value2"]]
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/record_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ def build(context, example:)
summary: metadata_options[:summary] || summary,
tags: metadata_options[:tags] || tags,
description: metadata_options[:description] || RSpec::OpenAPI.description_builder.call(example),
security: metadata_options[:security],
status: response.status,
response_body: response_body,
response_headers: response_headers,
Expand Down
1 change: 1 addition & 0 deletions lib/rspec/openapi/schema_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def build(record)
record.http_method.downcase => {
summary: record.summary,
tags: record.tags,
security: record.security,
parameters: build_parameters(record),
requestBody: build_request_body(record),
responses: {
Expand Down
10 changes: 5 additions & 5 deletions spec/rails/doc/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ paths:
database:
id: 2
name: production
null_sample:
null_sample:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
Expand Down Expand Up @@ -169,7 +169,7 @@ paths:
database:
id: 2
name: production
null_sample:
null_sample:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
Expand Down Expand Up @@ -222,7 +222,7 @@ paths:
database:
id: 2
name: production
null_sample:
null_sample:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
Expand Down Expand Up @@ -314,7 +314,7 @@ paths:
database:
id: 2
name: production
null_sample:
null_sample:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
Expand Down Expand Up @@ -366,7 +366,7 @@ paths:
database:
id: 2
name: production
null_sample:
null_sample:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
Expand Down
8 changes: 7 additions & 1 deletion spec/rails/doc/smart/expected.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ paths:
storage_size: 12.3
created_at: '2020-07-17T00:00:00+00:00'
updated_at: '2020-07-17T00:00:00+00:00'
"/users/{id}":
"/users/{id}":
get:
responses:
"200":
Expand Down Expand Up @@ -162,6 +162,12 @@ paths:
type: integer
example: 1
components:
securitySchemes:
Scheme1:
description: Authentication scheme
type: http
scheme: bearer
bearerFormat: JWT
schemas:
Table:
type: object
Expand Down
9 changes: 9 additions & 0 deletions spec/requests/rails_smart_merge_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@
update this file automatically. You can also manually edit this file.
COMMENT
RSpec::OpenAPI.servers = [{ url: 'http://localhost:3000' }]
RSpec::OpenAPI.security_schemes = {
'Scheme1' => {
description: 'Authentication scheme',
type: 'http',
scheme: 'bearer',
bearerFormat: 'JWT'
}
}

RSpec::OpenAPI.info = {
description: 'My beautiful API',
license: {
Expand Down