Hello! I'm trying to generate json schema from sev...
# ksp
g
Hello! I'm trying to generate json schema from several data classes (to be able to check KMP clients with legacy backend). Is there an open-source project doing something similar?
j
not sure about your specific ask here, but for generating json from data classes, maybe you can take a look at moshi?
m
@glureau I’m looking for the same thing: generating a JSON Schema document from certain Kotlin `data class`es that I’ve written. Makes it easier to tell partners what we expect when sending us data. What did you find? Anything provided by Kotlin itself?
@Jiaxiang I do not see anything in moshi or moshix (what you linked to) that generates JSON Schema from Kotlin classes. Are you aware of such a tool? I saw https://victools.github.io/jsonschema-generator/#introduction but not much else in that space, surprisingly.
g
I didn't make progress on that part. I may write a tool for my needs in the next weeks, but looking at the tool you shared, there is a lot of work to achieve a full compatibility, and I don't think I can find that much time.
After some research, I found that kotlinx.serialization has contained temporarily a similar feature in 2018, but looks like it never reached a release https://github.com/Kotlin/kotlinx.serialization/tree/55650ab54cd35729861dcce6abe0e[…]9f0/runtime/common/src/main/kotlin/kotlinx/serialization/schema (from this PR). I also found this repository that use kotlinx.serialization to generate json schema, not updated recently but it may be good enough... if you're using kotlinx. Is it the case @Matthew Adams? (I've started a draft but if I can avoid maintaining another project... 😄)
Just for future readers, installation and setup is super easy, simple case looks good. As you can define the annotation @Serializable on a
sealed interface
you can have hierarchy/polymorphism with anyOf but it will be limited to the compilation module, not sure how to do it if you have a gradle multi-modules project with a (non-sealed) interface...
And here we go for a polymorphism attempt on a fork, if it can help https://github.com/glureau/json-schema-serialization/tree/glureau
353 Views