spring-projects / spring-projects/spring-framework

Default parameter values can't access class scope?

Open
#35,121 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status: waiting-for-triage theme: kotlin
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

I have a spring service with some injected values. But all injected values are nullwhen accessing them from a default parameter value.

Steps to reproduce

  1. Write a simple spring component (service)
  2. Set a default for an optional parameter and reference an injected (non-nullable) member property
  3. Call this method

Sample:

@Service
class MyService(
  val repo: MyRepository // Injected from Spring
)

fun callDoSomething() {
  // `repo` is set when accessed from here
  println("Repo: $repo")
 
  // works
  update(case =  repo.getCase()) // works

 // throws NPE on runtime
  update()
}

fun doSomething(case: CaseBE = repo.getCase()) { // repo is null when accessed from parameter list
  // ...
}

Expected result:

Result of expression is assigned to parameter

Actual result:

Null-pointer exception because all injected properties are null for thiswhen evaluated as default parameter. Seems like the scope of the class is comlpletely lost which is highly unexpected.

It does not even work with an explictly scoped this:

fun doSomething(case: CaseBE = this@MyService.repo.getCase()) { // still NPE when accessing `repo`
  // ...
}

I even tried it with self-reference, same here:

@Service
class MyService(
  val repo: MyRepository // Injected from Spring,
  @Lazy self: MyService, // self-reference as attempted workaround
)

fun callDoSomething() {
  // `repo` is set when accessed from here
  println("Repo: $repo")
 
  // works
  update(case =  repo.getCase()) // works

 // throws NPE on runtime
  update()
}

fun doSomething(case: CaseBE = self.repo.getCase()) { // repo is null when accessed from parameter list
  // ...
}

I reported that issue to the Kotlin Team (https://youtrack.jetbrains.com/issue/KT-78600) but they immediately redirected me to here because they are convinced that this is an issue with Spring itself or the kotlin-spring plugin.

Thank you.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the MyService example with the Kotlin Spring service and kotlin-spring plugin, comparing an explicit argument with the default parameter expression. Use the reported KT-78600 context to determine whether the behavior belongs to Spring or the Kotlin plugin; done means identifying ownership and adding a focused regression test or clear handoff.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, spring
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.