Skip to content

Commit

Permalink
fix: proper error message on attempt to set single value into multi-v…
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumas committed Oct 31, 2024
1 parent 1ac12d2 commit 972b938
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,15 +215,23 @@ public void setFieldValue(@NotNull IPObject ipObject, @NotNull String fieldId, O
FieldMetadata.fromPrototype(ipObject.getPrototype(), fieldId) : FieldMetadata.fromCustomField(ipObject.getCustomFieldPrototype(fieldId));
Object valueToSet = value == null ? null : Optional.ofNullable(IConverter.getSuitableConverter(value, fieldMetadata.getType()))
.map(converter -> converter.convert(value, ConverterContext.builder().contextId(ipObject.getContextId()).enumsMapping(enumsMapping).build(), fieldMetadata)).orElse(value);
boolean multiValueField = fieldMetadata.getType() instanceof IListType;
if (valueToSet == null) {
if (fieldMetadata.isRequired()) {
throw new IllegalArgumentException("Cannot set empty value to the required field");
} else if (fieldMetadata.getType() instanceof IListType) {
} else if (multiValueField) {
valueToSet = new ArrayList<>(); //Multi-value fields expect empty list in case of empty value
}
}

if (valueToSet instanceof List<?> valuesList) {
valueToSet = new CustomTypedList(ipObject, (IListType) fieldMetadata.getType(), fieldMetadata.isRequired(), valuesList);
if (multiValueField) {
valueToSet = new CustomTypedList(ipObject, (IListType) fieldMetadata.getType(), fieldMetadata.isRequired(), valuesList);
} else {
throw new IllegalArgumentException("Cannot set multi-value into field '%s'".formatted(fieldId));
}
} else if (multiValueField) {
throw new IllegalArgumentException("Cannot set single value to the multi-value field '%s'".formatted(fieldId));
}
if (fieldMetadata.isCustom()) {
ipObject.setCustomField(fieldId, valueToSet);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -324,15 +324,21 @@ void testSetFieldValue() {
List<String> listValue = List.of("value1", "value2");

// primitive fields cannot receive lists
assertThrows(ClassCastException.class,
IllegalArgumentException multiValueException = assertThrows(IllegalArgumentException.class,
() -> polarionService.setFieldValue(workItem, "genericFieldId", listValue, enumsMapping));
assertEquals("Cannot set multi-value into field 'genericFieldId'", multiValueException.getMessage());

// lists must be supported
genericFieldMetadata.setType(new ListType("listTypeId", FieldType.STRING.getType()));
polarionService.setFieldValue(workItem, "genericFieldId", listValue, enumsMapping);
verify(workItem, times(3)).setValue(anyString(), any());
verify(workItem, times(1)).setCustomField(anyString(), any());

// multi fields cannot receive single values
IllegalArgumentException singleValueException = assertThrows(IllegalArgumentException.class,
() -> polarionService.setFieldValue(workItem, "genericFieldId", 42, enumsMapping));
assertEquals("Cannot set single value to the multi-value field 'genericFieldId'", singleValueException.getMessage());

// nulls for list-typed fields must be converted to an empty list
polarionService.setFieldValue(workItem, "genericFieldId", null, enumsMapping);
verify(workItem, times(1)).setValue("genericFieldId", new ArrayList<>());
Expand Down

0 comments on commit 972b938

Please sign in to comment.