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

Fix Internal Server Error When Filtering by String Column but the value can be interpreted as another type #36

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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 @@ -10,8 +10,7 @@ import java.sql.{Connection, Date, PreparedStatement, ResultSet}
import java.time.LocalDate
import java.util.UUID
import scala.collection.mutable.ListBuffer
import scala.util.Try

import scala.util.{Failure, Success, Try}
object DatabaseTablesDAO {

def all(schema: String = "public")(implicit conn: Connection): List[DatabaseTable] = {
Expand Down Expand Up @@ -73,6 +72,41 @@ object DatabaseTablesDAO {
""".as(foreignKeyParser.*)
}

private def columnTypeIsDouble(columnType: String): Boolean = {
// 'contains' is used because PostgreSQL types may include additional details like precision or scale
// https://www.postgresql.org/docs/8.1/datatype.html
List("float", "decimal").exists(columnType.contains)
}

private def columnTypeIsInt(columnType: String): Boolean = {
List("int", "serial").exists(columnType.contains)
}

private def columnTypeIsDate(columnType: String): Boolean = {
List("date", "time").exists(columnType.contains)
}

private def isUUID(value: String, columnType: String): Boolean = {
Try(UUID.fromString(value)) match {
case Success(_) => columnType == "uuid"
case Failure(_) => false
}
}

private def isInt(value: String, columnType: String): Boolean = {
value.toIntOption.isDefined && columnTypeIsInt(columnType)
}

private def isDecimal(value: String, columnType: String): Boolean = {
value.toDoubleOption.isDefined && columnTypeIsDouble(columnType)
}

private def isNumberOrUUID(value: String, columnType: String): Boolean = {
isInt(value, columnType) ||
isDecimal(value, columnType) ||
isUUID(value, columnType)
}

def getTableData(
settings: TableSettings,
columns: List[TableColumn],
Expand All @@ -88,12 +122,15 @@ object DatabaseTablesDAO {

val conditionsSql = queryParameters.filters
.map { case FilterParameter(filterField, filterValue) =>
val columnType = columns.find(_.name == filterField) match {
case Some(column) => column.`type`
case None => throw Exception(s"Column with name '$filterField' not found.")
}
filterValue match {
case dateRegex(_, _, _) =>
case dateRegex(_, _, _) if columnTypeIsDate(columnType) =>
s"DATE($filterField) = ?"

case _ =>
if (filterValue.toIntOption.isDefined || filterValue.toDoubleOption.isDefined)
if (isNumberOrUUID(filterValue, columnType))
s"$filterField = ?"
else
s"$filterField LIKE ?"
Expand All @@ -111,20 +148,25 @@ object DatabaseTablesDAO {
val preparedStatement = conn.prepareStatement(sql)

queryParameters.filters.zipWithIndex
.foreach { case (FilterParameter(_, filterValue), index) =>
.foreach { case (FilterParameter(filterField, filterValue), index) =>
// We have to increment index by 1 because SQL parameterIndex starts in 1
val sqlIndex = index + 1

val columnType = columns.find(_.name == filterField) match {
case Some(column) => column.`type`
case None => throw Exception(s"Column with name '$filterField' not found.")
}
filterValue match {
case dateRegex(year, month, day) =>
case dateRegex(year, month, day) if columnTypeIsDate(columnType) =>
val parsedDate = LocalDate.of(year.toInt, month.toInt, day.toInt)
preparedStatement.setDate(sqlIndex, Date.valueOf(parsedDate))

case _ =>
if (filterValue.toIntOption.isDefined)
if (isInt(filterValue, columnType))
preparedStatement.setInt(sqlIndex, filterValue.toInt)
else if (filterValue.toDoubleOption.isDefined)
else if (isDecimal(filterValue, columnType))
preparedStatement.setDouble(sqlIndex, filterValue.toDouble)
else if (isUUID(filterValue, columnType))
preparedStatement.setObject(sqlIndex, UUID.fromString(filterValue))
else
preparedStatement.setString(sqlIndex, s"%$filterValue%")
}
Expand Down
100 changes: 100 additions & 0 deletions spra-play-server/src/test/scala/controllers/AdminControllerSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,106 @@ class AdminControllerSpec extends PlayPostgresSpec with AdminUtils {
.futureValue
response.headOption.isEmpty must be(true)
}

"don't fail when the column is citext or similar, but the value can be interpreted as Int." in withApiClient {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When the value is CITEXT we should interpret it as string, any reason to interpret it as int?

Copy link
Contributor Author

@Antonio171003 Antonio171003 Sep 7, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Before, if the value was "123", it was assumed that the value was an Int, and the same for the column type. This caused an internal server error because it compared a citext with an Int.

Same for the others comments

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my point of view, the input argument must be handled like the type derived from the database instead of guessing its format.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the changes in this pull request consider both the column type and the value to avoid this.

client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue

val response = client
.getTableMetadata(
usersSettings.tableName,
List("email", "ASC"),
List(0, 9),
"""{"email":"17"}"""
)
.futureValue
val head = response.headOption.value
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}

"don't fail when the column is citext or similar, but the value can be interpreted as Decimal." in withApiClient {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue

val response = client
.getTableMetadata(
usersSettings.tableName,
List("email", "ASC"),
List(0, 9),
"""{"email":"17.10"}"""
)
.futureValue
val head = response.headOption.value
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}

"don't fail when the column is citext or similar, but the value can be interpreted as Date." in withApiClient {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue

val response = client
.getTableMetadata(
usersSettings.tableName,
List("email", "ASC"),
List(0, 9),
"""{"email":"2024-06"}"""
)
.futureValue
val head = response.headOption.value
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}

"don't fail when the column is citext or similar, but the value can be interpreted as UUID." in withApiClient {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same

client =>
val name = "wiringbits"
val email = "[email protected]"
val request = AdminCreateTable.Request(
Map("name" -> name, "email" -> email, "password" -> "wiringbits")
)
client.createItem(usersSettings.tableName, request).futureValue

val response = client
.getTableMetadata(
usersSettings.tableName,
List("name", "ASC"),
List(0, 9),
"""{"email":"8c861a28-e384-4a9b-b7c2-a0367aa3f3e8"}"""
)
.futureValue
val head = response.headOption.value
val nameValue = head.find(_._1 == "name").value._2
val emailValue = head.find(_._1 == "email").value._2
response.size must be(1)
name must be(nameValue)
email must be(emailValue)
}
}

"GET /admin/tables/:tableName/:primaryKey" should {
Expand Down
Loading