aws / aws-sdk-ruby-record

Official repository for the aws-record gem, an abstraction for Amazon DynamoDB.
Apache License 2.0
318 stars 41 forks source link

Do not try to hydrate undeclared attributes from storage on batch_read #139

Closed nronas closed 2 months ago

nronas commented 2 months ago

Issue #, if available: N/A

Description of changes:

Hello all and thank you for the great gem. There is an asymmetry around hydrating records on read operations between canonical queries and batch reads. The asymmetry is located around undeclared attributes on the model class.

Example of model with undeclared attribute

#
# Storage has ID, name and age.
#
class Model
  include Aws::Record

  string_attr :ID, hash_key: true
  string_attr :name
end

On the above model when using canonical queries like Model.find(ID: '123') or Model.query(...) any undeclared attributes are omitted when building the final model instance. So in the above case we will end up with a Model instance that does not have age hydrated onto it.

But if we use a Batch Read then the build_item method will try to call #= on Nil.

NoMethodError: undefined method `=' for an instance of Model
from /Users/nronas/.rbenv/versions/3.3.3/lib/ruby/gems/3.3.0/gems/aws-record-2.13.0/lib/aws-record/record/attributes.rb:69:in `block in initialize'

This PR just skips any storage attributes that are not declared on the model class when batch reading

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

jterapin commented 2 months ago

Hi! Thank you for submitting this fix. This looks good.

nronas commented 2 months ago

@mullermp done

mullermp commented 2 months ago

Sorry, this also needs a changelog entry, you can add something like

Issue - Do not try to hydrate undeclared attributes from storage on `batch_read`.
<newline>

Then it's good to go

nronas commented 2 months ago

@mullermp wasn't sure how the release process goes, will do that asap. Should this be under the Unreleased Changes section on the CHANGELOG.md?

jterapin commented 2 months ago

New version of the gem is out: https://rubygems.org/gems/aws-record/versions/2.13.1