Skip to content

Instantly share code, notes, and snippets.

@dmcg
Created October 11, 2017 20:02
Show Gist options
  • Select an option

  • Save dmcg/79cb784789c65c31b9f7311a548d377f to your computer and use it in GitHub Desktop.

Select an option

Save dmcg/79cb784789c65c31b9f7311a548d377f to your computer and use it in GitHub Desktop.
private class KotlinValidationConstraintResolver : AnnotationConstraintResolver() {
override fun getRequired(prop: BeanProperty): Boolean? = super.getRequired(prop) ?: isNullable(prop) == false
}
private fun isNullable(prop: BeanProperty): Boolean? = when (prop) {
is BeanPropertyWriter -> prop.isNullable()
else -> null
}
private fun BeanPropertyWriter.isNullable(): Boolean? {
val member: Member = member.member
return when (member) {
is Method -> {
member.declaringClass.findPropertyWithName(this.name)?.returnType?.isMarkedNullable
}
else -> null
}
}
private fun Class<*>.findPropertyWithName(name: String): KProperty<*>? = this.kotlin.memberProperties.find { it.name == name }
val generator = JsonSchemaGenerator(objectMapper, ValidationSchemaFactoryWrapper(KotlinValidationConstraintResolver())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment