WordPress.org

Make WordPress Core

Opened 11 months ago

Closed 9 months ago

#39341 closed defect (bug) (fixed)

`wp.api.utils.decorateFromRoute` should use `_.extend` not `_.union`

Reported by: Magenta Cuda Owned by: adamsilverstein
Milestone: 4.7.3 Priority: normal
Severity: normal Version: 4.7
Component: REST API Keywords: has-patch dev-feedback needs-testing needs-unit-tests reporter-feedback fixed-major
Focuses: javascript Cc:

Description

modelInstance.prototype.options = _.union( routeEndpoint.args, modelInstance.prototype.options );

Are not the arguments to _.union objects not arrays? In which case _.extend would be more appropriate.

Attachments (2)

39341.diff (858 bytes) - added by adamsilverstein 11 months ago.
39341.patch (868 bytes) - added by ketuchetan 10 months ago.

Download all attachments as: .zip

Change History (13)

#1 @adamsilverstein
11 months ago

  • Focuses javascript added
  • Owner set to adamsilverstein
  • Status changed from new to assigned

@Magenta Cuda thanks for the bug report. I am going to look into this, can I ask you though why you are raising it as a bug, did you come across somewhere trying to use the library and it failed because of this code? eg. does using _.union here vs _.extend actually break something, or are you noting it seems wrong on a pure code bases. If this does cause some demonstrable issue, I'd like to write a unit test demonstrating that so we can verify the code changes fixes the issue.

#2 @adamsilverstein
11 months ago

  • Keywords has-patch dev-feedback needs-testing needs-unit-tests added

@Magenta Cuda

I dug into the code a bit and for our default routes the model prototypes didn't have default options or args, so the _.union call was never reached. You are correct though that args or options at this point would be an object, not an array, so _.extend is more appropriate here.

In 39341.diff I fixed up the logic and switched from union to extend. If you have an issue that was caused by this bug I would appreciate if you could test the patch to see if it resolves your issue. I am going to try to add a unit test by mocking a custom endpoint that provides the default args & options this code is designed to extend, see #39264.

Thanks.

#3 @adamsilverstein
11 months ago

  • Summary changed from problem in wp-api.js line 205 to `wp.api.utils.decorateFromRoute` should use `_.extend` not `_.union`

#4 @adamsilverstein
11 months ago

  • Keywords reporter-feedback added

#5 @adamsilverstein
10 months ago

  • Milestone changed from Awaiting Review to 4.7.2

@ketuchetan
10 months ago

#7 @adamsilverstein
10 months ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 40040:

REST API: JavaScript client should use _.extend when merging objects.

Correct an issue during the client's dynamic route discovery in wp.api.utils.decorateFromRoute where _.union potentially failed if used on objects.

Props ketuchetan.
Fixes #39341.

#8 follow-up: @SergeyBiryukov
10 months ago

  • Keywords fixed-major added
  • Resolution fixed deleted
  • Status changed from closed to reopened

Reopening for 4.7.3 consideration.

#9 in reply to: ↑ 8 @adamsilverstein
10 months ago

Replying to SergeyBiryukov:

Reopening for 4.7.3 consideration.

Thanks @SergeyBiryukov!

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.


10 months ago

#11 @dd32
9 months ago

  • Resolution set to fixed
  • Status changed from reopened to closed

In 40084:

REST API: JavaScript client should use _.extend when merging objects.

Correct an issue during the client's dynamic route discovery in wp.api.utils.decorateFromRoute where _.union potentially failed if used on objects.

Props ketuchetan, adamsilverstein.
Merges [40040] to the 4.7 branch.
Fixes #39341.

Note: See TracTickets for help on using tickets.