diff --git a/lib/pact_broker/api/decorators/configuration.rb b/lib/pact_broker/api/decorators/configuration.rb index 61c724f3c..50db5de38 100644 --- a/lib/pact_broker/api/decorators/configuration.rb +++ b/lib/pact_broker/api/decorators/configuration.rb @@ -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 diff --git a/lib/pact_broker/api/decorators/embedded_error_problem_json_decorator.rb b/lib/pact_broker/api/decorators/embedded_error_problem_json_decorator.rb index 64bd59d7c..dced71605 100644 --- a/lib/pact_broker/api/decorators/embedded_error_problem_json_decorator.rb +++ b/lib/pact_broker/api/decorators/embedded_error_problem_json_decorator.rb @@ -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. diff --git a/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator.rb b/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator.rb index e3c96ed29..05d3a3af1 100644 --- a/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator.rb +++ b/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator.rb @@ -4,7 +4,7 @@ module PactBroker module Api module Decorators - class ValidationErrorsProblemJSONDecorator + class ValidationErrorsProblemJsonDecorator using PactBroker::StringRefinements @@ -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 diff --git a/spec/lib/pact_broker/api/decorators/configuration_spec.rb b/spec/lib/pact_broker/api/decorators/configuration_spec.rb index 3aac332c4..9a36eed83 100644 --- a/spec/lib/pact_broker/api/decorators/configuration_spec.rb +++ b/spec/lib/pact_broker/api/decorators/configuration_spec.rb @@ -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 diff --git a/spec/lib/pact_broker/api/decorators/dry_validation_errors_problem_json_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/dry_validation_errors_problem_json_decorator_spec.rb index 44031c3ec..011261943 100644 --- a/spec/lib/pact_broker/api/decorators/dry_validation_errors_problem_json_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/dry_validation_errors_problem_json_decorator_spec.rb @@ -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 @@ -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" } ] } @@ -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" } } } diff --git a/spec/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator_spec.rb b/spec/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator_spec.rb index 9f36a0e82..063122fb2 100644 --- a/spec/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator_spec.rb +++ b/spec/lib/pact_broker/api/decorators/validation_errors_problem_json_decorator_spec.rb @@ -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 @@ -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" } ] } @@ -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" } ] } @@ -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" } ] } @@ -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" } ] } @@ -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" } ] }