Skip to content
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

Attributes outside constructor in data class cause exception #654

Open
marthursson opened this issue Nov 7, 2024 · 1 comment
Open

Attributes outside constructor in data class cause exception #654

marthursson opened this issue Nov 7, 2024 · 1 comment

Comments

@marthursson
Copy link

Describe the bug
When a data class that has a val not included in its constructor is referenced in documentation it causes an exception on startup:

 io.bkbn.kompendium.json.schema.exception.UnknownSchemaException: An unknown type was encountered: class com.example.SomeClass.  
    This typically indicates that a complex scalar such as dates,
      timestamps, or custom number representations such as BigInteger were not added as custom types when
      configuring the NotarizedApplication plugin.  If you are still seeing this error despite adding all
      required custom types, this indicates a bug in Kompendium, please open an issue on GitHub.
at io.bkbn.kompendium.json.schema.handler.SimpleObjectHandler$handle$required$3.invoke(SimpleObjectHandler.kt:75)
at io.bkbn.kompendium.json.schema.handler.SimpleObjectHandler$handle$required$3.invoke(SimpleObjectHandler.kt:70)
at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:171)
at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:194)
at kotlin.sequences.TransformingSequence$iterator$1.hasNext(Sequences.kt:214)
at kotlin.sequences.SequencesKt___SequencesKt.toSet(_Sequences.kt:849)
at io.bkbn.kompendium.json.schema.handler.SimpleObjectHandler.handle(SimpleObjectHandler.kt:85)

This is because SimpleObjectHandler attempts to find the attribute in the constructor and - failing to do so - throws an exception.

To Reproduce
Steps to reproduce the behavior:
Example data class:

data class SomeVO(
  val id: String,
  val name: String,
  val category: String,
) {
  val slug: String = id.toSlug()
}

The point of this approach is that this attribute should always have the exact same value depending on the id, so it should not be possible to send it to the constructor.

Expected behavior
The most reasonable behaviour would be to mark this attribute as required, because this only ever makes sense for return values, and the attribute will always have a value.

Additional context
There are obviously workarounds for this. One possible solution is to include the attribute in the constructor, with a default value:

data class SomeVO(
  val id: String,
  val name: String,
  val category: String,
  val slug: String = id.toSlug()
)

While this works, it is misleading because it indicates that any slug can be supplied to this class. It will also make the attribute optional in the schema, which is not the intention.

@brizzbuzz
Copy link
Contributor

Yea... finding a good middle ground here is very tough... because for many classes, people will not want a variable exposed as part of the documentation.

I believe by default kotlinx serialization will omit vals that are not part of the constructor... but i'm not sure about that

I'm definitely up for attempts to implement a better solution here, but to be quite honest, Kompendium is very low priority for me personally, so will likely have to be a community member that takes charge here.

Happy to review PRs / continue discussion tho :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants