Skip to content

Equality and hashcode are incorrectly code generated for union members with nested blobs #1146

Open
@ianbotsf

Description

@ianbotsf

Describe the bug

Codegen for unions special-cases union members that are blobs to be handled correctly (i.e., by content, not by reference) when calculating hashcode and equality. But it does not account for union members that have nested blobs.

For example, consider DynamoDB's AttributeValue union:

sealed class AttributeValue {
    // This class is correct because the array is properly handled in equals/hashcode
    data class B(val value: ByteArray) : AttributeValue() {
        override fun hashCode(): Int {
            return value.contentHashCode()
        }

        override fun equals(other: Any?): Boolean {
            if (this === other) return true
            if (other == null || this::class != other::class) return false

            other as B

            if (!value.contentEquals(other.value)) return false

            return true
        }
    }

    // This class is correct because non-arrays don't need special handling
    data class Bool(val value: Boolean) : AttributeValue()

    // This class is incorrect because the arrays are hashed/compared by reference, not by content
    data class Bs(val value: List<ByteArray>) : AttributeValue()
}

Expected behavior

The generated code for union members that contain nested blobs should codegen equals/hashcode implementations which verify content equality, not reference equality.

Current behavior

(see above)

Steps to Reproduce

(see above)

Possible Solution

No response

Context

No response

Smithy-Kotlin version

1.3.5

Platform (JVM/JS/Native)

(any)

Operating system and version

(any)

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions