API Error with PHP 8.1
-
Just upgraded to Ubuntu 22.04 with PHP 8.1 (from 20.04 with PHP 7.4), also updated FOG from 1.5.9 to 1.5.10 - so I’m not sure quite where this error lies, it might be my code.
I have an application that uses the FOG API. I haven’t changed anything in this and it has always worked before the upgrade.
Specifically, when I use the ‘edit’ path to assign an image to a host:
PUT request URL host/[host ID]/edit JSON ["imageID": int]
I get the following in the PHP error log:
[proxy_fcgi:error] [pid 1166] [client 127.0.0.1:42750] AH01071: Got error ' PHP message: PHP Warning: Undefined property: stdClass::$name in /var/www/fog/lib/router/route.class.php on line 623 PHP message: PHP Warning: Undefined property: stdClass::$name in /var/www/fog/lib/router/route.class.php on line 626'
Looking quickly at the code it seems to be something to do with not having a ‘name’ parameter in the JSON, but Ive never needed that before and I’ve no idea what I would set it to. The request does seem to work, but clearly something is wrong somewhere.
-
If I set a ‘name’ field to an empty string it deletes the hostname for the host.
If I set a ‘name’ field to the hostname of the host, the check it’s trying to do matches and gives me an ‘Already created’ error.
So it looks like there is no parameter I can set ‘name’ to that doesn’t cause me a problem. I might be misunderstanding the code, but having a check which fails if the host already exists on an endpoint called ‘edit’ seems backwards? -
@MarkG The issue is if you change the name to another host that already exists:
SO let’s just use the example case here:
Say you have host named abc with id 1
Say you have a host named def with id 2You are trying to edit id 2:
You decide to change the hostname to abc… Which thing should we do, delete the existing abc host, delete the existing host? Instead we error out.
That said, I believe I see and have pushed what I think will fix this issue for you.
Please repull dev-branch, and you shouldn’t be required to have the name value in the json payload.
The other piece, while it was a warning, it definitely wasn’t an issue.
Now when you add the name parameter, if you set as nothing it should fail, so I’m not sure if there is a host in yoru database with that name?
If so, please let me know. I suspect you now have 2 hosts in your host table, one with an empty name and one with the original name?
SELECT * from hosts where host in ('', '<original_hostname_here>');
From a MYSQL prompt. I will likely want to ensure a name field is properly set in the router code
-
@Tom-Elliott Router code should now expect a name item for the edit param if you’re passing in a name property as part of the json payload.
Basically
‘name’ = ‘’ will fail
A payload completely missing name should work just like it did before without the warning messages.
-
@Tom-Elliott said in API Error with PHP 8.1:
You decide to change the hostname to abc… Which thing should we do, delete the existing abc host, delete the existing host? Instead we error out.
Oh that’s a very good point, I had not considered that.
The issue I’m seeing is essentially that it fails when I’m trying to set ‘name’ to the value it already has, so if I have to send ‘name’ I have to change it, which isn’t useful. (eg I try to edit host id 1 and include a name of abc, that fails).
Happy to try pulling dev branch but it’ll take a few days, I’m running the release so I need to switch a few things around.
When I set name to “”, it did set the hostname to “”. I immediately corrected it in the database so I’m afraid I can’t provide the output you’re looking for.
-
@Tom-Elliott The change on dev-branch has fixed one of the warnings but not both. I’m still getting
[proxy_fcgi:error] [pid 5049] [client 127.0.0.1:59748] AH01071: Got error ' PHP message: PHP Warning: Undefined property: stdClass::$name in /var/www/fog/lib/router/route.class.php on line 629'
Swapping lines 629 and 630 fixed that warning for me. Thanks.
-
@MarkG Can you describe more?
Here’s what I see in dev-branch:
$var_name = false; if (property_exists($vars, 'name')) { $exists = self::getClass($classname) ->getManager() ->exists($vars->name); $var_name = strtolower($vars->name); if (!$var_name) { self::setErrorMessage( _('A name must be defined if using the "name" property'), HTTPResponseCodes::HTTP_FORBIDDEN ); } }
629 is the start of the
self::setErrorMessage
-
@Tom-Elliott Apologies, I don’t know what went wrong there but somehow I got an incomplete version of your changes. git is a confusing beast to me :). I’ve now checked again that I’m running dev-branch and I can confirm that all the warnings have gone.
Thanks again.