Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ISSUE 864 Fix primitive id field RSQL filter #866

Merged
merged 7 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}