Paging with Kotlin (java) driver, generics error: Unsupported generic type

I can get this query to output what I want in Compass, but I can’t for the life of me figure out why I cant get the java driver to deserialize the projection.

It seems to be an error in the bson serializer, but can’t tell what exactly is happening.
I’m working in kotlin, and it could just be a conflict with how the serializer works relative to java (we really need a kotlin only driver).

This is the query i’m using in compass:

[
  {
    $match: {
      status: "Unread" // or whatever your criteria are
    }
  },
  {
    $facet: {
      data: [
        {
          $skip: 0
        },
        {
          $limit: 1
        }
      ],
      totalCount: [
        {
          $count: "total"
        }
      ]
    }
  },
  {
    $project: {
      offset: {
        $literal: 0
      },
      limit: {
        $literal: 1
      },
      total: {
        $ifNull: [
          {
            $arrayElemAt: ["$totalCount.total", 0]
          },
          0
        ]
      },
      data: 1
    }
  }
]

This is the query in kotlin using the java driver:

suspend inline fun <reified T : Any> MongoCollection<T>.findPagedList(
    initialStages: List<Bson>,
    offset: Long,
    limit: Int,
    sort: Bson? = null,
): PagedList<T> {
// 1. Define the pipeline for the "data" facet
    // This pipeline will operate on the documents that passed the initial 'match' stage.
    val dataPipeline = listOfNotNull(
        sort,                        // Add sort stage if not null (must be first in this sub-pipeline)
        Document(
            "\$skip",
            offset
        ),  // Using Document for Long offset, as Aggregates.skip() might take Int
        limit(limit)      // Use Aggregates.limit() for the limit stage
    )

    // 2. Create the "data" Facet
    val dataFacet = Facet("data", dataPipeline)

    // 3. Define the pipeline for the "totalCount" facet
    // This also operates on documents that passed the initial 'match' stage.
    val totalCountPipeline = listOf(
        count("total") // Counts documents and outputs: { "total": <count> }
    )

    // 4. Create the "totalCount" Facet
    val totalCountFacet = Facet("totalCount", totalCountPipeline)

    // 5. Build the main aggregation pipeline
    val fullPipeline = initialStages + listOf( // append to initial match stage

        // The Facet Stage using the helper
        facet(dataFacet, totalCountFacet),

        // The Projection Stage (should be compatible with the facet output)
        // Your existing projection logic seems fine with this facet structure.
        project( // Using Aggregates.project for consistency
            Document("offset", Document("\$literal", offset))
                .append("limit", Document("\$literal", limit))
                .append(
                    "total",
                    Document(
                        "\$ifNull", // If totalCount is empty (no matches), default to 0
                        listOf(
                            Document(
                                "\$arrayElemAt",
                                listOf("\$totalCount.total", 0L)
                            ), // Get the 'total' field from the first doc in totalCount array
                            0L
                        )
                    )
                )
                .append("data", "\$data") // Project the 'data' array from the dataFacet
        )
    )

    // 6. Execute the aggregation and get the result
    // Assuming PagedList and T are @Serializable for kotlinx.serialization
    // Execute the aggregation
    val result: PagedList<T> = try {
        aggregate<PagedList<T>>(fullPipeline).first()
    } catch (e: Exception) {
        logger.error(
            "Error executing aggregation pipeline for collection '${this.namespace.collectionName}': ${e.message}",
            e
        )
        throw e // Re-throw the exception after logging
    }

    return result
}

No matter what I do, i always get some sort of generics error, that I don’t think I should be getting. My function is properly inlined with a reified type. So where is this coming from?

org.bson.codecs.configuration.CodecConfigurationException: Unsupported generic type of container: T
	at org.bson.codecs.ContainerCodecHelper.getCodec(ContainerCodecHelper.java:102)
	at org.bson.codecs.CollectionCodecProvider.get(CollectionCodecProvider.java:99)
	at org.bson.internal.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:69)
	at org.bson.codecs.configuration.OverridableUuidRepresentationCodecProvider.get(OverridableUuidRepresentationCodecProvider.java:46)
	at org.bson.internal.ProvidersCodecRegistry.lambda$get$0(ProvidersCodecRegistry.java:81)
	at java.base/java.util.Optional.orElseGet(Unknown Source)

If anyone has an idea of where this might be coming from, a clue would be greatly approciated.