Skip to content

Commit

Permalink
Don't require empty fields in the JSON serialization of TextControlDe…
Browse files Browse the repository at this point in the history
…scriptor
  • Loading branch information
matthewhorridge committed Jul 2, 2024
1 parent 733a36c commit 7746420
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import edu.stanford.protege.webprotege.common.LanguageMap;
import edu.stanford.protege.webprotege.forms.PropertyNames;

import javax.annotation.Nonnull;
import javax.annotation.*;

import static com.google.common.base.MoreObjects.toStringHelper;
import static com.google.common.base.Preconditions.checkNotNull;
Expand All @@ -16,6 +16,7 @@
* 30/03/16
*/
@JsonTypeName(TextControlDescriptor.TYPE)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class TextControlDescriptor implements FormControlDescriptor {

protected static final String TYPE = "TEXT";
Expand All @@ -36,18 +37,18 @@ private TextControlDescriptor() {
}

@JsonCreator
public TextControlDescriptor(@JsonProperty(PropertyNames.PLACEHOLDER) @Nonnull LanguageMap placeholder,
public TextControlDescriptor(@JsonProperty(PropertyNames.PLACEHOLDER) @Nullable LanguageMap placeholder,
@JsonProperty(PropertyNames.STRING_TYPE) @Nonnull StringType stringType,
@JsonProperty(PropertyNames.SPECIFIC_LANG_TAG) @Nonnull String specificLangTag,
@JsonProperty(PropertyNames.SPECIFIC_LANG_TAG) @Nullable String specificLangTag,
@JsonProperty(PropertyNames.LINE_MODE) @Nonnull LineMode lineMode,
@JsonProperty(PropertyNames.PATTERN) @Nonnull String pattern,
@JsonProperty(PropertyNames.PATTERN_VIOLATION_ERROR_MESSAGE) @Nonnull LanguageMap patternViolationErrorMessage) {
this.placeholder = checkNotNull(placeholder);
@JsonProperty(PropertyNames.PATTERN) @Nullable String pattern,
@JsonProperty(PropertyNames.PATTERN_VIOLATION_ERROR_MESSAGE) @Nullable LanguageMap patternViolationErrorMessage) {
this.placeholder = placeholder != null ? placeholder : LanguageMap.empty();
this.stringType = checkNotNull(stringType);
this.specificLangTag = checkNotNull(specificLangTag);
this.specificLangTag = specificLangTag != null ? specificLangTag : "";
this.lineMode = checkNotNull(lineMode);
this.pattern = checkNotNull(pattern);
this.patternViolationErrorMessage = checkNotNull(patternViolationErrorMessage);
this.pattern = pattern != null ? pattern : "";
this.patternViolationErrorMessage = patternViolationErrorMessage != null ? patternViolationErrorMessage : LanguageMap.empty();
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package edu.stanford.protege.webprotege.forms.data;

import com.fasterxml.jackson.databind.ObjectMapper;
import edu.stanford.protege.webprotege.forms.field.TextControlDescriptor;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.json.JacksonTester;

import java.io.IOException;

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

class TextControlDataTest {

private JacksonTester<TextControlData> tester;

@BeforeEach
void setUp() {
JacksonTester.initFields(this, new ObjectMapper());
}

@Test
void shouldSerialize() throws IOException {
var data = TextControlData.get(TextControlDescriptor.getDefault(),
null
);
var written = tester.write(data);
System.out.println(written.getJson());
assertThat(written).hasJsonPathMapValue("value");
assertThat(written).hasJsonPathMapValue("field");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.springframework.boot.test.autoconfigure.json.*;
import org.springframework.boot.test.json.JacksonTester;

import java.io.IOException;
import java.io.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
Expand All @@ -30,9 +30,43 @@ public void shouldSerialize() throws IOException {
var written = tester.write(descriptor);
System.out.println(written.getJson());
assertThat(written).hasJsonPathValue("placeholder");
assertThat(written).hasJsonPathStringValue("pattern", "Pattern");
assertThat(written).hasJsonPathStringValue("stringType", "SPECIFIC_LANG_STRING");
assertThat(written).hasJsonPathStringValue("specificLangTag", "en");
assertThat(written).hasJsonPathStringValue("lineMode", "SINGLE_LINE");
assertThat(written).hasJsonPathValue("patternViolationErrorMessage");
}

@Test
void shouldNotSerializeEmpties() throws IOException {
var descriptor = new TextControlDescriptor(LanguageMap.empty(),
StringType.SPECIFIC_LANG_STRING,
"",
LineMode.SINGLE_LINE,
"",
LanguageMap.empty());
var written = tester.write(descriptor);
System.out.println(written.getJson());
assertThat(written).doesNotHaveJsonPath("placeholder");
assertThat(written).doesNotHaveJsonPath("pattern");
assertThat(written).doesNotHaveJsonPath("specificLangTag");
assertThat(written).doesNotHaveJsonPath("patternViolationErrorMessage");
}

@Test
void shouldDeserializeWithNonIncludedEmpties() throws IOException {
var json = """
{
"@type": "TEXT",
"stringType": "LANG_STRING",
"lineMode": "SINGLE_LINE"
}
""";
var read = tester.read(new StringReader(json));
assertThat(read.getObject().getPattern()).isEmpty();
assertThat(read.getObject().getSpecificLangTag()).isEmpty();
assertThat(read.getObject().getPlaceholder().asMap()).isEmpty();
assertThat(read.getObject().getStringType()).isEqualTo(StringType.LANG_STRING);
assertThat(read.getObject().getLineMode()).isEqualTo(LineMode.SINGLE_LINE);
}
}

0 comments on commit 7746420

Please sign in to comment.