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

update_item fails with data format error in Faraday #8

Open
sshipway opened this issue Jun 1, 2022 · 1 comment
Open

update_item fails with data format error in Faraday #8

sshipway opened this issue Jun 1, 2022 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@sshipway
Copy link

sshipway commented Jun 1, 2022

Describe the bug

The update_item() call fails, as the supplied data format is wrong

  • The specified content-type, although strictly correct with regard to RFC6902, causes the Farady library to stop json-encoding
  • The API requires an array of changes, but the function only provides a single change

To Reproduce

Try updating an item via the SDK, such as using

      item = op.update_item(
        vault_id: vaultid,
        id: itemid,
        op: 'replace', path: path, value: newpass
      )

This fails with "undefined method `bytesize'"

Expected behavior

The call should return cleanly with an updated object

Environment

  • Gem version: 0.1.3
  • Ruby version: 2.7

Additional context

The problem is here in client/items.rb -

def update_item(vault_id:, id:, **attributes)
    Item.new patch("vaults/#{vault_id}/items/#{id}", body: attributes, headers: {"Content-Type": "application/json-patch+json"}).body
end

The content-type header causes Faraday library to skip the JSON encoding which results in the error. Although this header is supposedly necessary according to the RFC in fact it is not required by the 1Password API.

In addition, though, the API expects the changes (in the body) to be a list. However as attributes comes from the remaining named parameters this is not possible

A solution is to instead use:

def update_item(vault_id:, id:, **attributes)
   Item.new patch("vaults/#{vault_id}/items/#{id}", body: [ attributes ], headers: {}).body
end

This converts the attributes into the required array, and returns to the default content-type (JSON) which is properly encoded by the Faraday library.

Tested Faraday library versions
faraday (1.10.0)
faraday-em_http (1.0.0)
faraday-em_synchrony (1.0.0)
faraday-excon (1.1.0)
faraday-httpclient (1.0.1)
faraday-multipart (1.0.3)
faraday-net_http (1.0.1)
faraday-net_http_persistent (1.2.0)
faraday-patron (1.0.0)
faraday-rack (1.0.0)
faraday-retry (1.0.3)
faraday_middleware (1.2.0)

@sshipway sshipway added the bug Something isn't working label Jun 1, 2022
@sshipway
Copy link
Author

sshipway commented Nov 2, 2022

Pull request #9 provided for this fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants