-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
00ac397
commit 3977f2c
Showing
4 changed files
with
141 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ classes/ | |
.idea/ | ||
.idea_modules | ||
build/ | ||
**/out/ | ||
*.iml | ||
*.ipr | ||
*.iws | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
101 changes: 101 additions & 0 deletions
101
json/src/test/groovy/grails/plugin/json/view/NullRenderingSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
package grails.plugin.json.view | ||
|
||
import grails.plugin.json.view.test.JsonViewTest | ||
import spock.lang.Specification | ||
|
||
class NullRenderingSpec extends Specification implements JsonViewTest { | ||
|
||
void "test rendering nulls with a domain"() { | ||
given: | ||
def templateText = ''' | ||
import grails.plugin.json.view.* | ||
model { | ||
Player player | ||
} | ||
json g.render(player) | ||
''' | ||
|
||
when: | ||
mappingContext.addPersistentEntity(Player) | ||
def renderResult = render(templateText, [player: new Player()]) | ||
|
||
then:"No fields are rendered because they are null" | ||
renderResult.jsonText == '{}' | ||
} | ||
|
||
void "test rendering nulls with a domain (renderNulls = true)"() { | ||
given: | ||
def templateText = ''' | ||
import grails.plugin.json.view.* | ||
model { | ||
Player player | ||
} | ||
json g.render(player, [renderNulls: true]) | ||
''' | ||
when: | ||
mappingContext.addPersistentEntity(Player) | ||
def renderResult = render(templateText, [player: new Player()]) | ||
then:"No fields are rendered because they are null" | ||
renderResult.jsonText == '{"name":null,"team":null}' | ||
} | ||
void "test rendering nulls with a map"() { | ||
given: | ||
def templateText = ''' | ||
model { | ||
Map map | ||
} | ||
json g.render(map) | ||
''' | ||
when: | ||
mappingContext.addPersistentEntity(Player) | ||
def renderResult = render(templateText, [map: [foo: null, bar: null]]) | ||
then:"Maps with nulls are rendered by default" | ||
renderResult.jsonText == '{"foo":null,"bar":null}' | ||
} | ||
void "test rendering nulls with a pogo"() { | ||
given: | ||
def templateText = ''' | ||
model { | ||
Object obj | ||
} | ||
json g.render(obj) | ||
''' | ||
when: | ||
mappingContext.addPersistentEntity(Player) | ||
def renderResult = render(templateText, [obj: new Child2()]) | ||
then:"No fields are rendered because they are null" | ||
renderResult.jsonText == '{}' | ||
} | ||
void "test rendering nulls with a pogo (renderNulls = true)"() { | ||
given: | ||
def templateText = ''' | ||
model { | ||
Object obj | ||
} | ||
json g.render(obj, [renderNulls: true]) | ||
''' | ||
when: | ||
mappingContext.addPersistentEntity(Player) | ||
def renderResult = render(templateText, [obj: new Child2()]) | ||
then: | ||
renderResult.jsonText == '{"name":null,"parent":null}' | ||
} | ||
} |