Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: proper error message on attempt to set single value into multi-v… #214

Merged
merged 1 commit into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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