procore-oss / blueprinter

Simple, Fast, and Declarative Serialization Library for Ruby
MIT License
1.14k stars 109 forks source link

Conditional views #423

Closed alexkalderimis closed 2 months ago

alexkalderimis commented 5 months ago

Prior to this change, applying conditions to a view required annotating all fields individually.

This change allows views to be defined as view :name, if: condition, using the same ->(field_name, object, options) { ... } callbacks as used on fields. These callbacks are propagated to the fields, and are in addition to (not in replacement of) any checks defined on the fields.

Tests are added to cover the logic of unless and if, and Field#skip? is altered to allow a field to have both unless and if defined.

Defining a view-level condition on an child blueprint does not affect the parent view of the same name - this behavior may be unexpected, and it is advised not to add conditions to inherited views, since this may represent a security flaw.

Checklist:

jhollinger commented 5 months ago

I concur with @lessthanjacob. But I'd also like to understand the use-case better. Why would you want to make all fields in a view conditional? If you want to hide all fields, couldn't the caller just make that determination and use a different view?

github-actions[bot] commented 3 months ago

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.