Replies: 2 comments 6 replies
-
thanks for the detailed post! TBH Kompendium has not been getting a lot of attention from me as I've been out of the Kotlin ecosystem for a while now :( It certainly seems weird that one of the
This is, to my understanding, not strictly true. I think certain languages (javascript?) make a distinction between a field being present in a dictionary and set to null vs. not being included at all in the payload. My understanding is that this is the semantic difference between a required nullable field vs a non-required field when it comes to openapi specs. |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Hey everyone!
I've been checking out Kompendium for a while now and have to say, it's pretty cool and seems to be the most advanced option for Ktor. I'm especially excited about the "complex scalars" feature.
But there's one thing that's been confusing me. It's how nullable fields get handled in the schema.
Let's say we have this class:
Kompendium turns it into this schema:
So, this basically says "
field
can be an empty object or a string." Plus, it's implied to be optional since it's not in therequired
array.I was pretty surprised by this. If you compare it to what the Ktor plugin for IDEA generates:
This seems much more straightforward and makes sense to me.
I dug into Kompendium's source code and found out that this behavior with nullable types isn't a bug, but quite contrary it's a tested feature. According to the tests, a nullable type should be described as:
But in my case, it's:
The missing
"type": "null"
does seem to be a bug, causing all sorts of confusion for openapi visualizers like redoc and scalar, and for client code generators too.However, even with the intended
"type": "null"
behavior, I don't get why this is necessary. Nullability already means the field is optional, which is indicated by it not being inrequired
. And vice versa if it's not nullable, it's automatically inrequired
. So why the need foroneOf
? It makes the schema super complex and heavy. In my case, all fields in all data classes are nullable, and reading the documentation generated that way is a mind-blowing experience I must tell you.I found a test in Kompendium's code called "Nullable fields do not lead to doom," which tests this scenario. Can someone explain the reasoning behind this decision and what kind of "doom" the authors wanted to avoid? And is there a way to make this behavior configurable?
Thanks a ton for any insights!
Sergey
Beta Was this translation helpful? Give feedback.
All reactions