quick icon indicating copy to clipboard operation
quick copied to clipboard

compareAttributesHash fails if non-persistent struct attributes are defined

Open DiscountDarcy opened this issue 4 years ago • 1 comments

When hydrating an entity from a cache, we stash a couple values in a non-persistent struct on the top level of the entity. This throws an error in 4.2.3 in compareAttributesHash() when it tries save "" to a struct property on this line:

var value = valueIsNotNull ? attributes[ arguments.key ] : "";

As a workaround, we added a filter at the top of compareAttributesHash() to just ignore any attributes passed into that function that aren't defined as attributes on the entity, since we don't care about hashing non-persistent attributes anyway for the purposes of detecting whether the object is dirty.

There's probably a more elegant way to do this, but here's our updated function:

	/**
	 * Computes an hash from a struct of key / value pairs.
	 *
	 * @attributes  A struct of attributes data to compute.
	 *
	 * @return      string
	 */
	public string function computeAttributesHash( required struct attributes ) {
		var validAttrs = this.get_attributes();
		var keys = arguments.attributes.keyArray().filter( ( attr ) => {
			return validAttrs.keyExists( attr );
		} );
		arraySort( keys, "textnocase" );
	
		return hash(
			keys.map( function( key ) {
					var valueIsNotNull = structKeyExists( attributes, arguments.key ) &&
					!isNull( attributes[ arguments.key ] );
					var value = valueIsNotNull ? attributes[ arguments.key ] : "";
					return lCase( arguments.key ) & "=" & value;
				} )
				.toList( "&" )
		);
	}

DiscountDarcy avatar Mar 03 '21 18:03 DiscountDarcy

Can you provide a reproduction on latest version of Quick? Thanks.

elpete avatar Dec 06 '23 17:12 elpete