codegen-java/kotlin: Fix generation of hashCode methods (#802)

codegen-java:
- use `pattern.pattern()` instead of `pattern` in hashCode method
  (consistent with equals method)

codegen-kotlin:
- use `regex.pattern` instead of `regex` in hashCode method
  (consistent with equals method)
- if a data class has a Regex property, generate not only
  an equals method but also a hashCode method
This commit is contained in:
translatenix
2024-11-13 15:42:45 -08:00
committed by GitHub
parent b8d90eddec
commit df38011c9e
5 changed files with 39 additions and 16 deletions
@@ -561,6 +561,26 @@ class KotlinCodeGeneratorTest {
)
}
@Test
fun `data class with Regex property has custom equals and hashCode methods`() {
val kotlinCode =
generateKotlinCode(
"""
module my.mod
class Person {
age: Int
name: Regex
}
"""
.trimIndent()
)
assertThat(kotlinCode)
.contains("if (this.name.pattern != other.name.pattern) return false")
.contains("result = 31 * result + Objects.hashCode(this.name.pattern)")
}
@Test
fun `recursive types`() {
val kotlinCode =
@@ -139,7 +139,7 @@ object Mod {
result = 31 * result + Objects.hashCode(this.container)
result = 31 * result + Objects.hashCode(this.container2)
result = 31 * result + Objects.hashCode(this.other)
result = 31 * result + Objects.hashCode(this.regex)
result = 31 * result + Objects.hashCode(this.regex.pattern)
result = 31 * result + Objects.hashCode(this.any)
result = 31 * result + Objects.hashCode(this.nonNull)
result = 31 * result + Objects.hashCode(this.enum)