What options exist for converting JSON schemas (no...
# getting-started
t
What options exist for converting JSON schemas (not payloads) into Kotlin data classes? I have tested the JsonToKotlinClass plugin via IntelliJ which comes close, but it does not seem to interpret required / optional directives correctly. E.g. given a schema with two properties, one required and the other optional -
Copy code
{
  "$schema": "<http://json-schema.org/draft-07/schema#>",
  "$id": "<https://abc.com/request.schema.json>",
  "title": "Request record",
  "type": "object",
  "properties": {
    "id": {
      "description": "The unique ID of a request",
      "type": "string"
    },
    "name": {
      "description": "The name of a request",
      "type": "string"
    }
  },
  "required": [
    "id"
  ]
}
I would expect to receive output like -
Copy code
data class Request(
    val id: String,
    val name: String?
)
but instead, this is generated (both fields are marked as required) -
Copy code
data class Request(
    val id: String,
    val name: String
)
Ideally I'd like to find a solution that can be implemented programatically too, rather than requiring a manual plugin (though that's not as important as getting the nullable part right).
e
Not that I know the answer, but out of curiosity want to ask a question: are json schemas a thing? Where do you encounter them in practice?
t
They're useful for defining the shape of data in projects where multiple systems will interact with the data (Kafka, spark, ML etc.) I think I found a way to make the plugin work, thanks for responding anyway.
m
Json schemas are a vital part of REST API Description and DDL technologies, like HATEOAS, OpenAPI, RAML, json:api, Apache Avro, etc. Moreover Json schemas should be used everywhere there is a Json data exchange, otherwise you’re missing the benefits of validation, versioning and elimination a whole class of bugs.
e
I’m curious of where they are used, not where they can be used. Do you have any big name REST API example that uses them?
s
all of Swagger and Open API ecosystem are based on it.
e
Example of it being <well-known-and-popular> REST API…?
The reason I’m curious, is that I want to really be able to play with it in practice on a service somebody else created, not in my own playground.
s
Gotcha. It has many applications in practice - it's a large ecosystem including MSFT/AWS/Google all using it and having productized features around it in their various clouds.
the two easiest to grok use-cases in my mind are client/server generation and runtime data validation.
e
Can you simply point to a website or webservice that uses it and which I can play with?
s
here's Stripe's open API spec. it uses/relies on JSON schema: https://github.com/stripe/openapi
@Terry Franklin I had the same question a couple months ago and came across the same project which seemed insufficient. We started to build our own within another project: https://github.com/mParticle/smartype/blob/master/smartype-generator/src/main/kotlin/com/mparticle/smartype/generator/SmartypeObject.kt
it's too specific for your needs though. A good insight we had though was using Kotlin Poet for the code generation.
t
@Sam Dozor will take a look for inspiration, thanks!