Skip to content

Commit

Permalink
Dig into implicit relative $ref in specification.
Browse files Browse the repository at this point in the history
  • Loading branch information
Manfred committed Aug 21, 2024
1 parent fe6f6b5 commit fc3a823
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
10 changes: 6 additions & 4 deletions lib/reynard/specification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ def read
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/CyclomaticComplexity
# rubocop:disable Metrics/MethodLength
def dig_into(data, cursor, path, filesystem_path)
while path.length.positive?
Expand All @@ -129,8 +128,8 @@ def dig_into(data, cursor, path, filesystem_path)
path = Rack::Utils.unescape_path(cursor['$ref'][2..]).split('/') + path
cursor = data
# References another file, with an optional anchor to an element in the data.
when %r{\A\./}
external = External.new(path: filesystem_path, ref: cursor['$ref'])
else
external = External.new(basepath:, path: filesystem_path, ref: cursor['$ref'])
filesystem_path = external.filesystem_path
path = external.path + path
cursor = external.data
Expand All @@ -139,7 +138,10 @@ def dig_into(data, cursor, path, filesystem_path)
cursor
end
# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/CyclomaticComplexity
# rubocop:enable Metrics/MethodLength

def basepath
File.dirname(File.expand_path(@filename))
end
end
end
20 changes: 17 additions & 3 deletions test/files/openapi/external.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,29 @@ paths:
schema:
type: integer
responses:
'200':
"200":
description: A author.
content:
application/json:
schema:
$ref: './schemas/author.yml'
$ref: "./schemas/author.yml"
default:
description: An error.
content:
application/json:
schema:
$ref: "./simple.yml#/components/schemas/Error"
$ref: "./simple.yml#/components/schemas/Error"
/authors/me:
get:
summary: Fetch own author profile
description: Fetch all details for own author profile
operationId: fetchAuthenticatedAuthor
tags:
- authors
responses:
"200":
description: A author.
content:
application/json:
schema:
$ref: "schemas/author.yml"
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
end

test 'digs into an external file through a reference' do
data = @specification.dig(
'paths', '/authors/me', 'get', 'responses', '200',
'content', 'application/json', 'schema',
'properties', 'id', 'type'
)
assert_equal 'integer', data
end

test 'digs into an external file through an explicit relative reference' do
data = @specification.dig(
'paths', '/authors/{id}', 'get', 'responses', '200',
'content', 'application/json', 'schema',
Expand Down

0 comments on commit fc3a823

Please sign in to comment.