Skip to content

Commit 2dea49c

Browse files
committed
add tests
1 parent 00632a1 commit 2dea49c

File tree

5 files changed

+30
-8
lines changed

5 files changed

+30
-8
lines changed

avro4s-core/src/main/scala/com/sksamuel/avro4s/AvroInputStream.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import java.io.{ByteArrayInputStream, File, InputStream}
44
import java.nio.ByteBuffer
55
import java.nio.file.{Files, Path, Paths}
66

7+
import com.sksamuel.avro4s.avroutils.ByteBufferHelper
78
import org.apache.avro.Schema
89

910
import scala.util.Try
@@ -55,7 +56,8 @@ class AvroInputStreamBuilder[T: Decoder](format: AvroFormat) {
5556
def from(file: File): AvroInputStreamBuilderWithSource[T] = from(file.toPath)
5657
def from(in: InputStream): AvroInputStreamBuilderWithSource[T] = new AvroInputStreamBuilderWithSource(format, in)
5758
def from(bytes: Array[Byte]): AvroInputStreamBuilderWithSource[T] = from(new ByteArrayInputStream(bytes))
58-
def from(buffer: ByteBuffer): AvroInputStreamBuilderWithSource[T] = from(new ByteArrayInputStream(buffer.array))
59+
def from(buffer: ByteBuffer): AvroInputStreamBuilderWithSource[T] = from(
60+
new ByteArrayInputStream(ByteBufferHelper.asArray(buffer)))
5961
}
6062

6163

avro4s-core/src/main/scala/com/sksamuel/avro4s/decoders/bytes.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.sksamuel.avro4s.decoders
22

3+
import com.sksamuel.avro4s.avroutils.ByteBufferHelper
34
import com.sksamuel.avro4s.{Avro4sDecodingException, Decoder}
45
import org.apache.avro.Schema
56

@@ -18,7 +19,7 @@ trait ByteDecoders:
1819
object ArrayByteDecoder extends Decoder[Array[Byte]] :
1920
override def decode(schema: Schema): Any => Array[Byte] = { value =>
2021
value match {
21-
case buffer: ByteBuffer => buffer.array
22+
case buffer: ByteBuffer => ByteBufferHelper.asArray(buffer)
2223
case array: Array[Byte] => array
2324
case fixed: org.apache.avro.generic.GenericFixed => fixed.bytes
2425
case _ => throw new Avro4sDecodingException(s"ArrayByteDecoder cannot decode '$value'", value)

avro4s-core/src/main/scala/com/sksamuel/avro4s/encoders/strings.scala

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.sksamuel.avro4s.encoders
22

3-
import com.sksamuel.avro4s.{Avro4sConfigurationException, Avro4sEncodingException, Encoder, FieldMapper}
4-
import org.apache.avro.Conversions.UUIDConversion
5-
import org.apache.avro.{Conversions, LogicalTypes, Schema}
3+
import com.sksamuel.avro4s.avroutils.ByteBufferHelper
4+
import com.sksamuel.avro4s.{Avro4sConfigurationException, Avro4sEncodingException, Encoder}
5+
import org.apache.avro.Schema
66
import org.apache.avro.generic.GenericData
77
import org.apache.avro.util.Utf8
88

@@ -44,6 +44,9 @@ object ByteStringEncoder extends Encoder[String] :
4444
*/
4545
object FixedStringEncoder extends Encoder[String] :
4646
override def encode(schema: Schema): String => Any = string =>
47-
if (string.getBytes.length > schema.getFixedSize)
48-
throw new Avro4sEncodingException(s"Cannot write string with ${string.getBytes.length} bytes to fixed type of size ${schema.getFixedSize}")
49-
GenericData.get.createFixed(null, ByteBuffer.allocate(schema.getFixedSize).put(string.getBytes).array, schema).asInstanceOf[GenericData.Fixed]
47+
val bytes = string.getBytes
48+
if (bytes.length > schema.getFixedSize)
49+
throw new Avro4sEncodingException(s"Cannot write string with ${bytes.length} bytes to fixed type of size ${schema.getFixedSize}")
50+
GenericData.get.createFixed(null,
51+
ByteBufferHelper.asArray(ByteBuffer.allocate(schema.getFixedSize).put(bytes)),
52+
schema).asInstanceOf[GenericData.Fixed]

avro4s-core/src/test/scala/com/sksamuel/avro4s/record/decoder/ByteArrayDecoderTest.scala

+7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class ByteArrayDecoderTest extends AnyFunSuite with Matchers {
4444
Decoder[VectorTest].decode(schema).apply(record).z shouldBe Vector[Byte](1, 4, 9)
4545
}
4646

47+
test("decode read-only ByteBuffer to Vector[Byte]") {
48+
val schema = AvroSchema[VectorTest]
49+
val record = new GenericData.Record(schema)
50+
record.put("z", ByteBuffer.wrap(Array[Byte](1, 4, 9)).asReadOnlyBuffer())
51+
Decoder[VectorTest].decode(schema).apply(record).z shouldBe Vector[Byte](1, 4, 9)
52+
}
53+
4754
test("decode Array[Byte] to List[Byte]") {
4855
val schema = AvroSchema[ListTest]
4956
val record = new GenericData.Record(schema)

avro4s-core/src/test/scala/com/sksamuel/avro4s/record/encoder/ByteArrayEncoderTest.scala

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ class ByteArrayEncoderTest extends AnyFunSuite with Matchers {
8888
fixed.bytes().length shouldBe 7
8989
}
9090

91+
test("encode byte buffers as FIXED (ReadOnlyBuffer)") {
92+
val schema = SchemaBuilder.fixed("foo").size(7)
93+
val fixed = Encoder[ByteBuffer]
94+
.encode(schema)
95+
.apply(ByteBuffer.wrap("hello".getBytes).asReadOnlyBuffer())
96+
.asInstanceOf[GenericFixed]
97+
fixed.bytes().toList shouldBe Seq(104, 101, 108, 108, 111, 0, 0)
98+
fixed.bytes().length shouldBe 7
99+
}
91100

92101
test("encode top level byte arrays") {
93102
val encoder = Encoder[Array[Byte]]

0 commit comments

Comments
 (0)