minetest / serverlist

The global Minetest server list server
GNU Lesser General Public License v2.1
51 stars 28 forks source link

Compatibility fix (string field conversion) #23

Closed nOOb3167 closed 6 years ago

nOOb3167 commented 6 years ago

This PR addresses this related forum thread .

In lua all numbers are floating point. An API serializing lua data to JSON does not have sufficient clues to know when an integer (or floating point respectively), field is wanted.

The master-server code places type constraints on received JSON fields. The master-server code already has a compatibility concept, where a string is accepted, and converted, for the int-valued clients_max field. This PR extends the compatibility to all int-valued fields.

A client application of master-server, such as a minetest lua mod using minetest.request_http_api, that has difficulties securing the correct serialization for int-valued fields, becomes able to workaround by sending such fields as string.

JSON snippets of port field value for an announce request: { "action":"start","port":3000,... } - works { "action":"start","port":3000.0,... } - fails (type does not match) { "action":"start","port":"3000",... } - previously fails but now works

sfan5 commented 6 years ago

e49da8f1b9e00af1cf467b8eeeb34b819bc6aeaf

sfan5 commented 6 years ago

does not have sufficient clues to know when an integer field is wanted

Lua's print() handles this by printing all numbers with an "empty" float part as integers, e.g. 2.0 -> 2 but 1.2 -> 1.2

nOOb3167 commented 6 years ago

Lua's print() handles this by printing all numbers with an "empty" float part as integers, e.g. 2.0 -> 2 but 1.2 -> 1.2

Nice catch. I looked into alternate possibilities a bunch: lua 5.3 introduces proper integer types into the language itself, but there are incompatibilities . The funniest is:

The conversion of a float to a string now adds a .0 suffix to the result if it looks like an integer. (For instance, the float 2.0 will be printed as 2.0, not as 2.)

So this conversion behaviour keeps getting changed between versions. On minetest end, it is hard to write a code that will be compatible if lua is upgraded to a newer version (for example due to security).

sfan5 commented 6 years ago

Minetest won't be upgrading Lua to either 5.2 or 5.3 due to 1) incompatiblities within versions we'd have to deal with 2) LuaJIT missing support for those versions