WordPress.org

Make WordPress Core

Opened 15 months ago

Last modified 4 months ago

#44745 new defect (bug)

REST API: incorrect slashes in url if parent empty

Reported by: nicomollet Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 4.9.8
Component: REST API Keywords: needs-patch
Focuses: Cc:
PR Number:

Description

Hello everyone,

I am using the WP.API with a custom versionString: "wc/v2/" from WooCommerce.

When fetching ProductCategories (or any other collections with two capital letters), the url called is:
http://websiteurl/wp-json/wc/v2/products//categories

Example of the code I use, to be able to reproduce the error:

  wp.api.init({'versionString': 'wc/v2/'});
  var productcategories = new wp.api.collections.ProductsCategories();
  productcategories.fetch().done( function() {
    productcategories.each( function( productcategory ) {
      console.log( productcategory.attributes );
    } );
  } );

My tentative fix is the following:

In wp-api.js, line 1406
The parent is sometimes empty, leading to two consecutive slashes.

// Function that returns a constructed url passed on the parent.
url: function() {
  return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
      parentName + '/' + this.parent + '/' +
      routeName;
},

should be replaced by:

// Function that returns a constructed url passed on the parent.
url: function() {
  return routeModel.get( 'apiRoot' ) + routeModel.get( 'versionString' ) +
    parentName + '/' +
    ( ( _.isUndefined( this.get( 'parent' ) ) || 0 === this.get( 'parent' ) ) ?
      ( _.isUndefined( this.get( 'parent_post' ) ) ? '' : this.get( 'parent_post' ) + '/' ) :
      this.get( 'parent' ) + '/' ) +
    routeName;
},

Thank you very much
Nicolas

Change History (1)

This ticket was mentioned in Slack in #core-restapi by timothybjacobs. View the logs.


4 months ago

Note: See TracTickets for help on using tickets.