Skip to content

Commit 790f359

Browse files
Serialize Annotation object with all required fields even empty (#110)
* Serialize Annotation object with all required fields even empty Having JsonInclude:NON_EMPTY removes empty fields (e.g., "") When the required fields are removed, API calls fail due to missing fields in the request. Let's use less restrictive JsonInclude:NON_ABSENT annotation, which removes only null fields. Required fields are anyway non-nullable.
1 parent d0c05a4 commit 790f359

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

src/main/java/com/spotify/github/v3/checks/Annotation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
@Value.Immutable
4444
@GithubStyle
4545
@JsonDeserialize(as = ImmutableAnnotation.class)
46-
@JsonInclude(JsonInclude.Include.NON_EMPTY)
46+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
4747
public interface Annotation {
4848

4949
/**

src/test/java/com/spotify/github/v3/checks/AnnotationTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@
2424
import static org.hamcrest.MatcherAssert.assertThat;
2525
import static org.junit.jupiter.api.Assertions.assertThrows;
2626

27-
import com.spotify.github.FixtureHelper;
2827
import com.spotify.github.jackson.Json;
2928
import com.spotify.github.v3.checks.ImmutableAnnotation.Builder;
30-
import java.io.IOException;
31-
import java.time.ZonedDateTime;
3229
import org.junit.Test;
3330

3431
public class AnnotationTest {
@@ -66,4 +63,20 @@ public void failsCreationWhenMaxLengthExceeded(){
6663
builder().rawDetails("a".repeat(66000)).build()
6764
);
6865
}
66+
67+
@Test
68+
public void serializesWithEmptyFields() {
69+
Annotation annotationWithEmptyStringFields = ImmutableAnnotation.builder()
70+
.message("")
71+
.path("")
72+
.title("")
73+
.startLine(1)
74+
.endLine(2)
75+
.annotationLevel(AnnotationLevel.notice)
76+
.build();
77+
78+
String serializedAnnotation = Json.create().toJsonUnchecked(annotationWithEmptyStringFields);
79+
String expected = "{\"path\":\"\",\"annotation_level\":\"notice\",\"message\":\"\",\"title\":\"\",\"start_line\":1,\"end_line\":2}";
80+
assertThat(serializedAnnotation, is(expected));
81+
}
6982
}

0 commit comments

Comments
 (0)