Closed clragon closed 9 months ago
Hi,
- Why does this example mark a field that can not be null as nullable?
No real technical reason, just a mistake on my side. Annotating a non-nullable field as nullable does not make sense. The annotation only affects the schema and has no effect on the "real" logic/model. Thanks for the hint - i'll change it.
- Why are fields which can be null in Kotlin not marked as nullable in the Swagger.json?
Classes are transformed into a json-schema using a another library, which is written in java, so it probaby doesnt know the difference between a nullable and a non-nullable field and can't automatically detect it itself.
Regarding the automatic detection of nullable fields, i think its possible to extend the schema-generator with custom modules/addons. This way i should be able add the nullable-detection "relativly" easily - i'll look into that.
After some reasearch, i dont think automatic detection of nullable fields is easily possible with the current setup. The json-schemas are generated with the help of Java's reflection, so all information specific to kotlin is lost. I'll leave it as is for now, though i may have an idea for the future ;)
Thank you for looking into it. This certainly throws a wrench into my machinations but I suppose there is nothing we can do.
I have worked around this by painstakingly marking all fields by hand:
data class User(
@field:Schema(required = true)
val id: Int,
@field:Schema(required = true)
val name: String,
@field:Schema(required = true)
val rank: UserRank,
@field:Schema(required = true)
val strikes: Int,
@field:Schema(required = true)
val isBanned: Boolean,
)
data class UserUpdate(
@field:Schema(nullable = true)
val name: String? = null,
@field:Schema(nullable = true)
val rank: UserRank? = null,
@field:Schema(nullable = true)
val strikes: Int? = null,
@field:Schema(nullable = true)
val isBanned: Boolean? = null,
)
With this setup, my Models actually generate correctly with the openapi-generator. This is better than wrong typing at least.
Pre-amble: I am quite new to Kotlin, so I might be missing something very obvious.
We assume I have the following model:
In the model we can see that a
User
has aname
which is aString
andpronouns
which is aString
orNull
.However, in the Swagger.json, we see:
We can spot that
pronouns
was not marked as"nullable": true
.In the Example section in
SchemaAnnotations
we can see:which defines a property
age
which isInt
. However, the schema annotation marks it asnullable
, meaning it is actuallyInt
orNull
.I dont quite understand this disconnect.
null
in Kotlin not marked asnullable
in the Swagger.json?null
asnullable
?From what I understand, the
nullable
annotation should transform thetype
of a field fromType
toType?
. Has this behaviour just not been implemented in thektor-swagger-ui
package or am I misunderstanding something?