Skip to content
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 @@ -333,6 +333,68 @@ class Kotlin2CodeGenTest {
assertCompilesKotlin(result)
}

@Test
fun `equals and hashCode compare requested fields by value, at runtime`() {
val schema =
"""
type Item {
name: String
age: Int
}
""".trimIndent()

val result =
CodeGen(
CodeGenConfig(
schemas = setOf(schema),
packageName = "com.netflix.test.equalshashcode",
language = Language.KOTLIN,
generateKotlinNullableClasses = true,
generateKotlinClosureProjections = true,
),
).generate()

val buildDir = assertCompilesKotlin(result)
val classLoader = URLClassLoader(arrayOf(buildDir.toUri().toURL()), this.javaClass.classLoader)
val builderClass = classLoader.loadClass("com.netflix.test.equalshashcode.types.Item\$Builder")
val withNameMethod = builderClass.getMethod("withName", String::class.java)
val withAgeMethod = builderClass.getMethod("withAge", Integer::class.java)
val buildMethod = builderClass.getMethod("build")

fun newItem(
name: String?,
requestName: Boolean,
age: Int?,
requestAge: Boolean,
): Any {
val builder = builderClass.getDeclaredConstructor().newInstance()
if (requestName) withNameMethod.invoke(builder, name)
if (requestAge) withAgeMethod.invoke(builder, age)
return buildMethod.invoke(builder)
}

// Same projection, same values -> equal, and hashCode is consistent with equals.
val sameProjectionA = newItem("Alice", requestName = true, age = 30, requestAge = true)
val sameProjectionB = newItem("Alice", requestName = true, age = 30, requestAge = true)
assertThat(sameProjectionA).isEqualTo(sameProjectionB)
assertThat(sameProjectionA.hashCode()).isEqualTo(sameProjectionB.hashCode())

// `age` requested on only one side -> not equal, in both directions, and does not throw.
val onlyNameRequested = newItem("Alice", requestName = true, age = null, requestAge = false)
assertThat(sameProjectionA).isNotEqualTo(onlyNameRequested)
assertThat(onlyNameRequested).isNotEqualTo(sameProjectionA)

// `age` unrequested on both sides -> equal without invoking the throwing supplier.
val onlyNameRequestedTwin = newItem("Alice", requestName = true, age = null, requestAge = false)
assertThat(onlyNameRequested).isEqualTo(onlyNameRequestedTwin)
assertThat(onlyNameRequested.hashCode()).isEqualTo(onlyNameRequestedTwin.hashCode())

// `age` requested-but-null vs. `age` absent -> not equal.
val nameRequestedNullAge = newItem("Alice", requestName = true, age = null, requestAge = true)
assertThat(onlyNameRequested).isNotEqualTo(nameRequestedNullAge)
assertThat(nameRequestedNullAge).isNotEqualTo(onlyNameRequested)
}

companion object {
@Suppress("unused")
@JvmStatic
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsForInputTypes.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

@Generated
Expand All @@ -29,6 +33,24 @@ public class Person(
public val lastname: String?
get() = __lastname.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__firstname === firstnameDefault) firstnameDefault else firstname,
if (__lastname === lastnameDefault) lastnameDefault else lastname,
)

override fun equals(other: Any?): Boolean = this === other || (other is Person &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__firstname === firstnameDefault) null else "firstname=" + firstname,
if (__lastname === lastnameDefault) null else "lastname=" + lastname,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Person(", postfix =
")")

@Generated
public companion object {
private val firstnameDefault: () -> String? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsForInputTypes.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

Expand All @@ -22,6 +26,22 @@ public class Query(
public val people: List<Person?>?
get() = __people.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__people === peopleDefault) peopleDefault else people,
)

override fun equals(other: Any?): Boolean = this === other || (other is Query &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__people === peopleDefault) null else "people=" + people,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Query(", postfix =
")")

@Generated
public companion object {
private val peopleDefault: () -> List<Person?>? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedInputTypes.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

@Generated
Expand All @@ -29,6 +33,24 @@ public class Person(
public val lastname: String?
get() = __lastname.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__firstname === firstnameDefault) firstnameDefault else firstname,
if (__lastname === lastnameDefault) lastnameDefault else lastname,
)

