1

I faced the issue after upgrade SpringBoot from 2.x to 3.0.1 In kotlin, @ConfigurationProperties deosn't generate 'peroperteis' field in some conditions.

1. Code: 'var' with default value

@ConfigurationProperties(prefix = "cnr.app")
data class AppProperties(
    var name: String = "cma",
    var version: String = "0.0.1"
)

spring-configuration-metadata.json

{
  "groups": [
    {
      "name": "cnr.app",
      "type": "studio.m2s.cnr.cma.AppProperties",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    }
  ],
  "properties": [
    {
      "name": "cnr.app.name",
      "type": "java.lang.String",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    },
    {
      "name": "cnr.app.version",
      "type": "java.lang.String",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    }
  ],
  "hints": []
}

result: Good

2. Code: 'val' with default value

@ConfigurationProperties(prefix = "cnr.app")
data class AppProperties(
    val name: String = "cma",
    val version: String = "0.0.1"
)

spring-configuration-metadata.json

{
  "groups": [
    {
      "name": "cnr.app",
      "type": "studio.m2s.cnr.cma.AppProperties",
      "sourceType": "studio.m2s.cnr.cma.AppProperties"
    }
  ],
  "properties": [],
  "hints": []
}

result: Bad.

In Spring Boot 3.0.1

  • If I declare fields with 'var', it works good.
  • If I declare fields with 'val', it works bad.
  • If I declare fields with 'var' without default value, it works good.
  • If I declare fields with 'val' without default value, it works good.

In Spring Boot 2.7.x

  • If I declare fields with 'var', it works good.
  • If I declare fields with 'val', it works good.
  • If I declare fields with 'var' without default value, it works good.
  • If I declare fields with 'val' without default value, it works good.

gradle.kts

configurations {
    compileOnly {
        extendsFrom(configurations.annotationProcessor.get())
    }
}

kapt {
    annotationProcessor("org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor")
}

tasks {
    withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
        dependsOn(processResources)
    }
}


dependencies {
    kapt("org.springframework.boot:spring-boot-configuration-processor")
    annotationProcessor("org.springframework.boot:spring-boot-configuration-processor")
}

I expect, it works with 'val' with default value. In Spring Boot 3.0.1

  • If I declare fields with 'var', it works good.
  • If I declare fields with 'val', it works good.
  • If I declare fields with 'var' without default value, it works good.
  • If I declare fields with 'val' without default value, it works good.

like spring boot 2.7.x

Brian Clozel
  • 56,583
  • 15
  • 167
  • 176
unD3R
  • 34
  • 5

1 Answers1

0

In Spring Boot 3.0.1

If I declare fields with 'var', it works good (with initial value I assume).

If I declare fields with 'val', it works bad (with initial value I assume).

If I declare fields with 'var' without default value, it works good.

If I declare fields with 'val' without default value, it works good.

This seems more like an issue of the past if it happened and not a current issue of spring-boot 3. Probably not related with spring boot but more with kotlin and jdk.

val in Kotlin is like final in java, meaning it's value could not change once assigned. So it is logical that spring can't reassign another value in case your initial value for that field is already set and the field is declared as final with val.

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
  • but if I downgrade from 3.0.x to 2.7.x, that issue disapeared. Of course, I'm using "io.spring.dependency-management", that means I think this issue related in "org.springframework.boot.configurationprocessor.ConfigurationMetadataAnnotationProcessor" or something. – unD3R Dec 30 '22 at 03:03
  • @unD3R you understand that a final variable should not be modified right? That is what `val` represents. So even if it worked in the past the correct behavior seems to be the current one. – Panagiotis Bougioukos Dec 30 '22 at 11:33
  • @unD3R is this a graddle project? If yes please share your graddle build files both for 3.0.1 version and for 2.7.x version. We might locate where this originates from – Panagiotis Bougioukos Dec 30 '22 at 11:37