From 8ab0471d8aa478b72789739d963fb431ebadcef3 Mon Sep 17 00:00:00 2001 From: Bauke Scholtz Date: Sat, 17 Dec 2022 08:07:30 -0400 Subject: [PATCH] https://github.com/eclipse-ee4j/mojarra/issues/5190 Add EmptyStringToNullELResolver to work around a misinterpreted change in EL3 spec --- .../main/java/com/sun/faces/el/ELUtils.java | 9 +++ .../faces/el/EmptyStringToNullELResolver.java | 66 +++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 impl/src/main/java/com/sun/faces/el/EmptyStringToNullELResolver.java diff --git a/impl/src/main/java/com/sun/faces/el/ELUtils.java b/impl/src/main/java/com/sun/faces/el/ELUtils.java index 0acbac12c2..17468e5f1d 100644 --- a/impl/src/main/java/com/sun/faces/el/ELUtils.java +++ b/impl/src/main/java/com/sun/faces/el/ELUtils.java @@ -18,6 +18,7 @@ import static com.sun.faces.RIConstants.EMPTY_CLASS_ARGS; import static com.sun.faces.cdi.CdiUtils.getBeanReference; +import static com.sun.faces.config.WebConfiguration.BooleanWebContextInitParameter.InterpretEmptyStringSubmittedValuesAsNull; import static com.sun.faces.el.FacesCompositeELResolver.ELResolverChainType.Faces; import static com.sun.faces.el.FacesCompositeELResolver.ELResolverChainType.JSP; import static com.sun.faces.util.MessageUtils.NULL_PARAMETERS_ERROR_MESSAGE_ID; @@ -62,6 +63,7 @@ import com.sun.faces.application.ApplicationAssociate; import com.sun.faces.cdi.CdiExtension; +import com.sun.faces.config.WebConfiguration; import com.sun.faces.context.flash.FlashELResolver; import com.sun.faces.mgbean.BeanManager; import com.sun.faces.util.MessageUtils; @@ -178,6 +180,8 @@ public String toString() { public static final ListELResolver LIST_RESOLVER = new ListELResolver(); + public static final EmptyStringToNullELResolver EMPTY_STRING_TO_NULL_RESOLVER = new EmptyStringToNullELResolver(); + public static final ManagedBeanELResolver MANAGED_BEAN_RESOLVER = new ManagedBeanELResolver(); @@ -266,6 +270,11 @@ public static void buildFacesResolver(FacesCompositeELResolver composite, Applic addVariableResolvers(composite, Faces, associate); addPropertyResolvers(composite, associate); composite.add(associate.getApplicationELResolvers()); + + if (WebConfiguration.getInstance().isOptionEnabled(InterpretEmptyStringSubmittedValuesAsNull)) { + composite.addPropertyELResolver(EMPTY_STRING_TO_NULL_RESOLVER); + } + composite.addRootELResolver(MANAGED_BEAN_RESOLVER); composite.addPropertyELResolver(RESOURCE_RESOLVER); composite.addPropertyELResolver(BUNDLE_RESOLVER); diff --git a/impl/src/main/java/com/sun/faces/el/EmptyStringToNullELResolver.java b/impl/src/main/java/com/sun/faces/el/EmptyStringToNullELResolver.java new file mode 100644 index 0000000000..abd71087e5 --- /dev/null +++ b/impl/src/main/java/com/sun/faces/el/EmptyStringToNullELResolver.java @@ -0,0 +1,66 @@ +/* + * Copyright (c) 1997, 2022 Oracle and/or its affiliates. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v. 2.0, which is available at + * http://www.eclipse.org/legal/epl-2.0. + * + * This Source Code may also be made available under the following Secondary + * Licenses when the conditions for such availability set forth in the + * Eclipse Public License v. 2.0 are satisfied: GNU General Public License, + * version 2 with the GNU Classpath Exception, which is available at + * https://www.gnu.org/software/classpath/license.html. + * + * SPDX-License-Identifier: EPL-2.0 OR GPL-2.0 WITH Classpath-exception-2.0 + */ + +package com.sun.faces.el; + +import java.beans.FeatureDescriptor; +import java.util.Iterator; + +import javax.el.ELContext; +import javax.el.ELResolver; + +public class EmptyStringToNullELResolver extends ELResolver { + + @Override + public Class getCommonPropertyType(ELContext context, Object base) { + return String.class; + } + + @Override + public Object convertToType(ELContext context, Object value, Class targetType) { + if (value == null && targetType == String.class) { + context.setPropertyResolved(true); + } + + return value; + } + + @Override + public Iterator getFeatureDescriptors(ELContext context, Object base) { + return null; + } + + @Override + public Class getType(ELContext context, Object base, Object property) { + return null; + } + + @Override + public Object getValue(ELContext context, Object base, Object property) { + return null; + } + + @Override + public boolean isReadOnly(ELContext context, Object base, Object property) { + return true; + } + + @Override + public void setValue(ELContext context, Object base, Object property, Object value) { + // NOOP. + } + +} \ No newline at end of file