Skip to content

Commit 28f27d2

Browse files
committed
Ensure null-safety for String uri and instance
1 parent 939a2b7 commit 28f27d2

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/main/java/io/github/malczuuu/problem4j/core/ProblemBuilder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public interface ProblemBuilder {
2626
* @param type string URI identifying the problem type
2727
* @return this builder instance for chaining
2828
* @throws IllegalArgumentException if the string is not a valid URI
29-
* @throws NullPointerException if the string is null
3029
*/
3130
ProblemBuilder type(String type);
3231

@@ -77,7 +76,6 @@ public interface ProblemBuilder {
7776
* @param instance string URI identifying the problem occurrence
7877
* @return this builder instance for chaining
7978
* @throws IllegalArgumentException if the string is not a valid URI
80-
* @throws NullPointerException if the string is null
8179
*/
8280
ProblemBuilder instance(String instance);
8381

src/main/java/io/github/malczuuu/problem4j/core/ProblemBuilderImpl.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public ProblemBuilder type(URI type) {
2727

2828
@Override
2929
public ProblemBuilder type(String type) {
30-
return type(URI.create(type));
30+
return type != null ? type(URI.create(type)) : type((URI) null);
3131
}
3232

3333
@Override
@@ -67,7 +67,7 @@ public ProblemBuilder instance(URI instance) {
6767

6868
@Override
6969
public ProblemBuilder instance(String instance) {
70-
return instance(URI.create(instance));
70+
return instance != null ? instance(URI.create(instance)) : instance((URI) null);
7171
}
7272

7373
@Override

src/test/java/io/github/malczuuu/problem4j/core/ProblemBuilderImplTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import java.net.URI;
56
import java.util.Arrays;
67
import java.util.Collection;
78
import java.util.HashMap;
@@ -10,6 +11,20 @@
1011

1112
class ProblemBuilderImplTests {
1213

14+
@Test
15+
void givenNullURIType_shouldNotSetIt() {
16+
Problem problem = Problem.builder().type((URI) null).build();
17+
18+
assertThat(problem.getType()).isEqualTo(Problem.BLANK_TYPE);
19+
}
20+
21+
@Test
22+
void givenNullStringType_shouldNotSetIt() {
23+
Problem problem = Problem.builder().type((String) null).build();
24+
25+
assertThat(problem.getType()).isEqualTo(Problem.BLANK_TYPE);
26+
}
27+
1328
@Test
1429
void givenNullProblemStatus_shouldNotSetTitleOrStatus() {
1530
Problem problem = Problem.builder().status(null).build();
@@ -18,6 +33,20 @@ void givenNullProblemStatus_shouldNotSetTitleOrStatus() {
1833
assertThat(problem.getTitle()).isNull();
1934
}
2035

36+
@Test
37+
void givenNullURIInstance_shouldNotSetIt() {
38+
Problem problem = Problem.builder().instance((URI) null).build();
39+
40+
assertThat(problem.getInstance()).isNull();
41+
}
42+
43+
@Test
44+
void givenNullStringInstance_shouldNotSetIt() {
45+
Problem problem = Problem.builder().instance((String) null).build();
46+
47+
assertThat(problem.getInstance()).isNull();
48+
}
49+
2150
@Test
2251
void givenNullNameExtension_shouldIgnoreIt() {
2352
Problem problem = Problem.builder().extension(null, "value").build();

0 commit comments

Comments
 (0)