Skip to content

Kotlin default values are silently ignored in POST body #1073

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

Open
distinctdan opened this issue Apr 8, 2025 · 2 comments
Open

Kotlin default values are silently ignored in POST body #1073

distinctdan opened this issue Apr 8, 2025 · 2 comments
Labels
type: bug Something isn't working

Comments

@distinctdan
Copy link

distinctdan commented Apr 8, 2025

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

@dstepanov
Copy link
Contributor

This works with KSP, but I believe Boolean and Long need to be nullable

@dstepanov dstepanov closed this as not planned Won't fix, can't repro, duplicate, stale Apr 9, 2025
@dstepanov dstepanov reopened this Apr 9, 2025
@distinctdan
Copy link
Author

Thank you so much for taking a look at this!

@ojebari-ma ojebari-ma added the type: bug Something isn't working label May 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants