Skip to content
Draft
Show file tree
Hide file tree
Changes from 16 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 @@ -11,6 +11,7 @@
import io.jenkins.plugins.casc.impl.attributes.DescribableAttribute;
import io.jenkins.plugins.casc.model.CNode;
import io.jenkins.plugins.casc.model.Mapping;
import java.lang.reflect.Array;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
Expand Down Expand Up @@ -301,8 +302,40 @@ public CNode describe(T instance, ConfigurationContext context) throws Exception
if (a != null) {
Object value = a.getValue(instance);
if (value != null) {
Object converted = Stapler.CONVERT_UTILS.convert(value, a.getType());
if (converted instanceof Collection || p.getType().isArray() || !a.isMultiple()) {
Class<?> targetType = a.getType();
Object converted;

if (a.isMultiple() && value instanceof Collection) {
List<Object> list = new ArrayList<>();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no coverage of this.

Can you please run the coverage locally?

mvnd clean verify jacoco:report -P enable-jacoco

# then
open plugin/target/site/jacoco/index.html

here's a screenshot of the current state:
image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I'm working on adding tests to get those exact lines covered, and thanks for sharing that local coverage command that is going to make testing this locally much easier

for (Object o : (Collection<?>) value) {
list.add(Stapler.CONVERT_UTILS.convert(o, targetType));
}
converted = list;
} else if (a.isMultiple() && value.getClass().isArray()) {
List<Object> list = new ArrayList<>();
int len = Array.getLength(value);
for (int j = 0; j < len; j++) {
list.add(Stapler.CONVERT_UTILS.convert(Array.get(value, j), targetType));
}
converted = list;
} else {
converted = Stapler.CONVERT_UTILS.convert(value, targetType);
}

if (p.getType().isArray() && converted instanceof Collection<?> col) {
Class<?> component = p.getType().getComponentType();
Object array = Array.newInstance(component, col.size());
int idx = 0;
for (Object o : col) {
Array.set(array, idx++, o);
}
args[i] = array;

} else if (Set.class.isAssignableFrom(p.getType()) && converted instanceof Collection) {
args[i] = new HashSet<>((Collection<?>) converted);
} else if (converted instanceof Collection
|| (converted != null && converted.getClass().isArray())
|| !a.isMultiple()) {
args[i] = converted;
} else if (Set.class.isAssignableFrom(p.getType())) {
args[i] = Collections.singleton(converted);
Expand Down
Loading
Loading