mbknor / mbknor-jackson-jsonSchema

Generate JSON Schema with Polymorphism using Jackson annotations
MIT License
235 stars 79 forks source link

JsonSchema*-Annotations ignored in kotlin code #129

Open stkleinbaum opened 4 years ago

stkleinbaum commented 4 years ago

Hey Morten,

thanks for this great library, but I have troubles to get it working in kotlin.

@Test
internal fun `json schema from object`() {
    val generator = JsonSchemaGenerator(jacksonObjectMapper())
    val schema = generator.generateJsonSchema(Example::class.java)
    println(schema.toPrettyString())
}

class Example {
    @JsonSchemaInject(
            ints = [
                JsonSchemaInt(path = "minLength", value = 3),
                JsonSchemaInt(path = "maxLength", value = 10)
            ]
    )
    val aString: String = ""

    @Size(min = 3, max = 10)
    val aString2: String = ""
}

produces the json schema

{
  "$schema" : "http://json-schema.org/draft-04/schema#",
  "title" : "Config Title",
  "description" : "Config Description",
  "type" : "object",
  "additionalProperties" : false,
  "properties" : {
    "astring" : {
      "type" : "string"
    },
    "astring2" : {
      "type" : "string"
    }
  },
  "required" : [ "astring", "astring2" ]
}

but minLength and maxLength are not set.

Do you have an idea?

Best

big-andy-coates commented 3 years ago

Not sure, for what its worth, I added your annotations to the KotlinWithDefaultValues type in the repo:

data class KotlinWithDefaultValues(
    val optional: String?,
    val required: String,
    @JsonSchemaInject(
            ints = [
                JsonSchemaInt(path = "minLength", value = 3),
                JsonSchemaInt(path = "maxLength", value = 10)
            ]
    )
    val optionalDefault: String = "Hello",
    val optionalDefaultNull: String? = "Hello"
)

...and the schema was as expected:

{
  "$schema" : "http://json-schema.org/draft-04/schema#",
  "title" : "Kotlin With Default Values",
  "type" : "object",
  "additionalProperties" : false,
  "properties" : {
    "optional" : {
      "type" : "string"
    },
    "required" : {
      "type" : "string"
    },
    "optionalDefault" : {
      "type" : "string",
      "minLength" : 3,
      "maxLength" : 10
    },
    "optionalDefaultNull" : {
      "type" : "string"
    }
  },
  "required" : [ "required", "optionalDefault" ]
}

Have you tried the latest version of the library?

AScully24 commented 3 years ago

Long time since you posted the original issue, but thought this may help others who land here.

Have you tried prepending "@field:JsonSchemaInject"?

@Test
internal fun `json schema from object`() {
    val generator = JsonSchemaGenerator(jacksonObjectMapper())
    val schema = generator.generateJsonSchema(Example::class.java)
    println(schema.toPrettyString())
}

class Example {
    /* 
     * Place it right here 
     */
    @field:JsonSchemaInject(
            ints = [
                JsonSchemaInt(path = "minLength", value = 3),
                JsonSchemaInt(path = "maxLength", value = 10)
            ]
    )
    val aString: String = ""

    @Size(min = 3, max = 10)
    val aString2: String = ""
}

Kotlin's generates getters and setters under the hood at compile time, so it's better to be explicit with intent or you may not get the results you want.