Is there any common approach to generate Kotlin multiplatfom data classes with validation annotations like JAXB+JSR303 in Java?

Hi all,

I am trying to find any approach to generate Kotlin data models with validation annotations from XSD files to use them in JVM and JS environment as main application models.

Previously in Java I used XJC + Krasa plugin to generate JAXB classes with JSR-303 validations.
Is there any similar way for Kotlin Multi-platform?

I see one complex workaround:

  1. Generate JAXB+JSR303 Java classes as usual
  2. Copy-paste them in IDE to Kotlin classes (more than 4000 classes)
  3. Create expect annotation classes for XML and JSR303 annotations (javax.validation etc.)
  4. Create empty actual annotation classes for javax.validation in JS.
  5. Set Serializable annotation manually to all calsses
  6. Convert XSD schema to JSON Schema to be able to validate JSON objects in JS
  7. Serialize multi-platform model to JSON and validate it via JSON Schema.

But this approach require too much manual work as I have several thousands classes and it does not allow to validate models directly in JS, but only via JSON serialization.

Appreciated for any thoughts…

What you probably need is a Gradle/Maven Plugin to do so. I would recommend to use Kotlin Poet to generate the sources and encapsulate it in a Gradle plugin.

The flow should be something like:

  • parse XSD/XML definitions using Kotlinx.serialization with xmlutil serialization library in a model of your own like data class XSDClass(val name: String, // ecc)
  • use your model to generate sources using Kotlin Poet
  • encapsulate the previous logic in a Gradle Plugin where you define the XSD files in input and where to generate the output sources
  • create a Gradle project with Kotlin JVM (or maybe Multiplatform if you do not need to generate JVM related apis), add your plugin (maybe using includeBuild) and configure it so that it generates the sources in a source folder.
  • enjoy your work!

I made a very similar solution for Kotlingram to generate K/MPP sources where instead of XSD files, my source is the HTML documentation page of Telegram Bot apis; also I did not use Kotlin Poet because I discovered it later and used plain old buildString { } to write the sources. Check out the :generator where the plugin is written and how is applied in :api:kotlingram-core!

1 Like

Adding XSD schema support is on the “in the future” roadmap for xmlultil. Unfortunately XSD is for from straightforward. If you want to do some work in this direction, the library has now been retrofitted to use an explicit schema guiding both serialization and deserialization. You can also get that schema/descriptor manually using the XML.xmlDescriptor(SerialDescriptor) function (it will use the policy you configure to generate the schema). This schema could be used to generate an XSD (the easy bit).

Harder is to generate classes based upon a schema. There are two challenges:

  • Handling the complexities of XSD itself
  • The way the policies work to define the translation between Kotlin to XML is using the SerialDescriptor as a starting point. For this purpose it would be the endpoint.
    • It is possible to do it for policies on individual basis

XML is a more complex language that JSON and cannot be easily translated (there is for example attributes as well as tags, not to speak of mixed content). This would only work for a narrow subset of schemas.

My main goal is not to serialize data to XML, but to automatically create Kotlin Multi-platform models with validation annotations for 4000+ entities defined via XSD. I will serialize them into Proto using kotlinx.serialization.

In my case I have no attributes, but only elements. And in Java I used Jsonix to create JSON Scheme from XSD.