-
Notifications
You must be signed in to change notification settings - Fork 59
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
Support Kotlin annotations #358
Comments
It looks like I jumped to a conclusion too quickly as this doesn't seem to be a general problem with Kotlin annotations, as I was able to successfully create a correct schema for a Kotline class like so: class TestNamedProperty2 {
@JsonProperty("my_text")
val text: String = ""
} So it seems that the issue is specific to Kotlin data classes. I am writing a schema generator that uses |
Hi @moranlefler, It's very unfortunate that the annotation goes to the constructor parameter in this scenario. As starting point, you can extend the Line 194 in 5965fdd
Preferably, the handling would be generic enough to allow being included via a PR. |
Created a PR for this. Feedback welcomed! |
@moranlefler class TestNamedProperty2 {
@get:JsonProperty("my_text")
val text: String = ""
} I stumbled upon the same issue using swagger 2 annotations and that did the job. |
If this is indeed setting the annotations on the getter methods instead of the constructor, that'd be awesome as no change would be required here. I'd merely include this in the documentation then, to share this with other Kotlin users. Thanks for sharing! |
This is indeed setting the annotations on getter methods as shown by the output of private data class MyParams(
@get:Schema(title="Jinja input", description = "Whole document if left empty, otherwise 'metadata.input' syntax accepted", defaultValue = " ")
val input: String = "",
)
|
I too would love to see this available in kotlin. What I have found is by adding a @get:JsonProperty or @field:JsonProperty to you data class properties fixes the issue, however what about the kotlin classes that are out of your control. This also has a general problem with kotlin data classes, as the @min from jakarta.validation-api also cannot work effectively. @get:Min works and hence the problem is a kotlin issue, not that kotlin is going to fix it. Possible work around... |
@SquirrelGrip I'm not sure I'm getting your point: if a Java class is out of your control then you cannot add annotations (min, JsonProperty) to it in order to tweak the behavior of the json schema generation, right? So why would it be required for Kotlin? In my understanding there is no issue with Kotlin and no issue with jsonschema-generator (and this ticket should be closed :)). |
It seems that the Jackson module ignores Jackson annotations in my Kotlin code, e.g.:
The generator generates a schema with the property name
text
instead ofmy_text
. I traced the generation with a debugger, and it seems that the methodgetPropertyNameOverrideBasedOnJsonPropertyAnnotation
in the Jackson module does not see the annotation in the Java class that I created from Kotlin usingTestNamedProperty::class.java
.I suspect that it has to do with how Kotlin and Java annotations differ - https://stackoverflow.com/questions/72559287/how-to-read-kotlin-annotation
I guess that means that
jsonschema-generator
cannot be used from Kotlin, which is a bummer for my use case :(The text was updated successfully, but these errors were encountered: