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

KSP: Can't create correct custom MessageBodyWriter #10117

Closed
altro3 opened this issue Nov 14, 2023 · 3 comments · Fixed by #10193
Closed

KSP: Can't create correct custom MessageBodyWriter #10117

altro3 opened this issue Nov 14, 2023 · 3 comments · Fixed by #10193

Comments

@altro3
Copy link
Contributor

altro3 commented Nov 14, 2023

Custom MessageBofyWriter:

@Singleton
@Produces(MediaType.APPLICATION_JSON)
@Order(-1)
internal class PageBodyWriter<T>(
    val registry: MessageBodyHandlerRegistry,
    val bodyWriter: @Nullable MessageBodyWriter<List<T>>?,
    val bodyType: @Nullable Argument<List<T>>?
) : MessageBodyWriter<Page<T>> {

    @Inject
    constructor(registry: MessageBodyHandlerRegistry) : this(registry, null, null)

    override fun createSpecific(type: Argument<Page<T>>): MessageBodyWriter<Page<T>> {
        val bt: Argument<List<T>> = Argument.listOf(type.getTypeParameters()[0]) as Argument<List<T>>
        val writer = registry.findWriter(bt, listOf(MediaType.APPLICATION_JSON_TYPE))
                .orElseThrow { ConfigurationException("No JSON message writer present") }
        return PageBodyWriter(registry, writer, bt)
    }

    @Throws(CodecException::class)
    override fun writeTo(
            type: Argument<Page<T>>,
            mediaType: MediaType,
            page: Page<T>,
            headers: MutableHeaders,
            outputStream: OutputStream
    ) {
        if (bodyType == null || bodyWriter == null) {
            throw ConfigurationException("No JSON message writer present")
        }
        headers.add(PAGE_NUMBER_HEADER, page.pageNumber.toString())
                .add(PAGE_SIZE_HEADER, page.size.toString())
                .add(PAGE_COUNT_HEADER, page.getTotalPages().toString())
                .add(TOTAL_COUNT_HEADER, page.totalSize.toString())
        bodyWriter.writeTo(bodyType, mediaType, page.content, headers, outputStream)
    }

    override fun isWriteable(type: Argument<Page<T>>, mediaType: MediaType): Boolean {
        return bodyType == null || bodyWriter != null && bodyWriter.isWriteable(bodyType, mediaType)
    }

    override fun isBlocking(): Boolean {
        return bodyWriter != null && bodyWriter.isBlocking
    }

    companion object {
        private const val PAGE_NUMBER_HEADER = "X-Page-Number"
        private const val PAGE_SIZE_HEADER = "X-Page-Size"
        private const val TOTAL_COUNT_HEADER = "X-Total-Count"
        private const val PAGE_COUNT_HEADER = "X-Page-Count"
    }
}

The poblem with call method createSpecific(type: Argument<Page<T>>). Look to screenshot:
изображение

As you see type is instance of DefaultGenericPlaceholder like SimpleModel T. But our type must be Page<T> because we set : MessageBodyWriter<Page<T>> {

Same test with kapt:
изображение

There are all fine - type is interface io.micronaut.data.model.Page and type argument is SimpleModel T

For full reproduce just run tests in test-suite-kotlin-ksp-server-generator module in this branch: micronaut-projects/micronaut-openapi#1173

Version

4.2.0

@altro3
Copy link
Contributor Author

altro3 commented Nov 14, 2023

@dstepanov Yesterday I told you about the problem in the KSP. This is what I was talking about

@altro3
Copy link
Contributor Author

altro3 commented Dec 2, 2023

@dstepanov look to this, pls

@dstepanov
Copy link
Contributor

Checking

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Status: Done
2 participants