This is an automated email from the ASF dual-hosted git repository. jsorel pushed a commit to branch geoapi-4.0 in repository https://gitbox.apache.org/repos/asf/sis.git
The following commit(s) were added to refs/heads/geoapi-4.0 by this push: new f07143ab6b fix(FeatureQuery): fix a case where query projection returned the wrong property name f07143ab6b is described below commit f07143ab6be64f69e027040c35287fd1786ff69c Author: jsorel <johann.so...@geomatys.com> AuthorDate: Fri Sep 29 10:57:22 2023 +0200 fix(FeatureQuery): fix a case where query projection returned the wrong property name --- .../main/org/apache/sis/storage/FeatureQuery.java | 8 +++++++ .../org/apache/sis/storage/FeatureQueryTest.java | 27 ++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/FeatureQuery.java b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/FeatureQuery.java index 36ab2fcb16..d0d8f9c218 100644 --- a/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/FeatureQuery.java +++ b/endorsed/src/org.apache.sis.storage/main/org/apache/sis/storage/FeatureQuery.java @@ -748,6 +748,14 @@ public class FeatureQuery extends Query implements Cloneable, Serializable { expression.getFunctionName().toInternationalString(), column)); } GenericName name = item.alias; + if (name == null && expression instanceof ValueReference<?,?>) { + /* + * If we do not have an alias, use the original property name. + * This name may be different from the resultType name because of links or functions. + */ + name = valueType.getProperty(((ValueReference<?, ?>) expression).getXPath()).getName(); + } + if (name == null) { /* * Build a list of aliases declared by the user, for making sure that we do not collide with them. diff --git a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/FeatureQueryTest.java b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/FeatureQueryTest.java index f28eb32fdf..4dea8a07ca 100644 --- a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/FeatureQueryTest.java +++ b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/FeatureQueryTest.java @@ -22,6 +22,8 @@ import java.util.Iterator; import java.util.stream.Collectors; import org.apache.sis.feature.Features; import org.apache.sis.feature.builder.FeatureTypeBuilder; +import org.apache.sis.feature.builder.AttributeRole; +import org.apache.sis.feature.internal.AttributeConvention; import org.apache.sis.storage.base.MemoryFeatureSet; import org.apache.sis.filter.DefaultFilterFactory; import org.apache.sis.util.iso.Names; @@ -327,6 +329,31 @@ public final class FeatureQueryTest extends TestCase { assertEquals("value3", 25, instance.getPropertyValue("value3")); } + /** + * Tests {@link FeatureQuery#setProjection(FeatureQuery.NamedExpression...)} on a field + * which is a link, ensure the link name is preserved. + * + * @throws DataStoreException if an error occurred while executing the query. + */ + @Test + public void testProjectionOfLink() throws DataStoreException { + + final FeatureTypeBuilder ftb = new FeatureTypeBuilder(); + ftb.setName("test"); + ftb.addAttribute(String.class).setName("id").addRole(AttributeRole.IDENTIFIER_COMPONENT); + FeatureType ft = ftb.build(); + + Feature feature = ft.newInstance(); + feature.setPropertyValue("id", "id-0"); + + final FeatureQuery query = new FeatureQuery(); + query.setProjection(AttributeConvention.IDENTIFIER); + + final FeatureSet fs = new MemoryFeatureSet(null, ft, List.of(feature)); + Feature r = fs.subset(query).features(true).iterator().next(); + assertEquals("id-0", r.getPropertyValue(AttributeConvention.IDENTIFIER)); + } + /** * Shortcut for creating expression for a projection computed on-the-fly. */