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

Support OpenAPI 3 remote $ref #266

Merged
merged 1 commit into from
May 13, 2020
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
2 changes: 1 addition & 1 deletion committee.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency "json_schema", "~> 0.14", ">= 0.14.3"

s.add_dependency "rack", ">= 1.5"
s.add_dependency "openapi_parser", ">= 0.6.1"
s.add_dependency "openapi_parser", ">= 0.11.1"

s.add_development_dependency "minitest", "~> 5.3"
s.add_development_dependency "rack-test", "~> 0.6"
Expand Down
10 changes: 5 additions & 5 deletions lib/committee/drivers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ def self.driver_from_name(name)
# @param [String] schema_path
# @return [Committee::Driver]
def self.load_from_json(schema_path)
load_from_data(JSON.parse(File.read(schema_path)))
load_from_data(JSON.parse(File.read(schema_path)), schema_path)
end

# load and build drive from YAML file
# @param [String] schema_path
# @return [Committee::Driver]
def self.load_from_yaml(schema_path)
load_from_data(YAML.load_file(schema_path))
load_from_data(YAML.load_file(schema_path), schema_path)
end

# load and build drive from file
Expand All @@ -48,10 +48,10 @@ def self.load_from_file(schema_path)
# load and build drive from Hash object
# @param [Hash] hash
# @return [Committee::Driver]
def self.load_from_data(hash)
def self.load_from_data(hash, schema_path = nil)
if hash['openapi']&.start_with?('3.0.')
parser = OpenAPIParser.parse(hash)
return Committee::Drivers::OpenAPI3::Driver.new.parse(parser)
openapi = OpenAPIParser.parse_with_filepath(hash, schema_path)
return Committee::Drivers::OpenAPI3::Driver.new.parse(openapi)
end

driver = if hash['swagger'] == '2.0'
Expand Down
10 changes: 10 additions & 0 deletions test/data/openapi3/normal.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,16 @@ paths:
'204':
description: no content

/ref-sample:
get:
responses:
'200':
description: sample of remote schema reference
content:
application/json:
schema:
$ref: referee.yaml#/components/schemas/referred_schema

components:
schemas:
nested_array:
Expand Down
17 changes: 17 additions & 0 deletions test/data/openapi3/referee.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
openapi: 3.0.0
info:
version: 1.0.0
title: OpenAPI3 Test
description: A Sample file
servers:
- url: https://github.com/interagent/committee/
paths: {}
components:
schemas:
referred_schema:
type: object
properties:
sample:
type: string
required:
- sample
14 changes: 14 additions & 0 deletions test/middleware/response_validation_open_api_3_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,20 @@ def app
assert_match(/valid JSON/i, last_response.body)
end

describe "remote schema $ref" do
it "passes through a valid response" do
@app = new_response_rack(JSON.generate({ "sample" => "value" }), {}, schema: open_api_3_schema)
get "/ref-sample"
assert_equal 200, last_response.status
end

it "detects a invalid response" do
@app = new_response_rack("{}", {}, schema: open_api_3_schema)
get "/ref-sample"
assert_equal 500, last_response.status
end
end

describe 'check header' do
[
{ check_header: true, description: 'valid value', header: { 'integer' => 1 }, expected: { status: 200 } },
Expand Down
2 changes: 1 addition & 1 deletion test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ def open_api_2_schema
end

def open_api_3_schema
@open_api_3_schema ||= Committee::Drivers.load_from_data(open_api_3_data)
@open_api_3_schema ||= Committee::Drivers.load_from_file(open_api_3_schema_path)
end

# Don't cache this because we'll often manipulate the created hash in tests.
Expand Down