0

We want to have a system that allows us to define data schemas that we can use to validate our data, and to generate code in specific languages. We found json schema's that lets us do something like

File "message.json.schema"

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "title": "Message",
    "properties": {
            "name": {
        "type" : "string"
        },
    
            "type": {
                "$ref": "type/message_type.schema.json"
            },

        "message_id":{
            "$ref": "type/uuid.schema.json"
        }
    },
    "required": ["name", "message_id"]
}

File "message_type.json.schema"


{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "title": "MessageType",
    "enum": ["Message", "Query"]
}

File "uuid_type.json.schema"

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "title": "UUID",
    "type": "string",
    "pattern": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$"
}

File "query.json.schema"

{
    "$schema": "https://json-schema.org/draft/2019-09/schema",
    "title": "Query",
    "allOf" : [ {"$ref": "type/message.schema.json" }],
    "required": ["type"]
}

Please ignore if there is something that doesn't make sense but the point is, we really enjoy this system because it allows us to define types, and to refer to types that we create in another files, and even to use them like for type inheritance.

Then we want to use this files for code generation and validation. In python we then use a library called python_jsonschema_objects that can parse this files and the files that it references recursively, and we can then really simply create a python object with all the validation included.

But then we also want to use them for Java/Kotlin but the library that we found jsonschema2pojo doesn't seem able to parse linked files expecting everything to be in the same file.

This leads us to think that for some reason Json Schema is not that supported or used, unfortunately.

So, we have the question if a system like Avro or OpenAPI would be better supported and more widely used and could be chosen to this type of task.

user246100
  • 670
  • 1
  • 11
  • 20
  • 1
    JSON Schema doesn't define how to do code generation. OpenAPI tooling generally does, but you won't be able to use newer versions of JSON Schema in it currently. Pre-OAS 3.1, OpenAPI uses a subset superset of JSON Schema draft-4. Any JSON Schema tooling which generates code is not likely to support current versions of JSON Schema, nor behave the same as similar tools. – Relequestual Sep 14 '20 at 12:32
  • Im looking more at OpenAPI but it seems that it always revolves around the definition of http services, we just want to define types, we don't want this to create a REST server. – user246100 Sep 14 '20 at 15:06
  • 1
    have you tried this one? https://github.com/schlothauer-wauer/jsoncodegen "references in the local document and local file system are supported" – aeberhart Sep 25 '20 at 16:06
  • @aeberhart for now the solution we've resorted to, is to use a node library that compiles a json schema with references to a single json schema file, and then another node library that parses the allOf keyword and transforms inheritance into single objects. Then jsonschema2pojo can parse that json schema file and generate code. This has some caveats but is the best we have. We ignored Avro because it has no inheritance. Im gonna check your suggestion, thank you! – user246100 Sep 29 '20 at 17:38

0 Answers0