Skip to content

Commit 3de8bf5

Browse files
author
Philip Champon
committed
entity caveats
1 parent abb1738 commit 3de8bf5

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.markdown

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,37 @@ module API
522522
end
523523
```
524524

525+
### Caveats
526+
527+
Entities with duplicate exposure names and conditions will silently overwrite one another.
528+
In the following example, when object#check equals "foo", only afield will be exposed.
529+
However, when object#check equals "bar" both bfield and foo will be exposed.
530+
531+
```ruby
532+
module API
533+
module Entities
534+
class User < Grape::Entity
535+
expose :afield, :foo, :if => lambda{|object,options| object.check=="foo"}
536+
expose :bfield, :foo, :if => lambda{|object,options| object.check=="bar"}
537+
end
538+
end
539+
end
540+
```
541+
542+
This can be problematic, when you have mixed collections. Using #respond_to? is safer.
543+
544+
```ruby
545+
module API
546+
module Entities
547+
class User < Grape::Entity
548+
expose :afield, :if => lambda{|object,options| object.check=="foo"}
549+
expose :bfield, :if => lambda{|object,options| object.check=="bar"}
550+
expose :foo, :if => lambda{object,options| object.respond_to?(:foo)}
551+
end
552+
end
553+
end
554+
```
555+
525556
## Describing and Inspecting an API
526557

527558
Grape lets you add a description to an API along with any other optional

0 commit comments

Comments
 (0)