Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix incorrect error for invalid customised payload
The change in #113 didn't handle a common usage scenario where a user modifies the payload passed into the block (example usage [1]). In order to handle this we need to make a copy of the original payload before it is modified. As this project doesn't have an ActiveSupport dependency I wrote the equivalent of a deep_dup method. using `Marshal.load(Marshal.dump(object))`, with an expectation that given the data generated is JSON in a Ruby Hash/Array structure there'd be no risk of objects not having Marshal support. Marshal is a Ruby module that can serialise Ruby objects into a byte stream and can take a byte stream and re-create the objects. By doing this for a payload it creates new objects for the entire payload hash. This means that any modifications to the payload are not represented in the original_payload object. An earlier approach was to iterate ourselves, but that was a bit more verbose: ``` def deep_dup_payload(item) case item when Array item.map { |i| deep_dup_payload(i) } when Hash item.dup.transform_values { |v| deep_dup_payload(v) } else item.dup end end ``` Anecdotally it does seem that previously this project considered modifying the payload as unsupported [2], but this wasn't enforced by a frozen object and would now be a breaking change to require this. [1]: https://github.com/alphagov/content-data-api/blob/cddc744baa0a073868a527b5513a7943fa53ddc6/spec/factories/messages.rb#L19-L26 [2]: https://github.com/alphagov/govuk_schemas/blob/e1da17587b10df8ae3b29d7583c3a4881a377f97/spec/lib/random_example_spec.rb#L45-L53
- Loading branch information