Skip to content

Commit

Permalink
feat: allow nilable nested params
Browse files Browse the repository at this point in the history
Closes #55
  • Loading branch information
vladfaust committed Feb 26, 2019
1 parent c418990 commit fd2763c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
24 changes: 22 additions & 2 deletions spec/onyx-rest/action_spec.cr
Expand Up @@ -57,15 +57,15 @@ class JSONAction

params do
json require: true, any_content_type: true do
type user do
type user, nilable: true do
type name : String
type email : String
end
end
end

def call
context.response << "#{params.json.user.name}, #{params.json.user.email}"
context.response << "#{params.json.user.try &.name}, #{params.json.user.try &.email}"
end
end

Expand Down Expand Up @@ -120,6 +120,26 @@ describe Onyx::REST::Action do
end

context "JSON action" do
context "with empty JSON" do
it do
response = client.post("/json",
body: "{}"
)
response.status_code.should eq 200
response.body.should eq ", "
end
end

context "with empty user" do
it do
response = client.post("/json",
body: %Q[{"user": null}]
)
response.status_code.should eq 200
response.body.should eq ", "
end
end

it do
response = client.post("/json",
body: %Q[{"user": {"name": "John", "email": "foo@example.com"}}]
Expand Down
6 changes: 3 additions & 3 deletions src/onyx-rest/endpoint/params/form.cr
Expand Up @@ -93,7 +93,7 @@ module Onyx::REST::Endpoint
include ::HTTP::Params::Serializable

{% verbatim do %}
macro type(argument, **options, &block)
macro type(argument, nilable = false, **options, &block)
{% if block %}
{% unless options.empty? %}
@[::HTTP::Param({{**options}})]
Expand All @@ -102,9 +102,9 @@ module Onyx::REST::Endpoint
{% if argument.is_a?(Path) %}
{% raise "Cannot declare namespaced nested query parameter" if argument.names.size > 1 %}

getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}
getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}{{" | Nil".id if nilable}}
{% elsif argument.is_a?(Call) %}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}{{" | Nil".id if nilable}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
Expand Down
6 changes: 3 additions & 3 deletions src/onyx-rest/endpoint/params/json.cr
Expand Up @@ -87,7 +87,7 @@ module Onyx::REST::Endpoint
include JSON::Serializable

{% verbatim do %}
macro type(argument, **options, &block)
macro type(argument, nilable = false, **options, &block)
{% if block %}
{% unless options.empty? %}
@[JSON::Field({{**options}})]
Expand All @@ -96,9 +96,9 @@ module Onyx::REST::Endpoint
{% if argument.is_a?(Path) %}
{% raise "Cannot declare namespaced nested query parameter" if argument.names.size > 1 %}

getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}
getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}{{" | Nil".id if nilable}}
{% elsif argument.is_a?(Call) %}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}{{" | Nil".id if nilable}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
Expand Down
6 changes: 3 additions & 3 deletions src/onyx-rest/endpoint/params/query.cr
Expand Up @@ -42,7 +42,7 @@ module Onyx::REST::Endpoint
include ::HTTP::Params::Serializable

{% verbatim do %}
macro type(argument, **options, &block)
macro type(argument, nilable = false, **options, &block)
{% if block %}
{% unless options.empty? %}
@[::HTTP::Param({{**options}})]
Expand All @@ -51,9 +51,9 @@ module Onyx::REST::Endpoint
{% if argument.is_a?(Path) %}
{% raise "Cannot declare namespaced nested query parameter" if argument.names.size > 1 %}

getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}
getter {{argument.names.first.underscore}} : {{argument.names.first.camelcase.id}}{{" | Nil".id if nilable}}
{% elsif argument.is_a?(Call) %}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}{{" | Nil".id if nilable}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
Expand Down

0 comments on commit fd2763c

Please sign in to comment.