Skip to content
This repository was archived by the owner on Feb 23, 2023. It is now read-only.
This repository was archived by the owner on Feb 23, 2023. It is now read-only.

Fix @ConstructorBinding on non nested class #382

Closed

Description

@fer-marino

Hi everybody,
I have the following application: https://github.com/fer-marino/spring-native-issue

It is a spring boot 2.4 kotlin application that is supposed to be used from command line. It uses an application.yml file located in the classpath and mapped to data classes via the @ConfigurationProperty annotation. This works fine on the JVM but it doesn't with graal native. The application compiles fine but when it start, it fails with a classic error saying it can't bind yml file to object properties.

APPLICATION FAILED TO START
***************************

Description:

Binding to target [Bindable@25f04156 type = java.util.List<org.eumetsat.coda.maintenance.configuration.CodaInstance>, value = 'none', annotations = array<Annotation>[[empty]]] failed:

    Property: coda.instances[0].description
    Value: OFL Primary Backend
    Origin: class path resource [application.yml] - 7:18
    Reason: The elements [....] were left unbound```

Activity

  1. added
    status: waiting-for-triage
    on Nov 25, 2020
  2. added this to the 0.9.0 milestone on Dec 15, 2020
  3. added
    type: compatibility
    and removed
    status: waiting-for-triage
    on Dec 15, 2020
  4. sdeleuze commented on Dec 15, 2020

    @sdeleuze
    Contributor

    I can reproduce it with 0.8.4 as well, maybe a @ConstructorBinding support related issue?

  5. fer-marino commented on Dec 15, 2020

    @fer-marino
    Author

    it is a possibility, as that annotation relies on reflection to infer the constructor to bind to. Unfortunately I have no experience with graal and I don't know how to proceed.
    After a quick check anyway I can tell you that I see support for @ConstructorBinding annotation in this project, but I don't see support for org.springframework.core.KotlinDetector which is used here. Maybe this is kotlin specific?

  6. added
    type: bug
    and removed
    type: compatibility
    on Jan 8, 2021
  7. jimbethancourt commented on Jan 14, 2021

    @jimbethancourt

    I'm running into a similar problem with a Java application and nested yaml properties. I can work around it to some extent, but it's not always the easiest to figure out how.

  8. sdeleuze commented on Jan 28, 2021

    @sdeleuze
    Contributor

    @jimbethancourt Could you please provide some details on the workaround you are using?

  9. self-assigned this
    on Jan 28, 2021
  10. jimbethancourt commented on Jan 28, 2021

    @jimbethancourt

    Hi @sdeleuze,
    You bet - I'll put something together tonight.

    Thanks,
    Jim

  11. jimbethancourt commented on Jan 29, 2021

    @jimbethancourt

    Hi @sdeleuze
    I didn't have an opportunity to put something together tonight - I'll try to put something together very soon.

    Short and sweet version is that I need to

    1. Specify the top level prefix in the @ConfigurationProperties(prefix = "my.properties") for a parent class
    2. Specify nested prefixes in the @ConfigurationProperties(prefix = "my.properties.that-are-nested") for a child class
    3. Declare the child class as a member of the parent class, and then inject it.

    Without Spring Native, the child object is created without issues. With Spring Native, I need to use a hierarchical approach.

    Thanks,
    Jim

  12. sdeleuze commented on Feb 3, 2021

    @sdeleuze
    Contributor

    This bug happens because only nested types are registered automatically, not the one returned by the getter.

  13. sdeleuze commented on Feb 3, 2021

    @sdeleuze
    Contributor

    @aclement I can reproduce it with the following modified boot-features sample and have crafted a draft fix for it (it fixed the issue) by adding configuration for return types instead of nested types, see sdeleuze@non-nested-configuration-properties. Could you provide me some guidance and/or take it from there?

    Notice that with the log I added I see that a lot (all?) properties are processed, whatever we use them or not. So I see reflection entries for properties like ArtemisProperties. I have the feeling that we don't filter out the @EnableConfigurationProperties on excluded auto-configurations. Should I create an issue for that, and if yes, for which version?

  14. jimbethancourt commented on Feb 4, 2021

    @jimbethancourt

    @sdeleuze and @aclement do you need the sample I mentioned? I apologise for not getting it to you yet.

  15. sdeleuze commented on Feb 9, 2021

    @sdeleuze
    Contributor

    @jimbethancourt No need for the repro thanks.

  16. sdeleuze commented on Feb 16, 2021

    @sdeleuze
    Contributor

    I will discuss with @bclozel tomorrow to try to define a reasonable and reliable implementation strategy.

  17. changed the title [-]annotation processor and yaml files[/-] [+]Fix @ConstructorBinding on non nested class[/+] on Feb 17, 2021
  18. fer-marino commented on Mar 29, 2021

    @fer-marino
    Author

    sorry but my project still not run, even with the latest 0.9.1 version and spring boot 2.4.4. New error anyway:

    java.lang.ClassNotFoundException: java.util.List at com.oracle.svm.core.hub.ClassForNameSupport.forName(ClassForNameSupport.java:60) ~[na:na] at java.lang.ClassLoader.loadClass(ClassLoader.java:281) ~[na:na] at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.parseType(KDeclarationContainerImpl.kt:267) ~[na:na] at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.loadParameterTypes(KDeclarationContainerImpl.kt:258) ~[na:na] at kotlin.reflect.jvm.internal.KDeclarationContainerImpl.findConstructorBySignature(KDeclarationContainerImpl.kt:229) ~[na:na] at kotlin.reflect.jvm.internal.KFunctionImpl$caller$2.invoke(KFunctionImpl.kt:67) ~[na:na] at kotlin.reflect.jvm.internal.KFunctionImpl$caller$2.invoke(KFunctionImpl.kt:36) ~[na:na] at kotlin.reflect.jvm.internal.ReflectProperties$LazyVal.invoke(ReflectProperties.java:62) ~[na:na] at kotlin.reflect.jvm.internal.ReflectProperties$Val.getValue(ReflectProperties.java:31) ~[na:na] at kotlin.reflect.jvm.internal.KFunctionImpl.getCaller(KFunctionImpl.kt) ~[na:na] at kotlin.reflect.jvm.ReflectJvmMapping.getJavaConstructor(ReflectJvmMapping.kt:71) ~[na:na] at org.springframework.beans.BeanUtils$KotlinDelegate.findPrimaryConstructor(BeanUtils.java:827) ~[na:na]

  19. sdeleuze commented on Mar 29, 2021

    @sdeleuze
    Contributor

    @aclement Do you think we should reopen this issue?

  20. aaronriekenberg commented on Apr 15, 2021

    @aaronriekenberg

    I had the same issue as @fer-marino . For me putting:

      {
        "name": "java.util.List"
      },
    

    into refect-config.json solved this issue.

    From what I can see this happens when a class using @ConfigurationProperties and @ConstructorBinding contains a List member, as here: https://github.com/fer-marino/spring-native-issue/blob/master/src/main/kotlin/org/eumetsat/coda/maintenance/configuration/CodaConfiguration.kt#L7

    Avoiding use of List in @ConfigurationProperties and @ConstructorBinding classes also avoids this issue for me.

    Thanks!

  21. aclement commented on Apr 15, 2021

    @aclement
    Contributor

    I've tweaked the configuration properties processing a little and the case I could recreate as failing is now working.

  22. AndreKoepke commented on Dec 3, 2022

    @AndreKoepke

    Same problem here with Spring 3.0.0, java-properties and gradle. I didn't found an option to set a "refect-config.json".

    https://github.com/AndreKoepke/home-system/blob/9d35fc9c6e5fc6d7234ee871134902246c61e695/src/main/java/ch/akop/homesystem/config/properties/HomeSystemProperties.java#L35

    Can I fix that without refactoring all configs classes?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

type: bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions