Skip to content

Commit

Permalink
Filter for known HTTP methods on Path (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
klausmeyer authored Jun 14, 2023
1 parent f0ac4d0 commit 2aafc59
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 3 deletions.
10 changes: 9 additions & 1 deletion lib/openapi_contracts/doc/path.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ module OpenapiContracts
class Doc::Path
def initialize(schema)
@schema = schema
@methods = @schema.to_h do |method, _|

@methods = (known_http_methods & @schema.keys).to_h do |method|
[method, Doc::Method.new(@schema.navigate(method))]
end
end
Expand All @@ -14,5 +15,12 @@ def methods
def with_method(method)
@methods[method]
end

private

def known_http_methods
# https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods
%w(get head post put delete connect options trace patch).freeze
end
end
end
2 changes: 1 addition & 1 deletion lib/openapi_contracts/doc/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def fragment
pointer.map { |p| p.gsub('/', '~1') }.join('/').then { |s| "#/#{s}" }
end

delegate :dig, :fetch, :key?, :[], :to_h, to: :as_h
delegate :dig, :fetch, :keys, :key?, :[], :to_h, to: :as_h

def at_pointer(pointer)
self.class.new(raw, pointer)
Expand Down
6 changes: 6 additions & 0 deletions spec/fixtures/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ info:
tags:
- name: Auth
description: Authentication
- name: Comments
description: Comments
- name: Messages
description: Messages
- name: User
description: Operations about user
servers:
Expand Down Expand Up @@ -45,5 +49,7 @@ paths:
$ref: components/schemas/Numbers.yaml#/All
/messages/{id}:
$ref: 'paths/message.yaml'
/comments/{id}:
$ref: 'paths/comment.yaml'
/user:
$ref: 'paths/user.yaml'
25 changes: 25 additions & 0 deletions spec/fixtures/openapi/paths/comment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
parameters:
- name: id
in: path
description: Id of the comment.
required: true
schema:
type: string
get:
tags:
- Comment
summary: Get Comment
description: Get Comment
operationId: get_comment
responses:
'200':
description: OK
patch:
tags:
- Comment
summary: Update Comment
description: Patch Comment
operationId: patch_comment
responses:
'204':
description: OK
2 changes: 1 addition & 1 deletion spec/openapi_contracts/doc_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@

it { is_expected.to all be_a(OpenapiContracts::Doc::Response) }

it { is_expected.to have_attributes(count: 8) }
it { is_expected.to have_attributes(count: 10) }
end
end

0 comments on commit 2aafc59

Please sign in to comment.