-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Consider all parameters for routing * Rename Method to Operation * Cleanup routing * DRY schema validation * Rename validators * improve message without pointer * Correctly name media_type * Fail on non-json payloads * Add PayloadParser
- Loading branch information
Showing
37 changed files
with
509 additions
and
389 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
module OpenapiContracts | ||
class Doc::Operation | ||
include Doc::WithParameters | ||
|
||
def initialize(path, spec) | ||
@path = path | ||
@spec = spec | ||
@responses = spec.navigate('responses').each.to_h do |status, subspec| # rubocop:disable Style/HashTransformValues | ||
[status, Doc::Response.new(subspec)] | ||
end | ||
end | ||
|
||
def request_body | ||
return @request_body if instance_variable_defined?(:@request_body) | ||
|
||
@request_body = @spec.navigate('requestBody')&.then { |s| Doc::Request.new(s) } | ||
end | ||
|
||
def responses | ||
@responses.each_value | ||
end | ||
|
||
def response_for_status(status) | ||
@responses[status.to_s] | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,44 @@ | ||
module OpenapiContracts | ||
class Doc::Path | ||
def initialize(path, schema) | ||
@path = path | ||
@schema = schema | ||
include Doc::WithParameters | ||
|
||
@methods = (known_http_methods & @schema.keys).to_h do |method| | ||
[method, Doc::Method.new(@schema.navigate(method))] | ||
end | ||
HTTP_METHODS = %w(get head post put delete connect options trace patch).freeze | ||
|
||
def initialize(path, spec) | ||
@path = path | ||
@spec = spec | ||
@supported_methods = HTTP_METHODS & @spec.keys | ||
end | ||
|
||
def dynamic? | ||
@path.include?('{') | ||
end | ||
|
||
def matches?(path) | ||
@path == path || regexp_path.match(path) do |m| | ||
m.named_captures.each do |k, v| | ||
return false unless parameter_matches?(k, v) | ||
end | ||
true | ||
end | ||
def operations | ||
@supported_methods.each.lazy.map { |m| Doc::Operation.new(self, @spec.navigate(m)) } | ||
end | ||
|
||
def methods | ||
@methods.each_value | ||
def path_regexp | ||
@path_regexp ||= begin | ||
re = /\{(\S+)\}/ | ||
@path.gsub(re) { |placeholder| | ||
placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" } | ||
}.then { |str| Regexp.new(str) } | ||
end | ||
end | ||
|
||
def static? | ||
!dynamic? | ||
end | ||
|
||
def with_method(method) | ||
@methods[method] | ||
def supports_method?(method) | ||
@supported_methods.include?(method) | ||
end | ||
|
||
private | ||
|
||
def parameter_matches?(name, value) | ||
parameter = Array.wrap(@schema['parameters']) | ||
.map.with_index { |_spec, index| @schema.navigate('parameters', index.to_s).follow_refs } | ||
.find { |s| s['name'] == name && s['in'] == 'path' } | ||
&.then { |s| Doc::Parameter.new(s) } | ||
return false unless parameter | ||
|
||
parameter.matches?(value) | ||
end | ||
|
||
def regexp_path | ||
re = /\{(\S+)\}/ | ||
@path.gsub(re) { |placeholder| | ||
placeholder.match(re) { |m| "(?<#{m[1]}>[^/]*)" } | ||
}.then { |str| Regexp.new(str) } | ||
end | ||
def with_method(method) | ||
return unless supports_method?(method) | ||
|
||
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 | ||
Doc::Operation.new(self, @spec.navigate(method)) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module OpenapiContracts | ||
class Doc | ||
module WithParameters | ||
def parameters | ||
@parameters ||= Array.wrap(@spec.navigate('parameters')&.each&.map { |s| Doc::Parameter.new(s) }) | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.