Skip to content
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

kotlin code using inheritance with an abstract class doesn't serialize abstract class field #173

Closed
abnud1 opened this issue Aug 31, 2020 · 8 comments

Comments

@abnud1
Copy link
Contributor

abnud1 commented Aug 31, 2020

We have a kotlin backend project, We're using dsl-json as the JSON serialization library and we have code similar to this:

@CompiledJson
abstract class A(val t: String)

@CompiledJson
class B(val r: String) : A("hello")

The problem that happens is that when serializing B object t property in A class is not serialized, By looking at the generated code We see no trace for the t property.

We tried multiple configuration options for @CompiledJson annotation like minified = false, changing include policy, We even used JsonAttribute annotation like this:

@CompiledJson
abstract class A(
    @JsonAttribute(mandatory = true)
    val t: String
)

But to no avail, here is how We configure the DslJson instance:

val dsljson = DslJson<Any>(
    Settings.basicSetup<Any>()
        .skipDefaultValues(false)
)

We tried multiple things at this code, again nothing works.

Here is full code with example serializing code:

@CompiledJson
abstract class A(val t: String)

@CompiledJson
class B(val r: String) : A("hello")
fun main(){
    val dsljson = DslJson<Any>(
        Settings.basicSetup<Any>()
            .skipDefaultValues(false)
            .includeServiceLoader()
    )
    FileOutputStream("t.json").use {
        dsljson.serialize(B("toto"), it)
    }       
}

we're using the latest version of dsl-json, kotlin is at 1.4, we're using kapt annotation processor
and gradle 6.6.

OS is Windows 10 x64
Java version is 11

@zapov
Copy link
Member

zapov commented Sep 1, 2020

Pattern like this seems to work

@CompiledJson
class B(val r: String, t: String) : A(t) {
    constructor(r: String) : this(r, "hello") {}
}

It would be of great help if someone invested some time and setup a Maven project for testing Kotlin: #158
It's kind of hard to support non-trivial use cases without it

@abnud1
Copy link
Contributor Author

abnud1 commented Sep 2, 2020

@zapov I created a maven project that will illustrate the issue

find it here

I noticed a warning about tools.jar not found, there is this issue regarding this warning but according to jetbrains it was fixed.

Maybe dsl-json is referencing tools.jar in some way ?

For some reason gradle plugin doesn't show this warning !!!

@zapov
Copy link
Member

zapov commented Sep 13, 2020

Hi @abnud1
I tried to create such testing project in February, but gave up...

It would be great if someone can make: https://github.com/ngs-doo/dsl-json/tree/kotlin-tests runnable

I can come closer by doing some hacks, eg putting Kotlin files in main instead of tests, but still it fails due to some dependency problem which I don't really have time to look into.

If you can make that branch compiling and tests running that would be a great start for improved Kotlin support

@abnud1
Copy link
Contributor Author

abnud1 commented Sep 14, 2020

@zapov I'll try and see If I can make those tests running

@abnud1
Copy link
Contributor Author

abnud1 commented Sep 14, 2020

Well mvn test now builds but the kotlin test doesn't run.

And the process is unfortunately very confusing, you have to execute test on parent project not submodule, if you don't want to test all the modules you use -pl subprojectgroupid:subprojectartifactid so command in our case is mvn -pl com.dslplatform:dsl-json-integration-tests-kotlin test -am, you run the command in root project directory, the -am is required otherwise it won't build.

I'll have to see later why the test isn't run even though maven show no errors at all, even with simple mvn test on root project I don't think the kotlin test is run(although I'm not sure really, very big output you know)

@zapov
Copy link
Member

zapov commented Sep 21, 2020

I'm fine with writing Java code for tests which uses Kotlin classes. If you can get that to work via mvn test that should be more than good enough

@zapov
Copy link
Member

zapov commented Oct 4, 2020

This should now work as "expected". Please look over and report back :)

Also finally managed to setup Kotlin tests, so it should be easier to resolve such cases in the future 🎉

@zapov
Copy link
Member

zapov commented Oct 11, 2020

v1.9.6 released

@zapov zapov closed this as completed Oct 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants