Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes and addition in InputMessegeContent and Parser modules #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

anronin
Copy link
Contributor

@anronin anronin commented May 30, 2016

  1. Bug fix in Nadia.Model.InputMessegeContent module where optional fields must be string instead of nil

  2. Fixes and additions in Nadia.Parser after the new models were introduced

@zhyu
Copy link
Owner

zhyu commented May 31, 2016

What's the problem if optional fields of Nadia.Model.InputMessegeContent are nil?

@anronin
Copy link
Contributor Author

anronin commented May 31, 2016

If its nil and not used then you get error message like that:
"Bad request: Field "last_name" must be of type String" from telegram server

@anronin
Copy link
Contributor Author

anronin commented May 31, 2016

this is response from server when it's nil

{:ok, %HTTPoison.Response{body: "{\"ok\":false,\"error_code\":400,\"description\":\"Bad request: Field \\\"last_name\\\" must be of type String\"}", headers: [{"Server", "nginx/1.10.0"}, {"Date", "Tue, 31 May 2016 07:07:08 GMT"}, {"Content-Type", "application/json"}, {"Content-Length", "101"}, {"Connection", "keep-alive"}, {"Access-Control-Allow-Origin", "*"}, {"Access-Control-Expose-Headers", "Content-Length,Content-Type,Date,Server,Connection"}], status_code: 400}}

and params are like that
[inline_query_id: "13947328571332811", results: "[{\"type\":\"article\",\"title\":\"Test\",\"input_message_content\":{\"phone_number\":\"19232114412\",\"last_name\":null,\"first_name\":\"Arbuz\"},\"id\":\"1\"},{\"type\":\"article\",\"title\":\"Test2\",\"input_message_content\":{\"phone_number\":\"19232114412\",\"last_name\":null,\"first_name\":\"Dyna\"},\"id\":\"2\"}]"]

@zhyu
Copy link
Owner

zhyu commented May 31, 2016

Well, instead of using empty string for optional field, I prefer to remove nil fields in request params.

@anronin
Copy link
Contributor Author

anronin commented May 31, 2016

I do not quite understand where to do it: in Nadia.answer_inline_query or Nadia.API.build_request ?

@zhyu
Copy link
Owner

zhyu commented May 31, 2016

I think this issue relates to this line: https://github.com/zhyu/nadia/blob/master/lib/nadia.ex#L635

Only nil field in the top level has been removed, a nil field in a non-nil field will not been removed.
e.g.,

%{nil_field: nil, non_nil_field: %{nil_field: nil, non_nil_field: "something"}}

will be changed to

%{non_nil_field: %{nil_field: nil, non_nil_field: "something"}}

if we change it to

%{non_nil_field: %{non_nil_field: "something"}}

there won't be any problem.

So I think removing nil fields recursively will be a solution.
And since nil fields are very common, I think it's better to do it in Nadia.API.build_request.

@anronin
Copy link
Contributor Author

anronin commented May 31, 2016

not sure that is right solution)
EDIT: just a moment, this is not right solution
EDIT2: still not sure that is right solution, but at least it works. i'm think so

@anronin
Copy link
Contributor Author

anronin commented Jun 2, 2016

wdyt?

lib/nadia/api.ex Outdated
if !is_nil(file_field) and File.exists?(params[file_field]) do
build_multipart_request(params, file_field)
else
{:form, params}
end
end

defp drop_nil_fields(value) when is_list(value) do
value
|> Enum.map(fn value ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not just use a map?

defp drop_nil_fields(value) when is_list(value), do: Enum.map(value, drop_nil_fields)

@anronin
Copy link
Contributor Author

anronin commented Jun 2, 2016

The result should look something like this?

defp drop_nil_fields(params) when is_list(params), do: Enum.map(params, &drop_nil_fields/1)
defp drop_nil_fields(params) when is_map(params) do
    params
    |> Map.from_struct
    |> Enum.filter(fn {_, v} -> v != nil end)
    |> Enum.into(%{})
    |> Poison.encode!
  end
defp drop_nil_fields(params), do: to_string(params)

EDIT: no, this is not working

@zhyu
Copy link
Owner

zhyu commented Jun 2, 2016

in drop_nil_fields for a map,
you should call drop_nil_fields on values of the map, not just return the non-nil value.

drop_nil_fields should remove any nil fields recursively,
it means you should call recursively on every item of a list, and every value of a map.

EDIT:
so just add |> Enum.map(&drop_nil_fields/1) after this line: |> Enum.filter(fn {_, v} -> v != nil end),
or you can use filter_map

@anronin
Copy link
Contributor Author

anronin commented Jun 2, 2016

so like this? test go fine, but i have a problem with this

defp drop_nil_fields(params) when is_list(params), do: Enum.map(params, &drop_nil_fields/1)
defp drop_nil_fields(params) when is_map(params) do
    params
    |> Map.from_struct
    |> Enum.filter_map(fn {_, v} -> v end, fn {k, v} -> {k, drop_nil_fields(v)} end)
    |> Enum.into(%{})
    |> Poison.encode!
  end
defp drop_nil_fields(params), do: to_string(params)

@anronin
Copy link
Contributor Author

anronin commented Jun 2, 2016

if i get it right |> Poison.encode! must be in drop_nil_fields for list

@anronin anronin force-pushed the master branch 2 times, most recently from 07b5648 to a621996 Compare June 2, 2016 12:40
@anronin
Copy link
Contributor Author

anronin commented Jun 2, 2016

how about that?

@anronin
Copy link
Contributor Author

anronin commented Jun 3, 2016

only problem is that reply_markup: nil not removed

@anronin
Copy link
Contributor Author

anronin commented Jun 3, 2016

i could add |> Keyword.delete(:reply_markup, nil) after |> Keyword.update(:reply_markup, nil, &(Poison.encode!(&1))) in build_request of course

@anronin
Copy link
Contributor Author

anronin commented Jun 9, 2016

any thoughts?

@zhyu
Copy link
Owner

zhyu commented Mar 5, 2018

Sorry for keeping you waiting 😿
However, this p-r is outdated. I may check and merge it when conflicts are resolved.

@anronin
Copy link
Contributor Author

anronin commented Mar 5, 2018

np, i'm glad you are back :) will look at this asap

@zhyu
Copy link
Owner

zhyu commented Mar 6, 2018

Thanks for your reply, please take your time 😺
After resolving conflicts, please use the formatter to format your code too, you can find details in #60.
I'll update .travis.yml to check it 🔜

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants