/
json.cr
162 lines (150 loc) Β· 4.47 KB
/
json.cr
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
require "json"
require "../../error"
module Onyx::REST::Endpoint
# Define JSON params which would be deserialized from the request body only if
# its "Content-Type" header is "application/json". The serialization is powered by
# stdlib's [`JSON::Serializable`](https://crystal-lang.org/api/latest/JSON/Serializable.html).
#
# ## Options
#
# * `required` -- if set to `true`, will attempt to parse JSON params regardless
# of the `"Content-Type"` header and return a parameter error otherwise; the `params.json`
# getter becomes non-nilable
#
# ## Example
#
# ```
# struct UpdateUser
# include Onyx::REST::Action
#
# params do
# path do
# type id : Int32
# end
#
# json do
# type user do
# type email : String?
# type username : String?
# end
# end
# end
#
# def call
# if json = params.json
# pp! json.user.email
# pp! json.user.username
# end
# end
# end
# ```
#
# ```shell
# > curl -X POST -H "Content-Type: application/json" -d '{"user":{"email":"foo@example.com"}}' http://localhost:5000/users/1
# json.user.email => "foo@example.com"
# json.user.username => nil
# ```
#
# If your endpoint expects JSON params only, then it can be simplified a bit:
#
# ```
# struct UpdateUser
# include Onyx::REST::Action
#
# params do
# path do
# type id : Int32
# end
#
# json required: true do
# type user do
# type email : String?
# type username : String?
# end
# end
# end
#
# def call
# pp! params.json.user.email
# pp! params.json.user.username
# end
# end
# ```
#
# ```shell
# > curl -X POST -d '{"user":{"email":"foo@example.com"}}' http://localhost:5000/users/1
# ```
macro json(required = false, &block)
class JSONError < Onyx::REST::Error(PARAMS_ERROR_CODE)
end
struct JSON
include ::JSON::Serializable
{% verbatim do %}
macro type(argument, nilable = false, **options, &block)
{% if block %}
{% unless options.empty? %}
@[::JSON::Field({{**options}})]
{% end %}
{% 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}}{{" | Nil".id if nilable}}
{% elsif argument.is_a?(Call) %}
getter {{argument.name.underscore}} : {{argument.name.camelcase.id}}{{" | Nil".id if nilable}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
{% if argument.is_a?(Path) %}
struct {{argument.names.first.camelcase.id}}
{% elsif argument.is_a?(Call) %}
struct {{argument.name.camelcase.id}}
{% end %}
include ::JSON::Serializable
{% if block.body.is_a?(Expressions) %}
{% for expression in block.body.expressions %}
JSON.{{expression}}
{% end %}
{% elsif block.body.is_a?(Call) %}
JSON.{{yield.id}}
{% else %}
{% raise "BUG: Unhandled block body type #{block.body.class_name}" %}
{% end %}
end
{% elsif argument.is_a?(TypeDeclaration) %}
{% unless options.empty? %}
@[::JSON::Field({{**options}})]
{% end %}
getter {{argument}}
{% else %}
{% raise "BUG: Unhandled argument type #{argument.class_name}" %}
{% end %}
end
{% end %}
{{yield.id}}
end
{% if required %}
getter! json : JSON
{% else %}
getter json : JSON?
{% end %}
def initialize(request : HTTP::Request)
previous_def
{% begin %}
begin
{% unless required %}
if request.headers["Content-Type"]?.try &.=~ /^application\/json/
{% end %}
if body = request.body
@json = JSON.from_json(body.gets_to_end)
else
raise JSONError.new("Missing request body")
end
{% unless required %}
end
{% end %}
rescue ex : ::JSON::MappingError
raise JSONError.new(ex.message.not_nil!.lines.first)
end
{% end %}
end
end
end