Skip to content

Commit

Permalink
Add Definition#[] to access a raw Hash representation of the OAS do…
Browse files Browse the repository at this point in the history
…cument

Related to #280
  • Loading branch information
ahx committed Jul 17, 2024
1 parent 48358a1 commit 811e617
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Add `Definition#[]` to access a raw Hash representation of the OAS document

## 2.0.3

- Fix `OpenapiFirst::Test.register` https://github.com/ahx/openapi_first/issues/276
Expand Down
5 changes: 5 additions & 0 deletions lib/openapi_first/definition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
require_relative 'request'
require_relative 'response'
require_relative 'builder'
require 'forwardable'

module OpenapiFirst
# Represents an OpenAPI API Description document
# This is returned by OpenapiFirst.load.
class Definition
extend Forwardable
attr_reader :filepath, :config, :paths, :router

# @param resolved [Hash] The resolved OpenAPI document.
Expand All @@ -20,9 +22,12 @@ def initialize(resolved, filepath = nil)
yield @config if block_given?
@config.freeze
@router = Builder.build_router(resolved, @config)
@resolved = resolved
@paths = resolved['paths'].keys # TODO: Move into builder as well
end

def_delegators :@resolved, :[]

def routes
@router.routes
end
Expand Down
9 changes: 9 additions & 0 deletions spec/definition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@ def build_request(path, method: 'GET')
end
end

describe '#[]' do
it 'gives access to the raw hash' do
definition = OpenapiFirst.load('./spec/data/train-travel-api/openapi.yaml')
expect(definition['webhooks']).to be_a(Hash)
expect(definition['webhooks'].dig('newBooking', 'post', 'operationId')).to eq('new-booking')
expect(definition['components'].dig('schemas', 'Station', 'type')).to eq('object')
end
end

describe '#paths' do
it 'returns all paths' do
definition = OpenapiFirst.load('./spec/data/petstore.yaml')
Expand Down

0 comments on commit 811e617

Please sign in to comment.