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

Remove automatic padding for fixed types as it should be done by custom serialization strategy #232

Closed
Chuckame opened this issue Jul 10, 2024 · 1 comment · Fixed by #235
Labels
enhancement New feature or request
Milestone

Comments

@Chuckame
Copy link
Contributor

Is your feature request related to a problem? Please describe.
Currently, when a byte-array or string is encoded as fixed, it tries to match the fixed size by padding with 0s at the beginning (for ByteArray and ByteBuffer) or at the end (for String), which ends up to a modified content.

This should be done on the user side, depending on its needs.

Describe the solution you'd like
Totally remove the automatic-padding feature, and fail when the given string/ByteArray/ByteBuffer is not of the same size as the expected fixed schema.

To still be able to pad with 0s, the user has to provide its own AvroSerializer, providing explicitly the fixed schema and doing its padding logic inside serializeAvro and deserializeAvro. Here is an example:

public object ZeroPaddedByteArraySerializer: AvroSerializer<ByteArray>("zero-padded-byte-array") {
    override fun getSchema(context: SchemaSupplierContext): Schema {
        return context.fixed?.createSchema() ?: throw SerializationException("ZeroPaddedByteArraySerializer requires @AvroFixed annotation")
    }

    override fun serializeAvro(encoder: AvroEncoder, value: ByteArray) {
        // pad with 0s to match 15 bytes
        encoder.encodeFixed(value.zeroPadded(15, endPadded = true))
    }

    override fun deserializeAvro(decoder: AvroDecoder): ByteArray {
        TODO("Not yet implemented")
    }
}
@Chuckame Chuckame added the enhancement New feature or request label Jul 10, 2024
@Chuckame Chuckame added this to the Version 2 milestone Jul 10, 2024
@Chuckame Chuckame linked a pull request Jul 10, 2024 that will close this issue
@Chuckame
Copy link
Contributor Author

Released in v2.0.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant