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

fix: Add filter for known HTTP methods to Path #50

Merged
merged 1 commit into from
Jun 14, 2023
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
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