Skip to content
This repository has been archived by the owner on Mar 19, 2024. It is now read-only.

registerGenerator doesn't work since Kotlin 1.4 #9

Closed
thecodinganalyst opened this issue Jan 20, 2022 · 3 comments · Fixed by #10
Closed

registerGenerator doesn't work since Kotlin 1.4 #9

thecodinganalyst opened this issue Jan 20, 2022 · 3 comments · Fixed by #10

Comments

@thecodinganalyst
Copy link

The following code in registerGenerator() will always return Any().

val returnType = generator.reflect()!!.returnType.withNullability(false)

Works ok in Kotlin 1.3, but not from Kotlin 1.4 onwards

@thecodinganalyst
Copy link
Author

##Workaround##
the workaround solution is to use the .withValue function of the InstanceCreator class, to manually specify the property name to a specific value, before creating the arbitrary object with the .createInstance() function.

##Explanation: ##
Normally before kotlin 1.4, we can specify which class should return what value with the .registerGenerator() function. But since this registerGenerator() is not working anymore, another way is to specify what property name (of the class which you want to create an arbitrary object) should return what value.

##Example##

Given ClassToCreateArbitraryValue has property "property" of type "SomeBusinessClass", instead of

val dataClass = ClassToCreateArbitraryValue::class
val arbitrator = dataClass.arbitrator()
arbitrator.registerGenerator { SomeBusinessClass() }
return arbitrator.createInstance()

do this instead:

val dataClass = ClassToCreateArbitraryValue::class
val arbitrator = dataClass.arbitrator()
arbitrator.apply {
    this.withValue("property", SomeBusinessClass())
}
return arbitrator.createInstance()

@sgerber-prospection
Copy link
Contributor

Hey, this issue was just brought to my attention. Not sure if anyone inside Tyro is watching this repo. I'll have a look soon and see if I can work out how to commit back to this from the outside.

@steveorourke
Copy link
Contributor

arbitrater v1.0.1 has been released and should be available on maven central shortly. Thanks @thecodinganalyst and @sgerber-prospection for your help with this issue.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants