Skip to content
Permalink
Browse files

[Expression] Do not transform NULL integer as 0 when concat

Fixes #36112 : don't concat null values

(cherry picked from commit 01563e6)
  • Loading branch information
troopa81 authored and nyalldawson committed May 18, 2020
1 parent 5c42904 commit 440a7e353671d921409ba4879564f3183eca1829
Showing with 27 additions and 1 deletion.
  1. +2 −1 src/core/expression/qgsexpressionfunction.cpp
  2. +25 −0 tests/src/core/testqgsexpression.cpp
@@ -1686,7 +1686,8 @@ static QVariant fcnConcat( const QVariantList &values, const QgsExpressionContex
QString concat;
for ( const QVariant &value : values )
{
concat += QgsExpressionUtils::getStringValue( value, parent );
if ( !value.isNull() )
concat += QgsExpressionUtils::getStringValue( value, parent );
}
return concat;
}
@@ -3624,6 +3624,31 @@ class TestQgsExpression: public QObject
QCOMPARE( QgsExpression::replaceExpressionText( input, &context ), expected );
}

void testConcatNULLAttributeValue()
{
// Test that null integer values coming from provider are not transformed as 0
// https://github.com/qgis/QGIS/issues/36112

QgsFields fields;
fields.append( QgsField( QStringLiteral( "foo" ), QVariant::Int ) );

QgsFeature f;
f.initAttributes( 1 );
f.setAttribute( 0, QVariant( QVariant::Int ) );

QgsExpressionContext context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
QgsExpression exp( QStringLiteral( "concat('test', foo)" ) );
QVariant res = exp.evaluate( &context );
QCOMPARE( res.type(), QVariant::String );
QCOMPARE( res.toString(), QStringLiteral( "test" ) );

f.setAttribute( 0, QVariant() );
context = QgsExpressionContextUtils::createFeatureBasedContext( f, fields );
res = exp.evaluate( &context );
QCOMPARE( res.type(), QVariant::String );
QCOMPARE( res.toString(), QStringLiteral( "test" ) );
}

};

QGSTEST_MAIN( TestQgsExpression )

0 comments on commit 440a7e3

Please sign in to comment.
You can’t perform that action at this time.