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

Protobuf Codec: Fails to decode lists of lists #94

Closed
thinkharderdev opened this issue Jul 20, 2021 · 2 comments · Fixed by #157
Closed

Protobuf Codec: Fails to decode lists of lists #94

thinkharderdev opened this issue Jul 20, 2021 · 2 comments · Fixed by #157
Labels
bug Something isn't working good first issue Good for newcomers

Comments

@thinkharderdev
Copy link
Contributor

Currently, we fail to decode empty lists of lists. Issue can replicated with the following test case

  case class WithPiggyback(
    conversationId: Long,
    message: Chunk[Byte],
    gossip: List[Chunk[Byte]]
  )

  object WithPiggyback {
    implicit val schema: Schema[WithPiggyback] = DeriveSchema.gen

    implicit val gen: Gen[Random with Sized, WithPiggyback] =
      for {
        conversationId <- Gen.anyLong
        message        <- Gen.chunkOf(Gen.anyByte)
        gossip         <- Gen.listOf(Gen.chunkOf(Gen.anyByte))
      } yield WithPiggyback(conversationId, message, gossip)
  }

      testM("record with array fields") {
        checkM(WithPiggyback.gen) { value =>
          encodeAndDecodeNS(Schema[WithPiggyback], value, print = true).map { ed =>
            assert(ed)(equalTo(value))
          }
        }
      }

The issue seems to be in composing Decoders where flatMap errors out if there are no bytes remaining in the chunk after running the "first" decoder. This causes an issue when the final field in a Record is a collection. In that case, the key will be decoded as a length delimited field with zero length. As expected there are no more bytes to decode since the list is empty but when flatMaping the Decoder will error out.

@thinkharderdev thinkharderdev added bug Something isn't working good first issue Good for newcomers labels Jul 20, 2021
@jupposessho
Copy link
Contributor

jupposessho commented Aug 9, 2021

The problem occurs with a single empty list as well:

testM("record with array fields") {
  val value = WithPiggyback(List(), 0)
  encodeAndDecodeNS(Schema[WithPiggyback], value, print = true).map { ed =>
    assert(ed)(equalTo(value))
  }
}

case class WithPiggyback(
  gossip: List[String],
  conversationId: Long
)

Even without the conversationId field.

I tried to decode the field as an EmptyCollection but the problem is the same, so not sure how to handle this.

@ashprakasan
Copy link
Contributor

I'll work on this.

thinkharderdev pushed a commit that referenced this issue Oct 26, 2021
…140)

* Fixing bug with decoding empty lists in protobuf code. (issue #94)

* Refactor. (issue #94)
landlockedsurfer pushed a commit to landlockedsurfer/zio-schema that referenced this issue May 28, 2022
zio#140)

* Fixing bug with decoding empty lists in protobuf code. (issue zio#94)

* Refactor. (issue zio#94)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants