-
-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[BUG] Kotlin's kotlin-server generator does not support bean validation #12351
Comments
Extra details is that
|
more cautious/compatible option could be to apply both: |
I stumbled across this issue and needed a quick pragmatic fix. Given a gradle build with kotlin this looks like: tasks.openApiGenerate {
doLast {
// pragmatic fix for https://github.com/OpenAPITools/openapi-generator/issues/12351
val replaceAnnotations = setOf(
"Valid",
"AssertFalse",
"AssertTrue",
"DecimalMax",
"DecimalMin",
"Digits",
"Email",
"Future",
"FutureOrPresent",
"Max",
"Min",
"Negative",
"NegativeOrZero",
"NotBlank",
"NotEmpty",
"NotNull",
"Null",
"Past",
"PastOrPresent",
"Pattern",
"Positive",
"PositiveOrZero",
"Size"
)
val kotlinFiles = File("$projectDir/<path_to_your_model_directory>").listFiles()
kotlinFiles.forEach { file ->
val lines = file.useLines { it.toList() }
val newLines = lines.map {
replaceAnnotations.fold(it) { acc, annotation ->
acc.replace("@$annotation", "@get:$annotation")
}
}
file.writeText(newLines.joinToString("\n"))
}
}
} |
Bug Report Checklist
Description
The code generated for data classes for
kotlin-server
does not specify the annotation's use-site target, meaning that the bean validation can't pick it up due to the annotation generated for the Java elements being in the wrong place (it requires a get- or field-level annotation, while the default is the param one).Context:
https://kotlinlang.org/docs/annotations.html#annotation-use-site-targets
https://stackoverflow.com/questions/35847763/kotlin-data-class-bean-validation-jsr-303
E.g. with a spec containing a property like:
The limit constrain will be generated as:
It has to be generated as:
(this is a minimal change, although I think that
Valid
andJsonProperty
should also get prefixed.NotNull
is probably irrelevant, because Kotlin offers language constructs (?
) that handlenull
ability.It looks like the moustache template doesn't take this functionality into account, while the Spring Kotlin generator does.
openapi-generator version
5.4.0
OpenAPI declaration file content or url
See above.
Generation Details
generatorName = "kotlin-server" with "useBeanValidation" to "true", everything else is - I believe - irrelevant.
Steps to reproduce
Use the definition above API in a Kotlin project that supports bean validation.
I do not have a "minimal" project at hand to test it - I'm using my real one.
Related issues/PRs
None (AFAIK)
Suggest a fix
Replace all
@<BeanValidationAnnotationName>
with@get:<BeanValidationAnnotationName>
or@field:<BeanValidationAnnotationName>
in relevant Moustache templates, e.g.:etc.
I have made the changes locally and can confirm they work, although I'm not sure if they're sufficient for all possible cases etc. - they work for what I need them to work and what I can easily test. I can open a PR if that's desired.
The text was updated successfully, but these errors were encountered: