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: EXPOSED-266 Between() accepts arguments of different type than column type #1983

Merged
merged 2 commits into from
Feb 5, 2024

Conversation

bog-walk
Copy link
Member

@bog-walk bog-walk commented Feb 1, 2024

between() currently accepts arguments of types that do not match the type stored in the column on which it is called.
This means that the following is technically possible to compile:

object TestTable : Table() {
    val number = integer("number")
}

TestTable
    .selectAll()
    .where { TestTable.number.between("hello", "there") }

In the event that this is being taken advantage of by users (for example with numeric or UUID types), rather than introducing an immediate breaking change, the upper bounds have been restricted to highlight the offending function and show a compiler warning.

Type argument for a type parameter S can't be inferred because it has incompatible upper bounds: Int, String (multiple incompatible classes). This will become an error in Kotlin 2.0

A function overload has also been introduced to ensure that this warning does not show when invoked on an EntityID column with appropriate literal arguments.

Note: No tests have been added and all tests for between() in the datetime modules provide matching argument types with no warnings.

Additional:

  • Replace some datetime functions with deprecation warnings.

…olumn type

between() currently accepts arguments of types that do not match the type stored
in the column on which it is called. In the event that this is being taken advantage
of by users, rather than introducing an immediate breaking change, the upper bounds
have been restricted to show a compiler warning.

A function override has also been introduced to ensure that this warning does not
show when invoked on an EntityID column with literal arguments.
…olumn type

Replace deprecated datetime functions in tests.
@bog-walk bog-walk requested review from e5l and joc-a February 1, 2024 22:09
Comment on lines -399 to +402
fun <T, S : T?> ExpressionWithColumnType<S>.between(from: T, to: T): Between = Between(this, wrap(from), wrap(to))
fun <T, S : T?> ExpressionWithColumnType<in S>.between(from: T, to: T): Between = Between(this, wrap(from), wrap(to))

/** Returns `true` if this [EntityID] expression is between the values [from] and [to], `false` otherwise. */
fun <T : Comparable<T>, E : EntityID<T>?> ExpressionWithColumnType<E>.between(from: T, to: T): Between = Between(this, wrap(from), wrap(to))
Copy link
Member Author

Choose a reason for hiding this comment

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

As mentioned above, I'm not sure how to test for the presence of a compiler warning as all existing tests use arguments with the correct type. And any test purposefully set up to show a warning would fail when we upgrade the Kotlin version.

Please let me know if there's any type of test that should be added.

Copy link
Member

Choose a reason for hiding this comment

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

It's fine not to test it

Copy link
Member

@e5l e5l left a comment

Choose a reason for hiding this comment

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

lgtm

Comment on lines -399 to +402
fun <T, S : T?> ExpressionWithColumnType<S>.between(from: T, to: T): Between = Between(this, wrap(from), wrap(to))
fun <T, S : T?> ExpressionWithColumnType<in S>.between(from: T, to: T): Between = Between(this, wrap(from), wrap(to))

/** Returns `true` if this [EntityID] expression is between the values [from] and [to], `false` otherwise. */
fun <T : Comparable<T>, E : EntityID<T>?> ExpressionWithColumnType<E>.between(from: T, to: T): Between = Between(this, wrap(from), wrap(to))
Copy link
Member

Choose a reason for hiding this comment

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

It's fine not to test it

@bog-walk bog-walk merged commit 46dc765 into main Feb 5, 2024
4 of 5 checks passed
@bog-walk bog-walk deleted the bog-walk/fix-between-arg-bounds branch February 5, 2024 17:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants