tracker issue : CF-4204499

select a category, or use search below
(searches all categories and all time range)
Title:

<cfproperty> default property does not support non-simple values or expressions

| View in Tracker

Status/Resolution/Reason: To Fix//BugVerified

Reporter/Name(from Bugbase): Mosh T. / ()

Created: 06/11/2019

Components: Language

Versions: 2016

Failure Type: Incorrect w/Workaround

Found In Build/Fixed In Build: 2016.0.06.308055 /

Priority/Frequency: Normal / All users will encounter

Locale/System: English / Windows 10 64 bit

Vote Count: 0

Problem Description:

The "default" property of the <cfproperty> tag does not support values that are non-simple or that are expressions.  So "abc" is fine but now(), {}, and [] are not.  Even worse, when the default value is set to an empty struct or array, instead of throwing an error, CF simply sets the default values as "{}" and "[]" (i.e., strings of the braces instead of a struct or an array).

Steps to Reproduce:

obj.cfc
=====
<cfcomponent displayName="obj" accessors="true">

	<!--- These require initialization of a default value --->
	<cfproperty name="isArray" type="array" required="false" />
	<cfproperty name="isStruct" type="struct" required="false" />

	<!--- These set the default value to a string ("[]" and "{}") instead of an empty object --->
	<cfproperty name="shouldBeArray" type="array" default=[] required="false" />
	<cfproperty name="shouldBeStruct" type="struct" default={} required="false" />

	<!---
		These cause a CF error:
		Unable to find an operator implementation for coldfusion.compiler.ASTArrayInitializer and
		Unable to find an operator implementation for coldfusion.compiler.ASTStructInitializer
	--->
	<!--- <cfproperty name="shouldAlsoBeArray" type="array" default="#[]#" required="false" /> --->
	<!--- <cfproperty name="shouldAlsoBeStruct" type="struct" default="#{}#" required="false" /> --->


	<cffunction name="init" returnType="obj" access="public">

		<cfset this.setIsArray([]) />
		<cfset this.setIsStruct({}) />

		<cfreturn this />

	</cffunction>

</cfcomponent>

index.cfm
========
<cfset obj = createObject("component", "obj").init() />
<cfdump var="#obj#" />
<cfoutput>
	isArray: #isArray( obj.getIsArray() )#<br />
	isStruct: #isStruct( obj.getIsStruct() )#<br />
	shouldBeArray: #isArray( obj.getShouldBeArray() )#<br />
	shouldBeStruct: #isStruct( obj.getShouldBeStruct() )#<br />
</cfoutput>

Actual Result:

An error or strings of the braces.

Expected Result:

A struct or an array.

Any Workarounds:

CFCs must have an init() function that sets the default values for those properties.

Attachments:

Comments: