gocodebox / lifterlms-rest

LifterLMS REST API Feature Plugin
6 stars 7 forks source link

Request to `access-plans/{id}` results in an error when no `price` is supplied for free access plans #267

Closed thomasplevy closed 2 years ago

thomasplevy commented 2 years ago

Reproduction Steps

Example payload:

{"title":"An updated title"}

Expected Behavior

Actual Behavior

Error Messages / Logs

{
    "code": "invalid_meta",
    "message": "Cannot insert/update the price meta",
    "data": null
}

Notes

I encountered this while developing the react-powered UI for managing access plans. This is easy enough to work around but ideally we shouldn't have to provide the price with every update request.

On closer inspection

This problem arises when not supplying the price for a free access plan or when supplying the existing price on paid access plans.

EG: If I leave the price empty for a free plan (by excluding the key from the payload) I get this error.

With a paid plan, if I specify the existing plan amount I also get this error.

This leads me to believe that this is actually an issue we've previously identified with regards to how we return false from the core postmodel class when updating meta data (and the value is unchanged). This is treated as an error by the rest api since there's no way to disambiguate between a false response that means NO CHANGE MADE and false response resulting from an error.

eri-trabiccolo commented 2 years ago

@thomasplevy yeah the problem is this one: https://github.com/gocodebox/lifterlms/issues/909

I think we then have to treat here only this case:

EG: If I leave the price empty for a free plan (by excluding the key from the payload) I get this error.

I actually already accounted for this kind of errors: https://github.com/gocodebox/lifterlms-rest/blob/1.0.0-beta.23/includes/server/class-llms-rest-access-plans-controller.php#L611

But I must've done something wrong. :D

Yeah, the issue was a strict type comparison one, the access plan's price is a float, and I was comparing it to the int 0.