Skip to content

Commit

Permalink
Fix #5460: regression after fixing #5417: the composite component
Browse files Browse the repository at this point in the history
attribute default value wasn't anymore available when explicitly calling
getAttributes().get(name) rather than relying on statehelper -- this has
been fixed
  • Loading branch information
BalusC committed Jul 27, 2024
1 parent 4c60acb commit ecd652c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -828,25 +828,6 @@ private void pushDeclaredDefaultValuesToAttributesMap(FacesContext context, Bean
}
}

/**
* Helper method to convert a value to a type as defined in PropertyDescriptor(s)
*
* @param name
* @param value
* @param propertyDescriptors
* @return value
*/
private Object convertValueToTypeIfNecessary(String name, Object value, PropertyDescriptor[] propertyDescriptors, ExpressionFactory expressionFactory) {
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
if (propertyDescriptor.getName().equals(name)) {
value = expressionFactory.coerceToType(value, propertyDescriptor.getPropertyType());
break;
}
}

return value;
}

/**
* <p>
* To enable EL Coercion to use Faces Custom converters, this method will call
Expand Down
28 changes: 27 additions & 1 deletion impl/src/main/java/jakarta/faces/component/UIComponentBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import static java.util.Collections.unmodifiableList;
import static java.util.logging.Level.FINE;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.io.IOException;
Expand Down Expand Up @@ -1978,10 +1979,35 @@ public Object get(Object keyObj) {
}
}
}

if (result == null && attributes != null && isCompositeComponent(component)) {
result = getCompositeComponentAttributeDefaultValue(key, attributes);
}
return result;
}

private Object getCompositeComponentAttributeDefaultValue(String name, Map<String, Object> attributes) {
var metadata = (BeanInfo) attributes.get(UIComponent.BEANINFO_KEY);

if (metadata != null) {
for (var propertyDescriptor : metadata.getPropertyDescriptors()) {
if (propertyDescriptor.getName().equals(name)) {
var defaultValue = propertyDescriptor.getValue("default");

if (defaultValue != null) {
if (defaultValue instanceof ValueExpression) {
defaultValue = ((ValueExpression) defaultValue).getValue(component.getFacesContext().getELContext());
}

var expressionFactory = component.getFacesContext().getApplication().getExpressionFactory();
return expressionFactory.coerceToType(defaultValue, propertyDescriptor.getPropertyType());
}
}
}
}

return null;
}

@Override
public Object put(String keyValue, Object value) {
if (keyValue == null) {
Expand Down

0 comments on commit ecd652c

Please sign in to comment.