Created
October 11, 2017 20:02
-
-
Save dmcg/79cb784789c65c31b9f7311a548d377f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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