Skip to content

Commit

Permalink
Merge pull request #475 from exadel-inc/EAK-444
Browse files Browse the repository at this point in the history
[EAK-444] Fixed default values for RequestParamInjector
  • Loading branch information
smiakchilo authored Nov 8, 2023
2 parents 4cf564c + d3f5607 commit 84577f0
Show file tree
Hide file tree
Showing 21 changed files with 387 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class CoreConstants {

public static final String PN_APPEND = "append";
public static final String PN_CHECKED = "checked";
public static final String PN_DEFAULT_VALUE = "defaultValue";
public static final String PN_ITEM_RESOURCE_TYPE = "itemResourceType";
public static final String PN_LIMIT = "limit";
public static final String PN_LIST_ITEM = "listItem";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.models.annotations.Default;
import org.apache.sling.models.spi.DisposalCallbackRegistry;
import org.apache.sling.models.spi.Injector;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.exadel.aem.toolkit.core.injectors.utils.CastResult;
import com.exadel.aem.toolkit.core.injectors.utils.CastUtil;

/**
* Represents a base for a Sling injector. A descendant of this class must extract an annotation from a Java class
* member and provide a value that matches the annotation. This value is subsequently assigned to the Java class member
Expand Down Expand Up @@ -72,8 +77,17 @@ public final Object getValue(
if (Objects.isNull(value)) {
logNullValue(element, annotation);
}
return populateDefaultValue(value, type, element);
}

return value;
protected Object populateDefaultValue(Object value, Type type, AnnotatedElement element) {
Object effectiveValue = value instanceof CastResult ? ((CastResult) value).getValue() : value;
boolean isFallback = value instanceof CastResult && ((CastResult) value).isFallback();
if ((effectiveValue == null || isFallback) && element.isAnnotationPresent(Default.class)) {
Object defaultValue = getDefaultValue(element.getDeclaredAnnotation(Default.class));
effectiveValue = CastUtil.toType(defaultValue, type).getValue();
}
return effectiveValue;
}

/**
Expand Down Expand Up @@ -112,4 +126,24 @@ private void logNullValue(AnnotatedElement annotatedElement, T annotation) {
LOG.debug(BRIEF_INJECTION_ERROR_MESSAGE, annotation);
}
}

private Object getDefaultValue(Default defaultAnnotation) {
if (ArrayUtils.isNotEmpty(defaultAnnotation.values())) {
return defaultAnnotation.values();
} else if (ArrayUtils.isNotEmpty(defaultAnnotation.booleanValues())) {
return defaultAnnotation.booleanValues();
} else if (ArrayUtils.isNotEmpty(defaultAnnotation.doubleValues())) {
return defaultAnnotation.doubleValues();
} else if (ArrayUtils.isNotEmpty(defaultAnnotation.floatValues())) {
return defaultAnnotation.floatValues();
} else if (ArrayUtils.isNotEmpty(defaultAnnotation.intValues())) {
return defaultAnnotation.intValues();
} else if (ArrayUtils.isNotEmpty(defaultAnnotation.longValues())) {
return defaultAnnotation.longValues();
} else if (ArrayUtils.isNotEmpty(defaultAnnotation.shortValues())) {
return defaultAnnotation.shortValues();
} else {
return new String[0];
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,4 @@ private String[] getRequestParameterValues(SlingHttpServletRequest request, Stri
.filter(StringUtils::isNotEmpty)
.toArray(String[]::new);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.exadel.aem.toolkit.core.injectors.utils;

public class CastResult {

private final Object value;
private final boolean isFallback;

CastResult(Object value, boolean isFallback) {
this.value = value;
this.isFallback = isFallback;
}

public Object getValue() {
return value;
}

public boolean isFallback() {
return isFallback;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ private CastUtil() {
* @param type A {@link Type} reference; usually reflects the type of Java class member
* @return Transformed value; returns null in case type casting is not possible
*/
public static Object toType(Object value, Type type) {
public static CastResult toType(Object value, Type type) {
return toType(value, type, CastUtil::toInstanceOfType);
}

Expand All @@ -67,27 +67,28 @@ public static Object toType(Object value, Type type) {
* object or primitive, the {@code converter} processes it as is
* @return Transformed value; returns null in case type casting is not possible
*/
public static Object toType(Object value, Type type, BiFunction<Object, Type, Object> converter) {
public static CastResult toType(Object value, Type type, BiFunction<Object, Type, Object> converter) {
if (value == null) {
return null;
}
boolean fallback = value.getClass().isArray() && Array.getLength(value) == 0;

Class<?> elementType = TypeUtil.getElementType(type);

if (TypeUtil.isArray(type)) {
return toArray(value, elementType, converter);
return new CastResult(toArray(value, elementType, converter), fallback);
}

if (TypeUtil.isSupportedCollection(type, true)) {
return Set.class.equals(TypeUtil.getRawType(type))
? toCollection(value, elementType, converter, LinkedHashSet::new)
: toCollection(value, elementType, converter, ArrayList::new);
? new CastResult(toCollection(value, elementType, converter, LinkedHashSet::new), fallback)
: new CastResult(toCollection(value, elementType, converter, ArrayList::new), fallback);
}

if (Object.class.equals(type)) {
return converter.apply(value, type);
return new CastResult(converter.apply(value, type), fallback);
}
return converter.apply(extractFirstElement(value), type);
return new CastResult(converter.apply(extractFirstElement(value), type), fallback);
}

/**
Expand All @@ -108,7 +109,7 @@ private static Object toArray(Object value, Class<?> type, BiFunction<Object, Ty
Object result = Array.newInstance(type, length);
if (value.getClass().isArray()) {
for (int i = 0; i < length; i++) {
Array.set(result, i, toType(Array.get(value, i), type, converter));
Array.set(result, i, toType(Array.get(value, i), type, converter).getValue());
}
} else if (value instanceof Collection) {
int i = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,37 +62,47 @@ public Object getValue(
String effectiveName = StringUtils.defaultIfBlank(requestProperty.name(), name);

if (delegate instanceof RequestAttributeInjector) {
return ((RequestAttributeInjector) delegate).getValue(
RequestAttributeInjector requestAttributeInjector = (RequestAttributeInjector) delegate;
Object value = requestAttributeInjector.getValue(
AdaptationUtil.getRequest(adaptable),
effectiveName,
type);
return requestAttributeInjector.populateDefaultValue(value, type, annotatedElement);
}

if (delegate instanceof RequestParamInjector) {
return ((RequestParamInjector) delegate).getValue(
RequestParamInjector requestParamInjector = (RequestParamInjector) delegate;
Object value = requestParamInjector.getValue(
AdaptationUtil.getRequest(adaptable),
effectiveName,
type);
return requestParamInjector.populateDefaultValue(value, type, annotatedElement);
}

if (delegate instanceof RequestSelectorsInjector) {
return ((RequestSelectorsInjector) delegate).getValue(
RequestSelectorsInjector requestSelectorsInjector = (RequestSelectorsInjector) delegate;
Object value = requestSelectorsInjector.getValue(
AdaptationUtil.getRequest(adaptable),
type);
return requestSelectorsInjector.populateDefaultValue(value, type, annotatedElement);
}

if (delegate instanceof RequestSuffixInjector) {
return ((RequestSuffixInjector) delegate).getValue(
RequestSuffixInjector requestSuffixInjector = (RequestSuffixInjector) delegate;
Object value = requestSuffixInjector.getValue(
AdaptationUtil.getRequest(adaptable),
type);
return requestSuffixInjector.populateDefaultValue(value, type, annotatedElement);
}

if (delegate instanceof EnumValueInjector) {
return ((EnumValueInjector) delegate).getValue(
EnumValueInjector enumValueInjector = (EnumValueInjector) delegate;
Object value = enumValueInjector.getValue(
adaptable,
effectiveName,
StringUtils.EMPTY,
type);
return enumValueInjector.populateDefaultValue(value, type, annotatedElement);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,21 @@ public void shouldInjectString() {
super.shouldInjectString();
}

@Test
public void shouldInjectDefaultString() {
super.shouldInjectDefaultString();
}

@Test
public void shouldInjectStringArray() {
super.shouldInjectStringArray();
}

@Test
public void shouldInjectDefaultStringArray() {
super.shouldInjectDefaultStringArray();
}

@Test
public void shouldInjectStringCollection() {
super.shouldInjectStringCollection();
Expand All @@ -84,11 +94,21 @@ public void shouldInjectInteger() {
super.shouldInjectInteger(RequestAttributeInjectorTest::assertStringifiedObjectValueEquals);
}

@Test
public void shouldInjectDefaultInteger() {
super.shouldInjectDefaultInteger();
}

@Test
public void shouldInjectIntegerArray() {
super.shouldInjectIntegerArray();
}

@Test
public void shouldInjectDefaultIntegerArray() {
super.shouldInjectDefaultIntegerArray();
}

@Test
public void shouldInjectIntegerCollection() {
super.shouldInjectIntegerCollection();
Expand All @@ -104,11 +124,21 @@ public void shouldInjectLong() {
super.shouldInjectLong(RequestAttributeInjectorTest::assertStringifiedObjectValueEquals);
}

@Test
public void shouldInjectDefaultLong() {
super.shouldInjectDefaultLong();
}

@Test
public void shouldInjectLongArray() {
super.shouldInjectLongArray();
}

@Test
public void shouldInjectDefaultLongArray() {
super.shouldInjectDefaultLongArray();
}

@Test
public void shouldInjectLongCollection() {
super.shouldInjectLongCollection();
Expand All @@ -124,11 +154,21 @@ public void shouldInjectDouble() {
super.shouldInjectDouble(RequestAttributeInjectorTest::assertStringifiedObjectValueEquals);
}

@Test
public void shouldInjectDefaultDouble() {
super.shouldInjectDefaultDouble();
}

@Test
public void shouldInjectDoubleArray() {
super.shouldInjectDoubleArray();
}

@Test
public void shouldInjectDefaultDoubleArray() {
super.shouldInjectDefaultDoubleArray();
}

@Test
public void shouldInjectDoubleCollection() {
super.shouldInjectDoubleCollection();
Expand All @@ -144,11 +184,21 @@ public void shouldInjectBoolean() {
super.shouldInjectBoolean();
}

@Test
public void shouldInjectDefaultBoolean() {
super.shouldInjectDefaultBoolean();
}

@Test
public void shouldInjectBooleanArray() {
super.shouldInjectBooleanArray();
}

@Test
public void shouldInjectDefaultBooleanArray() {
super.shouldInjectDefaultBooleanArray();
}

@Test
public void shouldInjectBooleanCollection() {
super.shouldInjectBooleanCollection();
Expand Down
Loading

0 comments on commit 84577f0

Please sign in to comment.