jaredhendrickson13 / pfsense-api

The missing REST API package for pfSense
https://pfrest.org/
Apache License 2.0
670 stars 101 forks source link

Dynamic Json in API Responses #24

Closed speatzle closed 2 months ago

speatzle commented 3 years ago

Hi, i have been testing this Plugin and found out that it returns JSON where a values type can change. For Example: if you Read an Alias from the API you'll notice that when there is only one address in the Alias it will return a string and if there are multiple addresses it will return an array. This behaviour makes using the API hard as you will always have to expect that it will return string or an array. Some Parsers parse JSON rigidly, Meaning you have to define all values and types before parsing. These parsers will then complain and refuse to parse if there is a type mismatch between the predefined Struct and the actual JSON. Because of this Issue id suggest changing the API so that it always returns an array even if there is only one element. This change should not break backwards compatibility as older clients had to support arrays anyway.

jaredhendrickson13 commented 3 years ago

Hey!

Thanks for the insight. This is an unfortunate side effect of pfSense using an XML based configuration. The XML parser pfSense uses will only parse values as string or array types. An array type is only returned when two or more identical XML tags exist under the same parent tag, otherwise it assumes it is a string. That is why the API will return a string when only one item exists.

For most endpoints, this problem can likely be remedied in the API framework by integrating a sort of type mapping to translate the types given by the XML parser to their expected type. I'll see if I can get something working, if there are no major complications I should be able to have a fix included in the next release.