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

Fix schema name resolution for Map (Scala 2) #3865

Merged
merged 1 commit into from
Jun 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ private[tapir] object SchemaMapMacro {

val genericTypeParameters =
(if (keyTypeParameter.split('.').lastOption.contains("String")) Nil else List(keyTypeParameter)) ++
extractTypeArguments(weakTypeK) ++ List(weakTypeV.typeSymbol.fullName) ++ extractTypeArguments(weakTypeV)
extractTypeArguments(weakTypeK.dealias) ++ List(weakTypeV.typeSymbol.fullName) ++ extractTypeArguments(weakTypeV.dealias)
val schemaForMap =
q"""{
val s = $schemaForV
Expand Down
26 changes: 22 additions & 4 deletions core/src/test/scala/sttp/tapir/SchemaTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers
import sttp.tapir.Schema.SName
import sttp.tapir.TestUtil.field
import javax.swing.plaf.ListUI

class SchemaTest extends AnyFlatSpec with Matchers {
it should "modify basic schema" in {
Expand Down Expand Up @@ -207,10 +206,9 @@ class SchemaTest extends AnyFlatSpec with Matchers {
case class SomeValueString[A](value: String, v2: A)
final case class SomeValueInt(value: Int)
final case class Node[A](values: List[A])
it should "generate correct names for Eithers with parameterized types" in {

it should "generate correct names for Eithers with parameterized types" in {
import sttp.tapir.generic.auto._

implicitly[Schema[Either[Int, Int]]].name shouldBe None
implicitly[Schema[Either[SomeValueInt, Int]]].name shouldBe None
implicitly[Schema[Either[SomeValueInt, SomeValueInt]]].name shouldBe Some(
Expand All @@ -225,7 +223,27 @@ class SchemaTest extends AnyFlatSpec with Matchers {
implicitly[Schema[Either[Node[Boolean], SomeValueInt]]].name shouldBe Some(
SName("Either", List("sttp.tapir.SchemaTest.Node", "scala.Boolean", "sttp.tapir.SchemaTest.SomeValueInt"))
)

}

it should "generate correct names for Maps with parameterized types" in {
import sttp.tapir.generic.auto._
type Tree[A] = Either[A, Node[A]]
val schema1: Schema[Map[SomeValueInt, Node[SomeValueString[Boolean]]]] = Schema.schemaForMap(_.toString)
schema1.name shouldBe Some(
SName("Map", List("sttp.tapir.SchemaTest.SomeValueInt", "sttp.tapir.SchemaTest.Node", "sttp.tapir.SchemaTest.SomeValueString", "scala.Boolean"))
)
val schema2: Schema[Map[Node[Boolean], Node[String]]] = Schema.schemaForMap(_.toString)
schema2.name shouldBe Some(
SName("Map", List("sttp.tapir.SchemaTest.Node", "scala.Boolean", "sttp.tapir.SchemaTest.Node", "java.lang.String"))
)
val schema3: Schema[Map[Int, Tree[String]]] = Schema.schemaForMap(_.toString)
schema3.name shouldBe Some(
SName("Map", List("scala.Int", "scala.util.Either", "java.lang.String", "sttp.tapir.SchemaTest.Node", "java.lang.String"))
)
val schema4: Schema[Map[Tree[String], Int]] = Schema.schemaForMap(_.toString)
schema4.name shouldBe Some(
SName("Map", List("scala.util.Either", "java.lang.String", "sttp.tapir.SchemaTest.Node", "java.lang.String", "scala.Int"))
)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,13 @@ class JsonSchemasTest extends AnyFlatSpec with Matchers with OptionValues with E
"Either_Node_Boolean_SomeValueInt"
)
}

it should "Generate correct names for Maps with parameterized types" in {
type Tree[A] = Either[A, Node[A]]
final case class Node[A](values: List[A])
val schema1: Schema[Map[Int, Tree[String]]] = Schema.schemaForMap(_.toString)
TapirSchemaToJsonSchema(schema1, true).title shouldBe Some("Map_Int_Either_String_Node_String")
val schema2: Schema[Map[Tree[Int], String]] = Schema.schemaForMap(_.toString)
TapirSchemaToJsonSchema(schema2, true).title shouldBe Some("Map_Either_Int_Node_Int_String")
}
}
Loading