Skip to content

Commit

Permalink
feat: remove status from individual error in problem+error response
Browse files Browse the repository at this point in the history
This was incorrectly added in the first place due to a SB spec that later got updated.

It is unnecessary because there is a status in the top level.
  • Loading branch information
bethesque committed Oct 10, 2023
1 parent ff98416 commit a4b3ec5
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 29 deletions.
2 changes: 1 addition & 1 deletion lib/pact_broker/api/decorators/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def validation_error_decorator_class_for(errors_class, accept_header)
if errors_class == Dry::Validation::MessageSet
PactBroker::Api::Decorators::DryValidationErrorsProblemJsonDecorator
else
PactBroker::Api::Decorators::ValidationErrorsProblemJSONDecorator
PactBroker::Api::Decorators::ValidationErrorsProblemJsonDecorator
end
else
if errors_class == Dry::Validation::MessageSet
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ class EmbeddedErrorProblemJsonDecorator < BaseDecorator
property :text, as: :detail
property :pointer, exec_context: :decorator
property :parameter, exec_context: :decorator
property :status, getter: -> (user_options:, **) { user_options[:status] || 400 }

# dry-validation doesn't support validating a top level array, so we wrap
# the json patch operations array in a hash with the key :_ops to validate it.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
module PactBroker
module Api
module Decorators
class ValidationErrorsProblemJSONDecorator
class ValidationErrorsProblemJsonDecorator
using PactBroker::StringRefinements


Expand Down Expand Up @@ -52,9 +52,8 @@ def walk_errors(object, list, path, base_url)
def append_error(list, message, path, base_url)
error = {
"type" => "#{base_url}/problems/invalid-body-property-value",
"title" => "Validation error",
"detail" => message,
"status" => 400
"title" => "Invalid body parameter",
"detail" => message
}
error["pointer"] = path.tr(".", "/") if path.present?
list << error
Expand Down
2 changes: 1 addition & 1 deletion spec/lib/pact_broker/api/decorators/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Decorators
let(:errors_class) { Hash }
let(:accept_header) { "application/hal+json, application/problem+json" }

it { is_expected.to be PactBroker::Api::Decorators::ValidationErrorsProblemJSONDecorator }
it { is_expected.to be PactBroker::Api::Decorators::ValidationErrorsProblemJsonDecorator }
end

context "when given Dry::Validation::MessageSet and application/hal+json" do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "pact_broker/api/decorators/dry_validation_errors_problem_json_decorator"
require "pact_broker/api/decorators/validation_errors_problem_json_decorator"
require "pact_broker/api/contracts/base_contract"

module PactBroker
Expand Down Expand Up @@ -35,8 +36,7 @@ class TestContract < PactBroker::Api::Contracts::BaseContract
"type" => "http://example.org/problems/invalid-body-property-value",
"pointer" => "/foo/bar",
"title" => "Invalid body parameter",
"detail" => "must be a string",
"status" => 400
"detail" => "must be a string"
}
]
}
Expand All @@ -45,6 +45,18 @@ class TestContract < PactBroker::Api::Contracts::BaseContract
it { is_expected.to match_pact(expected_hash, allow_unexpected_keys: false) }
end

context "it decorates the same way as the ValidationErrorsProblemJsonDecorator" do
let(:validation_errors) do
TestContract.new.call({ foo: { bar: 1 }}).errors
end

let(:decorated_hash) { ValidationErrorsProblemJsonDecorator.new(validation_errors.to_hash).to_hash(**decorator_options) }

it "decorates the dry validation errors the same way as we decorate a hash of errors" do
expect(subject).to eq decorated_hash
end
end

context "when the top level details are customised via user_options" do
let(:decorator_options) { { user_options: { title: "title", type: "type", detail: "detail", status: 409, instance: "/foo" } } }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
module PactBroker
module Api
module Decorators
describe ValidationErrorsProblemJSONDecorator do
describe ValidationErrorsProblemJsonDecorator do
describe "#to_json" do
let(:decorator_options) { { user_options: { base_url: "http://example.org" } } }

subject { ValidationErrorsProblemJSONDecorator.new(validation_errors).to_hash(**decorator_options) }
subject { ValidationErrorsProblemJsonDecorator.new(validation_errors).to_hash(**decorator_options) }

context "with a hash of errors" do
let(:validation_errors) do
Expand All @@ -27,9 +27,8 @@ module Decorators
{
"type" => "http://example.org/problems/invalid-body-property-value",
"pointer" => "/contract/content",
"title" => "Validation error",
"detail" => "this is some error text",
"status" => 400
"title" => "Invalid body parameter",
"detail" => "this is some error text"
}
]
}
Expand All @@ -56,9 +55,8 @@ module Decorators
{
"type" => "http://example.org/problems/invalid-body-property-value",
"pointer" => "/contract/content",
"title" => "Validation error",
"detail" => "this is some error text",
"status" => 400
"title" => "Invalid body parameter",
"detail" => "this is some error text"
}
]
}
Expand All @@ -85,9 +83,8 @@ module Decorators
{
"type" => "http://example.org/problems/invalid-body-property-value",
"pointer" => "/contract/content/1",
"title" => "Validation error",
"detail" => "this is some error text",
"status" => 400
"title" => "Invalid body parameter",
"detail" => "this is some error text"
}
]
}
Expand All @@ -110,15 +107,13 @@ module Decorators
"errors" => [
{
"type" => "http://example.org/problems/invalid-body-property-value",
"title" => "Validation error",
"detail" => "error 1",
"status" => 400
"title" => "Invalid body parameter",
"detail" => "error 1"
},
{
"type" => "http://example.org/problems/invalid-body-property-value",
"title" => "Validation error",
"detail" => "error 2",
"status" => 400
"title" => "Invalid body parameter",
"detail" => "error 2"
}
]
}
Expand All @@ -142,9 +137,8 @@ module Decorators
"errors" => [
{
"type" => "http://example.org/problems/invalid-body-property-value",
"title" => "Validation error",
"detail" => "error 1",
"status" => 400
"title" => "Invalid body parameter",
"detail" => "error 1"
}
]
}
Expand Down

0 comments on commit a4b3ec5

Please sign in to comment.