Skip to content

Commit 232baa5

Browse files
authored
Clarify README for overriding an Entity's documented name (#961)
1 parent c76eac3 commit 232baa5

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

README.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@
7575
- [Adding root element to responses ](#adding-root-element-to-responses-)
7676
- [Multiple present Response ](#multiple-present-response-)
7777
- [Using Grape Entities ](#using-grape-entities-)
78+
- [Documented class/definition](#documented-classdefinition)
7879
- [Relationships](#relationships)
7980
- [1xN](#1xn)
8081
- [1x1](#1x1)
@@ -1515,7 +1516,6 @@ The result will look like following:
15151516
Add the [grape-entity](https://github.com/ruby-grape/grape-entity) and [grape-swagger-entity](https://github.com/ruby-grape/grape-swagger-entity) gem to your Gemfile.
15161517

15171518
The following example exposes statuses. And exposes statuses documentation adding :type, :desc and :required.
1518-
The documented class/definition name could be set via `#entity_name`.
15191519

15201520
```ruby
15211521
module API
@@ -1558,6 +1558,57 @@ module API
15581558
end
15591559
```
15601560

1561+
### Documented class/definition
1562+
1563+
You can set the name of the Entity when being documented via `#entity_name`:
1564+
1565+
```ruby
1566+
module API
1567+
module Entities
1568+
class Status < Grape::Entity
1569+
expose :text, documentation: { type: 'string', desc: 'Status update text.', required: true }
1570+
end
1571+
1572+
class Link < Grape::Entity
1573+
expose :href, documentation: { type: 'url' }
1574+
1575+
def self.entity_name
1576+
'LinkedStatus'
1577+
end
1578+
1579+
end
1580+
end
1581+
end
1582+
```
1583+
Should generate the following definitions in your swagger json:
1584+
1585+
```json
1586+
{
1587+
"definitions": {
1588+
"API_Entities_Status": {
1589+
"type": "object",
1590+
"properties": {
1591+
"text": {
1592+
"type": "string",
1593+
"description": "Status update text.",
1594+
},
1595+
},
1596+
"required": [
1597+
"text",
1598+
],
1599+
"description": "API_Entities_Pet model"
1600+
},
1601+
"LinkedStatus": {
1602+
"type": "object",
1603+
"properties": {
1604+
"href": {
1605+
"type": "url",
1606+
},
1607+
"description": "LinkedStatus model"
1608+
}
1609+
}
1610+
}
1611+
```
15611612

15621613
### Relationships
15631614

0 commit comments

Comments
 (0)