Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix retrieving autosaves when using a custom rest_namespace #41542

Merged
merged 6 commits into from
Jun 27, 2022

Conversation

tomjn
Copy link
Contributor

@tomjn tomjn commented Jun 3, 2022

What?

When specifying rest_namespace in a custom post type, a hardcoded wp/v2 in getAutosaves results in a 404

So instead, use the rest namespace from the type, not just the base

Why?

Because bugs are bad and CPT's with rest_namespace deserve autosaves too!

How?

By not hardcoding /wp/v2

Testing Instructions

  1. Register a CPT named test
  2. In the registration, use rest_namespace and set it to something such as testing/v99
  3. Save permalinks
  4. Try to edit a post and there should not be a 404 for wp/v2/test?autosaves.... in the browser error console

When specifying `rest_namespace` in a custom post type, a hardcoded `wp/v2` in `getAutosaves` results in a 404
@tomjn tomjn requested a review from nerrad as a code owner June 3, 2022 16:08
@gziolo gziolo added [Type] Bug An existing feature does not function as intended REST API Interaction Related to REST API labels Jun 4, 2022
@TimothyBJacobs
Copy link
Member

Thanks for the PR @tomjn!

The test failures do look related. I'm guessing that the post types response is mocked somewhere and that mock is missing a rest_namespace field definition?

@adamsilverstein
Copy link
Member

Good catch @tomjn - your code probably just needs a fallback if namespace is undefined to get tests passing. I did notice we have some other non-test instances of wp/v2 being hard coded (for example post trashing also has it, see https://github.com/WordPress/gutenberg/search?q=wp%2Fv2). it would be good to:

  1. fix all instances of hard-coded name spaces
  2. add tests with post with custom namespaces to validate endpoints are constructed as expected

@tomjn
Copy link
Contributor Author

tomjn commented Jun 5, 2022

I'll be honest, I wrote this in the github UI then took a flight so not surprised it failed a check or two, so this is my first chance to respond.

I do remember seeing the namespace referenced like this elsewhere:

const namespace = postType?.rest_namespace ?? 'wp/v2';

@tomjn
Copy link
Contributor Author

tomjn commented Jun 5, 2022

@adamsilverstein @TimothyBJacobs I've addressed the linter issue, took me a bit more time than expected to get the dependencies/npm setup locally, I'm unsure on the best way to implement ?? while destructing. I thought about something like this:

	const type = await resolveSelect.getPostType( postType );
	const base = type.rest_base;
	const namespace = type?.rest_namespace ?? 'wp/v2';
	const autosaves = await apiFetch( {
		path: `/${ namespace }/${ base }/${ postId }/autosaves?context=edit`,
	} );

I expect there'll be pushback over the naming of type though, suggestions?

@tomjn
Copy link
Contributor Author

tomjn commented Jun 5, 2022

Updating the test mock data and adding new tests is beyond my bandwidth/knowledge for the project at the moment though, so I'll need help or someone else to pick it up

@TimothyBJacobs
Copy link
Member

You should be able to assign a default while destructuring.

  const { rest_namespace: restNamespace = 'wp/v2' } = resolveSelect.getPostType( postType );

@tomjn
Copy link
Contributor Author

tomjn commented Jun 7, 2022

Screenshot 2022-06-07 at 11 08 54

🥳 thanks @TimothyBJacobs that has CI passing!

Copy link
Member

@adamsilverstein adamsilverstein left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Would be nice to add tests to validate and fix any other hard coded namespaces in a future PR.

@adamsilverstein
Copy link
Member

Nice work @tomjn - approved!

I'd love to see a follow up adding the tests and fixing any other hard coded namespace instances. Could you please create an issue to track this (even if you don't have time to work on the PR)?

@hgodinho
Copy link

Is there a way to disable the autosave feature until this PR is merged?
The first call is taking more than 4 seconds and delaying the full loading of the page...

@tomjn
Copy link
Contributor Author

tomjn commented Jun 19, 2022

@adamsilverstein I no longer have the bandwidth to work on this, I don't have the capacity to push it forward, if you or someone else can create an issue for it I'd appreciate it, this was mostly me trying to help out someone on stack exchange and doesn't impact me personally. I also see there are now conflicts resulting from formatting changes made elsewhere

@skorasaurus skorasaurus added the Needs Dev Ready for, and needs developer efforts label Jun 23, 2022
Copy link
Member

@spacedmonkey spacedmonkey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to go to me!

@spacedmonkey
Copy link
Member

Related: #41950

@Mamaduka Mamaduka removed the Needs Dev Ready for, and needs developer efforts label Jun 27, 2022
@Mamaduka
Copy link
Member

Hi, @tomjn

It looks like static analysis checks are failing. Do you mind rebasing this branch and running the linter on the changed file?

@tomjn
Copy link
Contributor Author

tomjn commented Jun 27, 2022 via email

@Mamaduka
Copy link
Member

Thanks for the quick reply, @tomjn. Happy to take over to PR and see this merged.

Co-authored-by: Jonny Harris <[email protected]>
@spacedmonkey
Copy link
Member

@Mamaduka I think we are good to merge now.

@Mamaduka Mamaduka merged commit d81b923 into WordPress:trunk Jun 27, 2022
@Mamaduka
Copy link
Member

Thanks for the fix, @spacedmonkey!

@github-actions github-actions bot added this to the Gutenberg 13.6 milestone Jun 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
REST API Interaction Related to REST API [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants