http
http copied to clipboard
BUG: Unhandled argument type "Call"
I'm getting this error:
Error in macro 'params' /Users/remivillien/Git/Dwoom/dwoom-account-backend-signup/lib/onyx-http/src/onyx-http/endpoint/params.cr:12, line 19:
1. struct Params
2. # This method is used to copy request body if needed.
3. protected def copy_io(from : IO, limit = nil) : IO
4. to = IO::Memory.new
5.
6. if limit
7. IO.copy(from, to, limit)
8. else
9. IO.copy(from, to)
10. end
11.
12. return to.rewind
13. end
14.
15. def initialize(request : ::HTTP::Request)
16. end
17.
18.
> 19. Onyx::HTTP::Endpoint.json do
20. type(required(require: true) do
21. type(email : String)
22. type(username : String)
23. type(password : String)
24. type(first_name : String)
25. type(last_name : String)
26. type(dob : Array(Int32))
27. type(sexe : String)
28. type(country : String)
29. type(language : String)
30. type(accept_terms : Bool)
31. type(accept_news : Bool)
32. type(confirmation_email_id : String)
33. type(confirmation_email_confirmation_code : String)
34. end)
35. end
36.
37. end
38.
39. protected getter params : Params
40.
41. def initialize(@context : ::HTTP::Server::Context)
42.
43. super
44.
45.
46. @params = Params.new(@context.request)
47. end
48.
expanding macro
in macro 'json' /Users/remivillien/Git/Dwoom/dwoom-account-backend-signup/lib/onyx-http/src/onyx-http/endpoint/params/json.cr:80, line 59:
1. class JSONError < Onyx::HTTP::Error(400)
2. end
3.
4. struct JSON
5. include ::JSON::Serializable
6.
7.
8. macro type(argument, nilable = false, **options, &block)
9. {% if block %}
10. {% if options.empty? %}{% else %}
11. @[::JSON::Field({{ **options }})]
12. {% end %}
13.
14. {% if argument.is_a?(Path) %}
15. {% if argument.names.size > 1
16. raise("Cannot declare namespaced nested query parameter")
17. end %}
18.
19. getter {{ argument.names.first.underscore }} : {{ argument.names.first.camelcase.id }}{{ if nilable
20. " | Nil".id
21. end }}
22. {% else %}{% if argument.is_a?(Call) %}
23. getter {{ argument.name.underscore }} : {{ argument.name.camelcase.id }}{{ if nilable
24. " | Nil".id
25. end }}
26. {% else %}
27. {% raise("BUG: Unhandled argument type #{argument.class_name}") %}
28. {% end %}{% end %}
29.
30. {% if argument.is_a?(Path) %}
31. struct {{ argument.names.first.camelcase.id }}
32. {% else %}{% if argument.is_a?(Call) %}
33. struct {{ argument.name.camelcase.id }}
34. {% end %}{% end %}
35. include ::JSON::Serializable
36.
37. {% if block.body.is_a?(Expressions) %}
38. {% for expression in block.body.expressions %}
39. JSON.{{ expression }}
40. {% end %}
41. {% else %}{% if block.body.is_a?(Call) %}
42. JSON.{{ (yield).id }}
43. {% else %}
44. {% raise("BUG: Unhandled block body type #{block.body.class_name}") %}
45. {% end %}{% end %}
46. end
47. {% else %}{% if argument.is_a?(TypeDeclaration) %}
48. {% if options.empty? %}{% else %}
49. @[::JSON::Field({{ **options }})]
50. {% end %}
51.
52. getter {{ argument }}
53. {% else %}
54. {% raise("BUG: Unhandled argument type #{argument.class_name}") %}
55. {% end %}{% end %}
56. end
57.
58.
> 59. type(required(require: true) do
60. type(email : String)
61. type(username : String)
62. type(password : String)
63. type(first_name : String)
64. type(last_name : String)
65. type(dob : Array(Int32))
66. type(sexe : String)
67. type(country : String)
68. type(language : String)
69. type(accept_terms : Bool)
70. type(accept_news : Bool)
71. type(confirmation_email_id : String)
72. type(confirmation_email_confirmation_code : String)
73. end)
74. end
75.
76.
77. getter json : JSON?
78.
79.
80. def initialize(request : HTTP::Request)
81. previous_def
82.
83.
84. begin
85.
86. if request.headers["Content-Type"]?.try &.=~ /^application\/json/
87.
88. if body = request.body
89.
90.
91. @json = JSON.from_json(body.gets_to_end)
92.
93.
94. else
95. raise JSONError.new("Missing request body")
96. end
97.
98. end
99.
100. rescue ex : ::JSON::MappingError
101. raise JSONError.new(ex.message.not_nil!.lines.first)
102. end
103.
104. end
105.
expanding macro
in macro 'params' /Users/remivillien/Git/Dwoom/dwoom-account-backend-signup/lib/onyx-http/src/onyx-http/endpoint/params.cr:12, line 19:
1. struct Params
2. # This method is used to copy request body if needed.
3. protected def copy_io(from : IO, limit = nil) : IO
4. to = IO::Memory.new
5.
6. if limit
7. IO.copy(from, to, limit)
8. else
9. IO.copy(from, to)
10. end
11.
12. return to.rewind
13. end
14.
15. def initialize(request : ::HTTP::Request)
16. end
17.
18.
> 19. Onyx::HTTP::Endpoint.json do
20. type(required(require: true) do
21. type(email : String)
22. type(username : String)
23. type(password : String)
24. type(first_name : String)
25. type(last_name : String)
26. type(dob : Array(Int32))
27. type(sexe : String)
28. type(country : String)
29. type(language : String)
30. type(accept_terms : Bool)
31. type(accept_news : Bool)
32. type(confirmation_email_id : String)
33. type(confirmation_email_confirmation_code : String)
34. end)
35. end
36.
37. end
38.
39. protected getter params : Params
40.
41. def initialize(@context : ::HTTP::Server::Context)
42.
43. super
44.
45.
46. @params = Params.new(@context.request)
47. end
48.
BUG: Unhandled argument type "Call"
It's apparently related to this, not sure what is going on. Did I do something wrong?
struct Signup
include Onyx::HTTP::Endpoint
params do
json do
type required require: true do
type email : String
type username : String
type password : String
type first_name : String
type last_name : String
type dob : Array(Int32)
type sexe : String
type country : String
type language : String
type accept_terms : Bool
type accept_news : Bool
type confirmation_email_id : String
type confirmation_email_confirmation_code : String
end
type optional do
type phone_number : String
type confirmation_phone_id : String
type confirmation_phone_confirmation_code : String
end
end
end
def call
.......
end
end
So if I do it like that I don't have this error anymore
params do
json require: true do
type email : String
type username : String
type password : String
type first_name : String
type last_name : String
type dob : Array(Int32)
type sexe : String
type country : String
type language : String
type accept_terms : Bool
type accept_news : Bool
type confirmation_email_id : String
type confirmation_email_confirmation_code : String
end
end
params do
json do
type phone_number : String
type confirmation_phone_id : String
type confirmation_phone_confirmation_code : String
end
end
But now, the compiler is telling me that the required field can be nil at compile time. I didn't have this issue previously when all the params were required.
Could you try
type required, require: true do
instead of
type required require: true do
?
It solves the first issue, but I'm still getting
(compile-time type is (Params::JSON | Nil))
for required params