Skip to content

Commit

Permalink
ISSUE 864 Fix primitive id field RSQL filter (#866)
Browse files Browse the repository at this point in the history
* ISSUE 864 Fix primitive id field RSQL filter

remove files

* Update RSQLFilterDialectTest.java
  • Loading branch information
hellohanchen authored and aklish committed Oct 3, 2019
1 parent 0f354bd commit 0442484
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.yahoo.elide.core.filter.dialect;

import static com.yahoo.elide.core.EntityDictionary.REGULAR_ID_NAME;
import static com.yahoo.elide.utils.TypeHelper.isPrimitiveNumberType;

import com.yahoo.elide.core.EntityDictionary;
import com.yahoo.elide.core.Path;
Expand Down Expand Up @@ -299,7 +300,7 @@ public FilterExpression visit(ComparisonNode node, Class entityType) {
//Coerce arguments to their correct types
List<Object> values = arguments.stream()
.map(argument ->
Number.class.isAssignableFrom(relationshipType)
isPrimitiveNumberType(relationshipType) || Number.class.isAssignableFrom(relationshipType)
? argument.replace("*", "") //Support filtering on number types
: argument
)
Expand Down
29 changes: 29 additions & 0 deletions elide-core/src/main/java/com/yahoo/elide/utils/TypeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2019, Oath Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/

package com.yahoo.elide.utils;

import com.google.common.collect.Sets;

import java.util.Set;

/**
* Utilities for checking classes and primitive types.
*/
public class TypeHelper {
private static final Set<Class<?>> PRIMITIVE_NUMBER_TYPES = Sets
.newHashSet(short.class, int.class, long.class, float.class, double.class);

/**
* Determine whether a type is primitive number type
*
* @param type type to check
* @return True is the type is primitive number type
*/
public static boolean isPrimitiveNumberType(Class<?> type) {
return PRIMITIVE_NUMBER_TYPES.contains(type);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import example.Author;
import example.Book;
import example.Job;
import example.PrimitiveId;
import example.StringId;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand All @@ -38,6 +39,7 @@ public static void init() {
dictionary.bindEntity(Book.class);
dictionary.bindEntity(StringId.class);
dictionary.bindEntity(Job.class);
dictionary.bindEntity(PrimitiveId.class);
dialect = new RSQLFilterDialect(dictionary);
}

Expand Down Expand Up @@ -291,4 +293,20 @@ public void testFilterOnCustomizedStringIdField() throws ParseException {

assertEquals("stringId.surrogateKey INFIX_CASE_INSENSITIVE [identifier]", expression.toString());
}

@Test
public void testInfixFilterOnPrimitiveIdField() throws ParseException {
MultivaluedMap<String, String> queryParams = new MultivaluedHashMap<>();

queryParams.add(
"filter",
"id==*1*"
);

FilterExpression expression = dialect.parseGlobalExpression("/primitiveTypeId", queryParams);

assertEquals(expression.toString(),
"primitiveId.primitiveId INFIX_CASE_INSENSITIVE [1]"
);
}
}
22 changes: 22 additions & 0 deletions elide-core/src/test/java/example/PrimitiveId.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2015, Yahoo Inc.
* Licensed under the Apache License, Version 2.0
* See LICENSE file in project root for terms.
*/
package example;

import com.yahoo.elide.annotation.Include;
import lombok.Data;

import javax.persistence.Entity;
import javax.persistence.Id;


@Include(rootLevel = true, type = "primitiveTypeId")
@Entity
@Data
public class PrimitiveId {

@Id
private long primitiveId;
}

0 comments on commit 0442484

Please sign in to comment.