Fix REST API 404 response during Client Credentials setup step
Bug Description
Several REST API endpoints (i.e. credentials, API key, GCP project) return a 404 if no value is set for them yet (being most apparent during the setup flow when Site Kit checks for whether OAuth credentials are already set). This was intentionally designed like that originally, but revisiting it doesn't actually make sense. That response code should only be expected if 404 is seen as "the data doesn't exist", however not in the more RESTful way of 404 meaning that "the resource doesn't exist". The resource does in fact exist as it is the endpoint itself. The only place in REST where 404s should be used are route URIs containing variables where the passed variable name doesn't identity an existing resource. For all these endpoints in Site Kit, that is however not the case, so the response for all of them should be turned into a 200.
Steps to reproduce
- Go to '...'
- Click on '....'
- Scroll down to '....'
- See error
Screenshots
Additional Context
- PHP Version:
- OS: [e.g. iOS]
- Browser [e.g. chrome, safari]
- Plugin Version [e.g. 22]
- Device: [e.g. iPhone6]
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
- No 404 error should be given for any request in the console during the client credentials step in the setup wizard.
- The routes
core/site/data/gcpproject,core/site/data/apikey, andcore/site/data/credentialscan never return a 404, but instead a 200 with an empty dataset following the required format (object with expected properties forgcpprojectandcredentials, falsy scalar value forapikey). - All JavaScript code relying on one of these values must be ensured to correctly check whether the respective value is present and not empty, to replace the "falsy" lookup previously covered by the 404.
Implementation Brief
- Add
parse_defaults()method toAuthentication\Credentialsclass, which works similar to the one inGCP_Project\Credentialsclass, ensuringoauth2_client_idandoauth2_client_secretare always present in the returned object. - Remove the
! $gcp_project->has()if clause from theGET core/site/data/gcpprojectroute inREST_API\REST_Routes. - Remove the
! $api_key->has()if clause from theGET core/site/data/apikeyroute inREST_API\REST_Routes. - Remove the
! $credentials->has()if clause from theGET core/site/data/credentialsroute inREST_API\REST_Routes. - Change https://github.com/google/site-kit-wp/blob/develop/assets/js/components/setup-wizard/wizard-step-client-credentials.js#L67 to check for
response && response.oauth2_client_id && response.oauth2_client_secretinstead, to account for the new response. - The other two changed routes aren't actually called by the plugin currently and therefore don't need adjustment.
Changelog entry
- Fix inaccurate 404 error response of client credentials REST API call during setup.
IB LGTM!
QA PASS
Went through setup flow a couple times and did not see any 404s in console or network.