Skip to content

Kotlin default values are silently ignored in POST body #1073

Closed
@distinctdan

Description

@distinctdan

Expected Behavior

I've discovered that for POST body params, kotlin default values are silently ignored and are set to "zero" values instead. Adding validation doesn't help, and adding required parameters doesn't help either. This is a very serious issue because programmers assume that core language functionality actually works like it's supposed to, and this can lead to a lot of tricky bugs.

This is serious enough that Micronaut can't honestly say it supports Kotlin if this doesn't work. Data classes are a big selling point of Kotlin, and if Micronaut can't use them correctly, then I don't think it's fair to say that kotlin is supported. I've done some googling and it looks like this may be caused by changes in some of micronaut's dependencies. However, that doesn't matter. At the end of the day, if you guys say you support a language, then the framework needs to work correctly in that language. Or, you need big warnings documenting that some core language features don't work at all, or worse, only work for certain cases.

Is there any way to make kotlin defaults work with serialization?

// Hit the endpoint with POST /temp {"required": "some value"}

@Controller
open class MyController {
    private val LOG = LoggerFactory.getLogger(this::class.java)
    
    @Serdeable
    data class Params(
        val required: String,
        // Works
        val stringDefault: String = "default",
        // Silently ignored
        val boolDefault: Boolean = true,
        // Silently ignored
        val longDefault: Long = 5,
    )

    @Post("/temp")
    @Secured(REST_OPS_WEB_DEV_TEAM)
    open suspend fun temp(
        @Body params: Params,
    ) {
        LOG.info("params: $params")
    }
}

Actual Behaviour

Default values are silently ignored.

Environment Information

No response

Example Application

No response

Version

4.7.4

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions