Skip to content

Commit

Permalink
fix: handle missing languages gracefully (#92)
Browse files Browse the repository at this point in the history
* fix: handle missing languages gracefully

* fix: skip if unknown
  • Loading branch information
ferhatelmas committed Nov 2, 2022
1 parent 2eb0242 commit 1d02a90
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public Language deserialize(JsonParser jsonParser, DeserializationContext deseri
return null;
}
for (Language enumValue : Language.values()) {
if (enumValue == Language.UNKNOWN) {
continue;
}
try {
if (jsonString.equals(
Language.class.getField(enumValue.name()).getAnnotation(JsonProperty.class).value())) {
Expand All @@ -27,7 +30,6 @@ public Language deserialize(JsonParser jsonParser, DeserializationContext deseri
throw deserializationContext.instantiationException(Language.class, "Should not happen");
}
}
throw deserializationContext.instantiationException(
Language.class, "Unparseable value for Language: " + jsonString);
return Language.UNKNOWN;
}
}
20 changes: 20 additions & 0 deletions src/test/java/io/getstream/chat/java/UserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ void whenParsingLanguage_thenNoException() {
Assertions.assertEquals(lang, Language.FR);
}

@DisplayName("Not defined language is set to unknown")
@Test
void whenParsingNotDefinedLanguage_thenUnknown() {
var id = RandomStringUtils.randomAlphabetic(10);
Assertions.assertDoesNotThrow(
() ->
User.upsert()
.user(
UserRequestObject.builder()
.id(id)
.additionalField("language", "something-unknown")
.build())
.request());

var lang =
Assertions.assertDoesNotThrow(
() -> User.list().filterCondition("id", id).request().getUsers().get(0).getLanguage());
Assertions.assertEquals(lang, Language.UNKNOWN);
}

@DisplayName("Can partial update a user")
@Test
void whenPartiallyUpdatingUser_thenNoException() {
Expand Down

0 comments on commit 1d02a90

Please sign in to comment.