In XeroQuery.php, a POST request (update) is always changed to PUT if $query->uuid is not set.

Why is this so? The POST example in the documentation makes no mention of requiring a uuid.

The comment for $uuid in XeroQuery.php states that it is "The xero UUID to use for a quick filter in get queries." XeroQuery::setId() states "Useful only in get method". OK then - but in XeroQuery::execute(), we have this:

// Change to PUT if UUID is not set for a post.
      if ($this->method === 'post' && !$this->uuid) {
        $this->setMethod('put');
      }

So while it may only be "useful" for GET queries, not setting it has other consequences.

It looks to be impossible to update an existing Xero item via POST using the example code. The request will always be made via PUT and will fail because a Xero item with those distinguishing characteristics already exists (Contact Name, for example).

Issue fork xero-3227619

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

John Pitcairn created an issue. See original summary.

johnpitcairn’s picture

Issue summary: View changes
johnpitcairn’s picture

So what is currently needed if you want to update a list of existing contacts is something like:

$response_data = $query
    ->setType('xero_contact')
    ->setMethod('post')
    ->setData($contacts)
    ->setId($contacts->first()->get('ContactID')->getValue())
    ->execute();

Correct?

That's still a bit of a WTF moment, because the UUID you are giving it is only one of potentially many in the contact list. It makes little sense.

We could just update the README to include the above, but first I think it might be better to examine why that POST/PUT switch is there in the first place. Is it just a hangover from some outdated implementation? Can/should it be removed? If not, a more verbose comment for that and the $uuid property/method is needed, as well as a README update.

johnpitcairn’s picture

Category: Support request » Task
johnpitcairn’s picture

Title: POST always changed to PUT if query uuid not set, documentation unclear » Clarify the uuid requirement for POST queries
jonathanshaw’s picture

I think it might be better to examine why that POST/PUT switch is there in the first place. Is it just a hangover from some outdated implementation?

I think you're right.

The doc for setId say it's only relevant to get, and it is definitely used there.

  /**
   * Set the Xero UUID for the request. Useful only in get method.
...
   */
  public function setId($uuid) {

Xero docs say that post can create or update, put is create only.

If we want to have this put/post switch, it should probably be linked to an explicit flag "canCreate" not this implicit uuid logic that only makes sense when working with a single item.

jonathanshaw’s picture

Status: Active » Needs review

I suggest a simple removal.

johnpitcairn’s picture

Status: Needs review » Reviewed & tested by the community

I agree. If anything is setting the uuid for post queries, it won't cause any issues.

I can't imagine a scenario where you'd be relying on POST getting silently switched to PUT?

mradcliffe made their first commit to this issue’s fork.

mradcliffe’s picture

Status: Reviewed & tested by the community » Fixed

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.