You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When you have some ADT with recursive types - automatic schema derivation will fail in runtime if your schema is implicit:
tapir-test[ERROR] Exception in thread "main" java.lang.ExceptionInInitializerError
tapir-test[ERROR] at Endpoints.<init>(Endpoints.scala:17)
tapir-test[ERROR] at Main$.<clinit>(Main.scala:13)
tapir-test[ERROR] at Main.main(Main.scala)
tapir-test[ERROR] Caused by: java.lang.NullPointerException
tapir-test[ERROR] at sttp.tapir.Schema$.schemaForIterable(Schema.scala:140)
tapir-test[ERROR] at Animal$.$anonfun$schema$1(Animal.scala:22)
tapir-test[ERROR] at magnolia.CallByNeed.value$lzycompute(magnolia.scala:818)
tapir-test[ERROR] at magnolia.CallByNeed.value(magnolia.scala:817)
tapir-test[ERROR] at magnolia.Subtype$$anon$1.typeclass(interface.scala:73)
tapir-test[ERROR] at sttp.tapir.generic.internal.SchemaMagnoliaDerivation.$anonfun$dispatch$1(SchemaMagnoliaDerivation.scala:101)
tapir-test[ERROR] at scala.collection.immutable.ArraySeq.$anonfun$map$1(ArraySeq.scala:71)
tapir-test[ERROR] at scala.collection.immutable.ArraySeq.$anonfun$map$1$adapted(ArraySeq.scala:71)
tapir-test[ERROR] at scala.collection.immutable.ArraySeq$.tabulate(ArraySeq.scala:286)
tapir-test[ERROR] at scala.collection.immutable.ArraySeq$.tabulate(ArraySeq.scala:265)
tapir-test[ERROR] at scala.collection.ClassTagIterableFactory$AnyIterableDelegate.tabulate(Factory.scala:679)
tapir-test[ERROR] at scala.collection.immutable.ArraySeq.map(ArraySeq.scala:71)
tapir-test[ERROR] at scala.collection.immutable.ArraySeq.map(ArraySeq.scala:35)
tapir-test[ERROR] at sttp.tapir.generic.internal.SchemaMagnoliaDerivation.dispatch(SchemaMagnoliaDerivation.scala:101)
tapir-test[ERROR] at sttp.tapir.generic.internal.SchemaMagnoliaDerivation.dispatch$(SchemaMagnoliaDerivation.scala:100)
tapir-test[ERROR] at sttp.tapir.Schema$.dispatch(Schema.scala:99)
tapir-test[ERROR] at Animal$.<clinit>(Animal.scala:22)
But you can still run your app since Animal extends the SchemaDerivation trait. But this trait does not use configuration so discriminator does not appears.
classEndpoints {
/* You won't see Tapir's discriminator without this. But json with "type" will work fine since it uses Circe's * discriminator while decoding */implicitvalanimalS:Schema[Animal] =Animal.schema
privatevalbaseEndpoint:Endpoint[Unit, Unit, Unit, Any] =
endpoint
.in("api"/"v1")
valpostAnimal:Endpoint[Animal, Unit, Animal, Any] =
baseEndpoint.post
.in("animal")
.in(jsonBody[Animal])
.out(jsonBody[Animal])
}
This way you'll see the discriminator:
Dog:
required:
- name
- training
- typetype: objectproperties:
name:
type: stringtraining:
type: booleanfriends:
type: arrayitems:
$ref: '#/components/schemas/Animal'type:
type: string
Every time you open your Swagger and see no discriminator somewhere you need to go to this type, remove SchemaDerivation and see where else you need to import Animal.schema explicitly.
The text was updated successfully, but these errors were encountered:
For recursive schemas you'll need a lazy val - I've added a note about that to the docs. Also, I've improved caching of recursive coproduct (sealed traits) schemas - see the PR.
Tapir version: 0.17.2
Scala version: 2.13.4
Describe the bug
When you have some ADT with recursive types - automatic schema derivation will fail in runtime if your schema is implicit:
How to reproduce?
Example app is here: https://github.com/DenisNovac/tapir-schema-test
Model Animal (https://github.com/DenisNovac/tapir-schema-test/blob/master/src/main/scala/Animal.scala):
When schema is not implicit - it won't be used. Tapir won't show the discriminator:
But you can still run your app since
Animal
extends theSchemaDerivation
trait. But this trait does not use configuration so discriminator does not appears.The workaround is to import the
Animal.schema
to your routes description (https://github.com/DenisNovac/tapir-schema-test/blob/master/src/main/scala/Endpoints.scala) and make it implicit there:This way you'll see the discriminator:
Every time you open your Swagger and see no discriminator somewhere you need to go to this type, remove SchemaDerivation and see where else you need to import
Animal.schema
explicitly.The text was updated successfully, but these errors were encountered: