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
3 changes: 2 additions & 1 deletion lib/reynard/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ def build_body(operation_node, data)
end

def operation(operation_name)
dig('paths').each do |path, operations|
dig('paths').each_key do |path|
operations = dig('paths', path)
operations.slice(*VERBS).each do |verb, operation|
return Operation.new(node: ['paths', path, verb]) if operation_name == operation['operationId']
end
Expand Down
2 changes: 2 additions & 0 deletions test/files/openapi/external.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,5 @@ paths:
application/json:
schema:
$ref: "schemas/author.yml"
/authors:
$ref: "./paths/authors.yml#/paths/index"
21 changes: 21 additions & 0 deletions test/files/openapi/paths/authors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
paths:
index:
get:
summary: Fetch authors
description: Fetch a list of all authors
operationId: listAuthors
tags:
- authors
responses:
"200":
description: A list of authors
content:
application/json:
schema:
$ref: "../schemas/authors.yml"
default:
description: An error.
content:
application/json:
schema:
$ref: "../simple.yml#/components/schemas/Error"
6 changes: 6 additions & 0 deletions test/files/openapi/schemas/authors.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: array
title: Authors
description: >-
A list of authors of a book.
items:
$ref: "author.yml"
25 changes: 25 additions & 0 deletions test/reynard/object_builder_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,31 @@ def setup
end
end

class ExternalRequestPathAndDeepRefsBuilderTest < Reynard::Test
def setup
@specification = Specification.new(filename: fixture_file('openapi/external.yml'))
@inflector = Inflector.new
end

test 'builds a singular record' do
operation = @specification.operation('listAuthors')
media_type = @specification.media_type(operation.node, '200', 'application/json')
schema = @specification.schema(media_type.node)
authors = Reynard::ObjectBuilder.new(
schema:,
inflector: @inflector,
parsed_body: [
{ id: 42, name: 'Jerry Writer', bio: { age: 42 } }
]
).call
assert_model_name('AuthorsCollection', authors)
authors.each do |author|
assert_model_name('Author', author)
assert_equal 42, author.id
end
end
end

class TitledObjectBuilderTest < Reynard::Test
def setup
@specification = Specification.new(filename: fixture_file('openapi/titled.yml'))
Expand Down
2 changes: 1 addition & 1 deletion test/reynard/schema/model_naming_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ModelNamingTest < Reynard::Test
class RegressionModelNamingTest < Reynard::Test
EXPECTED = {
'bare' => [],
'external' => %w[Author Bio Error Author Bio],
'external' => %w[Author Bio Error Author Bio Author Bio Error AuthorsCollection],
'minimal' => %w[Spaceship SpaceshipCollection],
'naming' => %w[
Sector Subsector IndustryGroup Industry NationalIndustry Art NationalIndustry
Expand Down
9 changes: 9 additions & 0 deletions test/reynard/specification_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,15 @@ def setup
@specification = Specification.new(filename: fixture_file('openapi/external.yml'))
end

test 'finds an operation, loads media type, and returns schema defined externally in a file' do
operation = @specification.operation('listAuthors')
media_type = @specification.media_type(operation.node, '200', 'application/json')
assert_equal(
'array',
@specification.dig(*media_type.node, 'schema', 'type')
)
end

test 'digs into an external file' do
data = @specification.dig(
'paths', '/authors/{id}', 'get', 'responses', '200',
Expand Down