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

Default field values should be added to responses as clones, rather than by reference #11

Merged
merged 1 commit into from
Apr 8, 2015

Conversation

Radagaisus
Copy link
Owner

This solves an issue where modifying responses would "pollute" default values for other queries.

  • Now using the node-clone package, version 1.0.2
  • Fixed the issue: default values will be cloned before being added to the response
  • Prettified the code around the cloning of the default value a bit
  • Added a validation test
    # This test validates that default field values are cloned and passed by-value
    # to the response, rather than by-reference. If the end-user tries to modify
    # a response that contains by-reference default fields, this can "pollute"
    # default values for other queries. The scenario we test here:
    # 
    #1. Every reader has a set of books that defaults to an empty array.
    #2. We retrieve the first reader's (empty) list of books
    #3. We update that list with a new book that that reader has read
    #4. We retrieve the second reader's (empty) list of books.
    # 
    # When default values are passed by-reference, this would've caused the second
    # reader's list of books to be "polluted" with the first readers book. Cloning
    # the default value takes care of this.
    # 
    it 'Default field values are returned in the response as clones', (done) -> 
        # Define a new Reader model, that holds a set of books they've read, that
        # defaults to an empty array. Arrays are by-reference in JavaScript.
        class Reader extends Orpheus
            constructor: ->
                @set 'books', default: []
        # Create the reader API
        reader = Reader.create()
        # Retrieve the first reader's books. The first reader has no books yet,
        # so this will return the default value - an empty array.
        reader('first').books.members().exec (err, res) ->
            # Update the reader's books with a new book they've read.
            res.books.push('The Great Hunt')
            # Update the books set for the first reader in the database. This step
            # is not really needed for the test, but it helps illustrate a real
            # world scenario for this issue.
            reader('first').books.add(res.books).exec (err, res) ->
                # Retrieve the books of the second reader. The second reader has no
                # books yet, so we expect the list to be empty. The bug we test here
                # would have caused the empty default array to be "polluted" with the
                # first reader's books.
                reader('second').books.members().exec (err, res) ->
                    # Check that the books list is empty
                    expect(res.books.length).toEqual 0
                    # We're done.
                    done()

…than the actual default field value

This solves an issue where modifying responses would "pollute" default values for other queries.

- Now using the `node-clone` package, version 1.0.2
- Fixed the issue: default values will be cloned before being added to the response
- Added a validation test
@Radagaisus Radagaisus self-assigned this Apr 8, 2015
Radagaisus added a commit that referenced this pull request Apr 8, 2015
Default field values should be added to responses as clones, rather than by reference
@Radagaisus Radagaisus merged commit bc0be85 into master Apr 8, 2015
@Radagaisus Radagaisus deleted the fix/by-value-defaults branch April 8, 2015 17:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant