Skip to content

Commit 7ce9302

Browse files
authored
Merge pull request #1851 from bibonix/fix-1168-user-name-null
#1168: handle JSON null in User.Smart#name() and #hasName()
2 parents 0d57c51 + 1011da7 commit 7ce9302

2 files changed

Lines changed: 43 additions & 3 deletions

File tree

src/main/java/com/jcabi/github/User.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,8 @@ public URL url() throws IOException {
162162
*/
163163
public String name() throws IOException {
164164
final JsonObject json = this.json();
165-
if (!json.containsKey("name")) {
165+
final String key = "name";
166+
if (!json.containsKey(key) || json.isNull(key)) {
166167
throw new IllegalStateException(
167168
String.format(
168169
// @checkstyle LineLength (1 line)
@@ -171,7 +172,7 @@ public String name() throws IOException {
171172
)
172173
);
173174
}
174-
return json.getString("name");
175+
return json.getString(key);
175176
}
176177

177178
/**
@@ -180,7 +181,9 @@ public String name() throws IOException {
180181
* @throws IOException If it fails
181182
*/
182183
public boolean hasName() throws IOException {
183-
return this.json().containsKey("name");
184+
final JsonObject json = this.json();
185+
final String key = "name";
186+
return json.containsKey(key) && !json.isNull(key);
184187
}
185188

186189
/**

src/test/java/com/jcabi/github/RtUserTest.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ void checksIfHeHasNoName() throws IOException {
9999
);
100100
}
101101

102+
@Test
103+
void hasNameReturnsFalseWhenNameIsNull() throws IOException {
104+
MatcherAssert.assertThat(
105+
"hasName() must return false when 'name' JSON value is null",
106+
RtUserTest.userWithNullName().hasName(),
107+
Matchers.equalTo(false)
108+
);
109+
}
110+
111+
@Test
112+
void nameThrowsIllegalStateWhenNameIsNull() throws IOException {
113+
Assertions.assertThrows(
114+
IllegalStateException.class,
115+
RtUserTest.userWithNullName()::name,
116+
"name() must throw IllegalStateException when 'name' JSON value is null"
117+
);
118+
}
119+
102120
@Test
103121
void describeAsJson() throws IOException {
104122
final RtUser user = new RtUser(
@@ -422,6 +440,25 @@ void markAsReadErrorIfResponseStatusIsNot205() throws IOException {
422440
}
423441
}
424442

443+
/**
444+
* Return User.Smart with a JSON null "name" property.
445+
* @return User.Smart whose JSON has "name":null.
446+
*/
447+
private static User.Smart userWithNullName() {
448+
return new User.Smart(
449+
new RtUser(
450+
Mockito.mock(GitHub.class),
451+
new FakeRequest().withBody(
452+
Json.createObjectBuilder()
453+
.addNull("name")
454+
.build()
455+
.toString()
456+
),
457+
"octoc"
458+
)
459+
);
460+
}
461+
425462
/**
426463
* Return User.Smart with given property.
427464
* @param property The property as specified at https://developer.github.com/v3/users/#get-a-single-user

0 commit comments

Comments
 (0)