KATT multiplies floats by a factor of 10 or 100.

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

KATT multiplies floats by a factor of 10 or 100.

Bjarne Wichmann Bagge Petersen
Have this head scratcher where I apply a float to a KATT parameter but in the request that float have been inflated by a factor of 10 or 100. 

I am wondering whether I am missing something obvious about how to treat floats in katt/jsx/erlang? Or this is a KATT bug?

Got this minimal test case:

float_test(Config) ->
    File = filename:join([?config(data_dir, Config), "float_test.apib"]),
    Params = [ {test_float, 1.23}],
    katt:run(File, Params).

And the contents of float_test.apib:

> Accept: application/json
> Content-Type: application/json
> User-Agent: KATT
{
    "test": "{{<test_float}}",
    "reference": 1.23
}
< 200
< Content-Type: application/json
{}

And this is how the request looks using nc -l 9999

POST / HTTP/1.1
Accept: application/json
Content-Type: application/json
User-Agent: KATT
Host: localhost:9999
Content-Length: 43

{
    "test": 12.3,
    "reference": 1.23
}

As you can see "test_float" have been multiplied by a factor 10.


Best regards


Bjarne Wichmann Bagge Petersen

Backend Developer, ShopGun

A: Arne Jacobsens Allé 16, Field's, 3rd floor, DK-2300 København S, Denmark

_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions
Reply | Threaded
Open this post in threaded view
|

Re: KATT multiplies floats by a factor of 10 or 100.

Leonard Boyce-2
Hello Bjanre,

If I'm looking at the correct source for KATT
(https://github.com/for-GET/katt), it looks like it may be a bug in
katt_util:my_float_to_list/2

Quick test.

1> F = fun My_float_to_list(X, Decimals) ->
1>   Multiplier = trunc(math:pow(10, Decimals)),
1>   X1 = X * Multiplier,
1>   X2 = trunc(X1) + 0.0,
1>   case X1 =:= X2 of
1>     true ->
1>       String = integer_to_list(trunc(X1)),
1>       Number = [ string:sub_string(String, 1, Decimals)
1>                , string:sub_string(String, min( Decimals + 1
1>                                               , string:len(String) + 1
1>                                               ))
1>                ],
1>       string:join(Number, ".");
1>     false ->
1>       Decimals1 = Decimals + 1,
1>       My_float_to_list(X, Decimals1)
1>   end end.
#Fun<erl_eval.36.54118792>
2> F(1.23,0).
"12.3"

On Thu, Jan 18, 2018 at 2:51 AM, Bjarne Wichmann Bagge Petersen
<[hidden email]> wrote:

> Have this head scratcher where I apply a float to a KATT parameter but in
> the request that float have been inflated by a factor of 10 or 100.
>
> I am wondering whether I am missing something obvious about how to treat
> floats in katt/jsx/erlang? Or this is a KATT bug?
>
> Got this minimal test case:
>
> float_test(Config) ->
>     File = filename:join([?config(data_dir, Config), "float_test.apib"]),
>     Params = [ {test_float, 1.23}],
>     katt:run(File, Params).
>
> And the contents of float_test.apib:
>
> POST http://localhost:9999
>> Accept: application/json
>> Content-Type: application/json
>> User-Agent: KATT
> {
>     "test": "{{<test_float}}",
>     "reference": 1.23
> }
> < 200
> < Content-Type: application/json
> {}
>
> And this is how the request looks using nc -l 9999
>
> POST / HTTP/1.1
> Accept: application/json
> Content-Type: application/json
> User-Agent: KATT
> Host: localhost:9999
> Content-Length: 43
>
> {
>     "test": 12.3,
>     "reference": 1.23
> }
>
> As you can see "test_float" have been multiplied by a factor 10.
>
>
> Best regards
>
>
> Bjarne Wichmann Bagge Petersen
>
> Backend Developer, ShopGun
>
>
> E: [hidden email]
> L: dk.linkedin.com/in/bjarnewp
> W: shopgun.com
> A: Arne Jacobsens Allé 16, Field's, 3rd floor, DK-2300 København S, Denmark
>
> _______________________________________________
> erlang-questions mailing list
> [hidden email]
> http://erlang.org/mailman/listinfo/erlang-questions
>
_______________________________________________
erlang-questions mailing list
[hidden email]
http://erlang.org/mailman/listinfo/erlang-questions