huehnerlady

    huehnerlady

    1 year ago
    Hi, I currently try to generate a json schema from kotlin classes and generating kotlin classes from json schema. For my first part - creating schema from kotlin classes, I ran into this post on stack overflow and there I was pointed to kotlinx.serialization at a very early stage (see here). unfortunately this folder was completely removed in this commit. Now I cannot find any hint of similar classes or how I could achieve just that. Is this still possible with kotlinx.serialization? If so — how? If not — any other idea how I can achieve that?
    Nikky

    Nikky

    1 year ago
    https://github.com/Ricky12Awesome/json-schema-serialization does serializer -> schema i have a fork that does some adjustments to match my usecase.. but YES it is possible
    as for generating code from json-schema.. i think i searched for that before.. but in the end.. json-schema allows some combinations that are very hard to model using kotlin classes so i gave up on that
    i am not entirely sure what i did in the fork.. but i am using it in some projects to generate schemas.. for easier config writing (intellij idea works very well, vscode okayyish) https://github.com/NikkyAI/json-schema-serialization
    huehnerlady

    huehnerlady

    1 year ago
    @Nikky Thank you for that, I did stumble over the problem around kotlin generation from json schema, too. I now found this project and have a look further into it. so far it is not 100% what I am looking for, but I feel it gets in the right direction and has potential. With the repositories you linked I ran into that one, too. The problem I had was the ton of annotations it seems to need for working (I did not look at it in detail, but from the example it looks like you do need annotation on everything for it to work). So I dismissed that. I found now this project to have good potential. It is for java classes, but so far it works for kotlin as well. the nullable/nonnullable is obviously not supported, as java does not have that pattern, but we have everything nullable at the moment anyways, so it works. So both of these together have a good potential, although it is not exactly what I want yet and so need to look if a few tweaks might make it THE solution. If someone has other ideas I would be happy to hear them 🙂
    Nikky

    Nikky

    1 year ago
    it works without the annotations.. but some things like descriptions and valid ranges for values.. it just won't be able to guess without the annotations it produces a perfectly working schema without it it can know what valid values are for enums represented as a string.. but it won't be able to guess a validation regex for a email
    huehnerlady

    huehnerlady

    1 year ago
    Ah cool, I will have a second look then, thanks 🙂
    @Nikky I did try it now and unfortunately the schema and the enums are unfortunately inlined. Maybe that is possible to prevent with annotations, but seems not to work out of the box: Kotlin:
    @Serializable
    data class Foo(
        val field1: String? = null,
        val field2: FooEnum? = null,
        val field3: FooEnum? = null,
    )
    
    @Serializable
    enum class FooEnum {
      VALUE_1, VALUE_2
    }
    {
      "$schema": "<http://json-schema.org/draft-07/schema>",
      "type": "object",
      "properties": {
        "field1": {
          "if": {
            "type": "string"
          },
          "else": {
            "type": "null"
          }
        },
        "field2": {
          "if": {
            "type": "string"
          },
          "else": {
            "type": "null"
          },
          "enum": [
            "VALUE_1",
            "VALUE_2"
          ]
        },
        "field3": {
          "if": {
            "type": "string"
          },
          "else": {
            "type": "null"
          },
          "enum": [
            "VALUE_1",
            "VALUE_2"
          ]
        }
      },
      "definitions": {}
    }
    With definition it looks even worse as the names are auto-generated:
    {
      "$schema": "<http://json-schema.org/draft-07/schema>",
      "$ref": "#/definitions/x1a1q3f9pvqxof",
      "definitions": {
        "x1a1q3f9pvqxof": {
          "type": "object",
          "properties": {
            "field1": {
              "$ref": "#/definitions/x1oxo2fpbh9ren"
            },
            "field2": {
              "enum": [
                "VALUE_1",
                "VALUE_2"
              ],
              "$ref": "#/definitions/x1u18olg46aewv"
            },
            "field3": {
              "enum": [
                "VALUE_1",
                "VALUE_2"
              ],
              "$ref": "#/definitions/x1u18olg46aewv"
            }
          }
        },
        "x1oxo2fpbh9ren": {
          "if": {
            "type": "string"
          },
          "else": {
            "type": "null"
          }
        },
        "x1u18olg46aewv": {
          "if": {
            "type": "string"
          },
          "else": {
            "type": "null"
          },
          "enum": [
            "VALUE_1",
            "VALUE_2"
          ]
        }
      }
    }
    So I will continue with my current approach I think as at least I do not need any annotations for this
    Nikky

    Nikky

    1 year ago
    i don't quite understand why annotations are a bad thing here.. these are marked for serialization to add them to the descriptors.. so it is compiled in and reflection is not necessary.. it also works on kotlinJs with a few small tweaks good luck with your approach
    huehnerlady

    huehnerlady

    1 year ago
    Well it is about the readability and the amount of work you have to do in order to add fields. We want to have this as small as possible and reflection per se is not a problem for us. But thank you very much for the information 🙂
    Nikky

    Nikky

    1 year ago
    i find json schema is unreadable as it is.. even with descriptive names.. so .. i just need something "working" as output
    Dariusz Kuc

    Dariusz Kuc

    1 year ago
    have you looked at https://github.com/OpenAPITools/openapi-generator? you could generate just models (pojos)
    huehnerlady

    huehnerlady

    1 year ago
    @Dariusz Kuc thanks, that was my first try, but this is just for an openapi-yaml and unfortunatelt the
    paths
    variable is mandatory in the spec so it seems not to be designed to use that just for a model. so we had to discard that