override fun equals(other: Any?): Boolean = this === other || (other is Person &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__firstname === firstnameDefault) null else "firstname=" + firstname,
if (__lastname === lastnameDefault) null else "lastname=" + lastname,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Person(", postfix =
")")

@Generated
public companion object {
private val firstnameDefault: () -> String? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedInputTypes.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

Expand All @@ -22,6 +26,22 @@ public class Query(
public val people: List<Person?>?
get() = __people.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__people === peopleDefault) peopleDefault else people,
)

override fun equals(other: Any?): Boolean = this === other || (other is Query &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__people === peopleDefault) null else "people=" + people,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Query(", postfix =
")")

@Generated
public companion object {
private val peopleDefault: () -> List<Person?>? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedInterface.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

Expand All @@ -22,6 +26,22 @@ public class Query(
public val people: List<Person?>?
get() = __people.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__people === peopleDefault) peopleDefault else people,
)

override fun equals(other: Any?): Boolean = this === other || (other is Query &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__people === peopleDefault) null else "people=" + people,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Query(", postfix =
")")

@Generated
public companion object {
private val peopleDefault: () -> List<Person?>? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedQuery.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

@Generated
Expand All @@ -29,6 +33,24 @@ public class Person(
public val lastname: String?
get() = __lastname.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__firstname === firstnameDefault) firstnameDefault else firstname,
if (__lastname === lastnameDefault) lastnameDefault else lastname,
)

override fun equals(other: Any?): Boolean = this === other || (other is Person &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__firstname === firstnameDefault) null else "firstname=" + firstname,
if (__lastname === lastnameDefault) null else "lastname=" + lastname,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Person(", postfix =
")")

@Generated
public companion object {
private val firstnameDefault: () -> String? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedQuery.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

Expand All @@ -29,6 +33,24 @@ public class Query(
public val friends: List<Person?>?
get() = __friends.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__people === peopleDefault) peopleDefault else people,
if (__friends === friendsDefault) friendsDefault else friends,
)

override fun equals(other: Any?): Boolean = this === other || (other is Query &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__people === peopleDefault) null else "people=" + people,
if (__friends === friendsDefault) null else "friends=" + friends,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Query(", postfix =
")")

@Generated
public companion object {
private val peopleDefault: () -> List<Person?>? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedTypes.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

@Generated
Expand Down Expand Up @@ -36,6 +40,26 @@ public class Person(
public val email: String?
get() = __email.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__firstname === firstnameDefault) firstnameDefault else firstname,
if (__lastname === lastnameDefault) lastnameDefault else lastname,
if (__email === emailDefault) emailDefault else email,
)

override fun equals(other: Any?): Boolean = this === other || (other is Person &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__firstname === firstnameDefault) null else "firstname=" + firstname,
if (__lastname === lastnameDefault) null else "lastname=" + lastname,
if (__email === emailDefault) null else "email=" + email,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Person(", postfix =
")")

@Generated
public companion object {
private val firstnameDefault: () -> String? =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import com.fasterxml.jackson.databind.`annotation`.JsonDeserialize
import com.fasterxml.jackson.databind.`annotation`.JsonPOJOBuilder
import com.netflix.graphql.dgs.codegen.cases.constantsWithExtendedTypes.expected.Generated
import java.lang.IllegalStateException
import kotlin.Any
import kotlin.Boolean
import kotlin.Int
import kotlin.String
import kotlin.collections.List
import kotlin.jvm.JvmName

Expand All @@ -22,6 +26,22 @@ public class Query(
public val people: List<Person?>?
get() = __people.invoke()

private fun `__$fieldValues`(): List<Any?> = listOf(
if (__people === peopleDefault) peopleDefault else people,
)

override fun equals(other: Any?): Boolean = this === other || (other is Query &&
`__$fieldValues`() == other.`__$fieldValues`())

override fun hashCode(): Int = `__$fieldValues`().hashCode()

private fun `__$fieldStrings`(): List<String> = listOfNotNull(
if (__people === peopleDefault) null else "people=" + people,
)

override fun toString(): String = `__$fieldStrings`().joinToString(prefix = "Query(", postfix =
")")

@Generated
public companion object {
private val peopleDefault: () -> List<Person?>? =
Expand Down
Loading
Loading