-
-
Notifications
You must be signed in to change notification settings - Fork 178
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(pacts for verification): support querying by POST
- Loading branch information
Showing
15 changed files
with
444 additions
and
73 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
lib/pact_broker/api/contracts/dry_validation_workarounds.rb
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,38 @@ | ||
module PactBroker | ||
module Api | ||
module Contracts | ||
module DryValidationWorkarounds | ||
extend self | ||
|
||
# I just cannot seem to get the validation to stop on the first error. | ||
# If one rule fails, they all come back failed, and it's driving me nuts. | ||
# Why on earth would I want that behaviour? | ||
def select_first_message(messages) | ||
messages.each_with_object({}) do | (key, value), new_messages | | ||
new_messages[key] = [value.first] | ||
end | ||
end | ||
|
||
def flatten_array_of_hashes(array_of_hashes) | ||
new_messages = array_of_hashes.collect do | index, hash | | ||
hash.values.flatten.collect { | text | "#{text} at index #{index}"} | ||
end.flatten | ||
end | ||
|
||
def flatten_indexed_messages(messages) | ||
if messages.values.any?{ | value | is_indexed_structure?(value) } | ||
messages.each_with_object({}) do | (key, value), new_messages | | ||
new_messages[key] = is_indexed_structure?(value) ? flatten_array_of_hashes(value) : value | ||
end | ||
else | ||
messages | ||
end | ||
end | ||
|
||
def is_indexed_structure?(thing) | ||
thing.is_a?(Hash) && thing.keys.first.is_a?(Integer) | ||
end | ||
end | ||
end | ||
end | ||
end |
28 changes: 28 additions & 0 deletions
28
lib/pact_broker/api/contracts/verifiable_pacts_json_query_schema.rb
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,28 @@ | ||
require 'dry-validation' | ||
require 'pact_broker/hash_refinements' | ||
require 'pact_broker/api/contracts/dry_validation_workarounds' | ||
|
||
module PactBroker | ||
module Api | ||
module Contracts | ||
class VerifiablePactsJSONQuerySchema | ||
extend DryValidationWorkarounds | ||
using PactBroker::HashRefinements | ||
|
||
SCHEMA = Dry::Validation.Schema do | ||
optional(:providerVersionTags).maybe(:array?) | ||
optional(:consumerVersionSelectors).each do | ||
schema do | ||
required(:tag).filled(:str?) | ||
optional(:latest).filled(included_in?: [true, false]) | ||
end | ||
end | ||
end | ||
|
||
def self.call(params) | ||
select_first_message(flatten_indexed_messages(SCHEMA.call(params&.symbolize_keys).messages(full: true))) | ||
end | ||
end | ||
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
22 changes: 12 additions & 10 deletions
22
lib/pact_broker/api/decorators/verifiable_pacts_query_decorator.rb
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 |
---|---|---|
@@ -1,13 +1,61 @@ | ||
require 'pact_broker/string_refinements' | ||
|
||
module PactBroker | ||
module HashRefinements | ||
|
||
refine Hash do | ||
using PactBroker::StringRefinements | ||
|
||
def deep_merge(other_hash, &block) | ||
block_actual = Proc.new {|key, oldval, newval| | ||
newval = block.call(key, oldval, newval) if block_given? | ||
[oldval, newval].all? {|v| v.is_a?(Hash)} ? oldval.merge(newval, &block_actual) : newval | ||
} | ||
merge(other_hash, &block_actual) | ||
end | ||
|
||
def symbolize_keys | ||
symbolize_keys_private(self) | ||
end | ||
|
||
def snakecase_keys | ||
snakecase_keys_private(self) | ||
end | ||
|
||
private | ||
|
||
def snakecase_keys_private(params) | ||
case params | ||
when Hash | ||
params.inject({}) do |result, (key, value)| | ||
snake_key = case key | ||
when String then key.snakecase | ||
when Symbol then key.to_s.snakecase.to_sym | ||
else | ||
key | ||
end | ||
result.merge(snake_key => snakecase_keys_private(value)) | ||
end | ||
when Array | ||
params.collect { |value| snakecase_keys_private(value) } | ||
else | ||
params | ||
end | ||
|
||
end | ||
|
||
def symbolize_keys_private(params) | ||
case params | ||
when Hash | ||
params.inject({}) do |result, (key, value)| | ||
result.merge(key.to_sym => symbolize_keys_private(value)) | ||
end | ||
when Array | ||
params.collect { |value| symbolize_keys_private(value) } | ||
else | ||
params | ||
end | ||
end | ||
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
Oops, something went wrong.