Skip to content

Commit 8f410a3

Browse files
committed
Add sanity checks for extension keys
1 parent 874ea3b commit 8f410a3

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,15 @@ static ProblemBuilder builder() {
4242
/**
4343
* Creates a named extension for use in a {@link Problem}.
4444
*
45-
* @param key the extension key
45+
* @param key the extension key, must not be {@code null}
4646
* @param value the extension value
4747
* @return a new {@link Extension} instance
48+
* @throws IllegalArgumentException if the {@code key} is {@code null}
4849
*/
4950
static Extension extension(String key, Object value) {
51+
if (key == null) {
52+
throw new IllegalArgumentException("key cannot be null");
53+
}
5054
return new ProblemImpl.ExtensionImpl(key, value);
5155
}
5256

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package io.github.malczuuu.problem4j.core;
22

33
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
45

56
import java.net.URI;
67
import java.util.Arrays;
@@ -48,4 +49,19 @@ void givenProblem_whenToBuilder_shouldBeAbleToRecreateOriginal() {
4849
assertThat(problem).isNotSameAs(copy);
4950
assertThat(problem).isEqualTo(copy);
5051
}
52+
53+
@Test
54+
void givenProblemExtensionWithNullKey_shouldThrowIllegalArgumentException() {
55+
assertThatThrownBy(() -> Problem.extension(null, "v"))
56+
.isInstanceOf(IllegalArgumentException.class)
57+
.hasMessage("key cannot be null");
58+
}
59+
60+
@Test
61+
void givenProblemExtensionWithKey_shouldCreateExtension() {
62+
Problem.Extension ext = Problem.extension("code", 123);
63+
64+
assertThat(ext.getKey()).isEqualTo("code");
65+
assertThat(ext.getValue()).isEqualTo(123);
66+
}
5167
}

0 commit comments

Comments
 (0)