Skip to content

Commit

Permalink
Param refactor (#426)
Browse files Browse the repository at this point in the history
* Remove `ATHA::QueryParam` and `ATHA::RequestParam`
* Remove `ATH::Exception::InvalidParamete`
* Remove everything in `ATH::Params` namespace, including `ATH::Action#params`
* Add `ATH::Exception.from_status`
  • Loading branch information
Blacksmoke16 authored Aug 23, 2024
1 parent b09a847 commit 339336e
Show file tree
Hide file tree
Showing 33 changed files with 233 additions and 1,511 deletions.
15 changes: 15 additions & 0 deletions src/components/framework/spec/athena_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ private class MockHandler
end
end

private enum Color
Red
Green
Blue
end

describe Athena::Framework do
describe ".from_parameter" do
describe Number do
Expand Down Expand Up @@ -53,6 +59,10 @@ describe Athena::Framework do
end
end

it Enum do
Color.from_parameter("green").should eq Color::Green
end

it Object do
str = "foo"

Expand Down Expand Up @@ -121,6 +131,11 @@ describe Athena::Framework do
end
end

it Enum do
Color.from_parameter?("green").should eq Color::Green
Color.from_parameter?("black").should be_nil
end

it Object do
str = "foo"

Expand Down
108 changes: 0 additions & 108 deletions src/components/framework/spec/compiler_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -391,113 +391,5 @@ describe Athena::Framework do
CODE
end
end

describe ATHA::QueryParam do
it "missing name" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation but is missing the parameter's name. It was not provided as the first positional argument nor via the 'name' field.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::QueryParam]
def action(all : Bool) : Int32
123
end
end
CODE
end

it "missing corresponding action parameter" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation but does not have a corresponding action parameter for 'foo'.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::QueryParam("foo")]
def action(active : Bool) : Bool
active
end
end
CODE
end

it "disallows non nilable non strict and no default params" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation with `strict: false` but the related action parameter is not nilable nor has a default value.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get("/")]
@[ATHA::QueryParam("page", strict: false)]
def action(page : Int32) : Int32
page
end
end
CODE
end

describe "requirements" do
describe "only allows `Assert` annotations as requirements (other than regex)" do
it "with a single annotation" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation whose 'requirements' value is invalid. Expected `Assert` annotation, got '@[ARTA::Get]'.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::QueryParam("all", requirements: @[ARTA::Get])]
def action(all : Bool) : Int32
123
end
end
CODE
end

describe "in an array" do
it "with a scalar value" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation whose 'requirements' array contains an invalid value. Expected `Assert` annotation, got '1' at index 1.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::QueryParam("all", requirements: [@[Assert::NotBlank], 1])]
def action(all : Bool) : Int32
123
end
end
CODE
end

it "with an annotation value" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation whose 'requirements' array contains an invalid value. Expected `Assert` annotation, got '@[ARTA::Get]' at index 1.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::QueryParam("all", requirements: [@[Assert::NotBlank], @[ARTA::Get]])]
def action(all : Bool) : Int32
123
end
end
CODE
end
end

it "with a scalar requirements value" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::QueryParam annotation with an invalid 'requirements' type: 'StringLiteral'. Only Regex, NamedTuple, or Array values are supported.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::QueryParam("all", requirements: "foo")]
def action(all : Bool) : Int32
123
end
end
CODE
end
end
end
end

# This is essentially the same as `ATHA::QueryParam`.
# Just do a simple check to ensure its working as expected while doing most other assertions with `ATHA::QueryParam`.
describe ATHA::RequestParam do
it "missing name" do
assert_error "Route action 'CompileController#action' has an Athena::Framework::Annotations::RequestParam annotation but is missing the parameter's name. It was not provided as the first positional argument nor via the 'name' field.", <<-CODE
class CompileController < ATH::Controller
@[ARTA::Get(path: "/")]
@[ATHA::RequestParam]
def action(all : Bool) : Int32
123
end
end
CODE
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
require "../../spec_helper"

private def parameter(
klass : T.class = String,
*,
name : String? = nil,
validation_failed_status : HTTP::Status = :not_found,
default : T? = nil
) forall T
ATH::Controller::ParameterMetadata(T).new(
"foo",
default_value: default,
has_default: !default.nil?,
annotation_configurations: ADI::AnnotationConfigurations.new({
ATHA::MapQueryParameter => [
ATHA::MapQueryParameterConfiguration.new(name, validation_failed_status),
] of ADI::AnnotationConfigurations::ConfigurationBase,
} of ADI::AnnotationConfigurations::Classes => Array(ADI::AnnotationConfigurations::ConfigurationBase))
)
end

describe ATHR::QueryParameter do
describe "#resolve" do
it "does not have the annotation" do
parameter = ATH::Controller::ParameterMetadata(String).new "foo"
ATHR::QueryParameter.new.resolve(new_request, parameter).should be_nil
end

it "valid scalar parameter" do
ATHR::QueryParameter.new.resolve(new_request(query: "foo=bar"), parameter).should eq "bar"
end

it "custom param name" do
ATHR::QueryParameter.new.resolve(new_request(query: "blah=bar"), parameter name: "blah").should eq "bar"
end

it "valid array parameter" do
ATHR::QueryParameter.new.resolve(new_request(query: "foo=1&foo=2"), parameter(Array(Int32))).should eq [1, 2]
end

it "missing nilable" do
ATHR::QueryParameter.new.resolve(new_request, parameter(Float64?)).should be_nil
end

it "non-nilable with default" do
ATHR::QueryParameter.new.resolve(new_request, parameter(Bool, default: false)).should be_nil
end

it "missing non-nilable no default" do
expect_raises ATH::Exception::NotFound, "Missing query parameter: 'foo'." do
ATHR::QueryParameter.new.resolve new_request, parameter
end
end

it "missing non-nilable no default custom status" do
expect_raises ATH::Exception::UnprocessableEntity, "Missing query parameter: 'foo'." do
ATHR::QueryParameter.new.resolve new_request, parameter(validation_failed_status: :unprocessable_entity)
end
end

it "invalid" do
expect_raises ATH::Exception::NotFound, "Invalid query parameter: 'foo'." do
ATHR::QueryParameter.new.resolve new_request(query: "foo=bar"), parameter(Int32)
end
end

it "missing non-nilable no default custom status" do
expect_raises ATH::Exception::UnprocessableEntity, "Invalid query parameter: 'foo'." do
ATHR::QueryParameter.new.resolve new_request(query: "foo=bar"), parameter(Int32, validation_failed_status: :unprocessable_entity)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ describe ATHR::UUID do
ATHR::UUID.new.resolve(new_request, ATH::Controller::ParameterMetadata(Int32?).new "foo").should be_nil
ATHR::UUID.new.resolve(new_request, ATH::Controller::ParameterMetadata(Bool | String).new "foo").should be_nil
end

it "attribute exists but is not a string" do
parameter = ATH::Controller::ParameterMetadata(UUID).new "foo"
request = new_request
Expand Down

This file was deleted.

26 changes: 0 additions & 26 deletions src/components/framework/spec/exceptions/invalid_parameter_spec.cr

This file was deleted.

50 changes: 0 additions & 50 deletions src/components/framework/spec/listeners/param_fetcher_spec.cr

This file was deleted.

Loading

0 comments on commit 339336e

Please sign in to comment.