Skip to content

Commit

Permalink
Improve name generation for Schemas of Eithers with parameterized typ…
Browse files Browse the repository at this point in the history
…es (#3848)
  • Loading branch information
kciesielski authored Jun 17, 2024
1 parent f93ed86 commit 16ae14c
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 1 deletion.
5 changes: 4 additions & 1 deletion core/src/main/scala/sttp/tapir/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,10 @@ object Schema extends LowPrioritySchema with SchemaCompanionMacros {
for {
na <- sa.name
nb <- sb.name
} yield Schema.SName("Either", List(na.show, nb.show))
} yield Schema.SName(
"Either",
(na.fullName :: na.typeParameterShortNames) ++ (nb.fullName :: nb.typeParameterShortNames)
)
)
}

Expand Down
25 changes: 25 additions & 0 deletions core/src/test/scala/sttp/tapir/SchemaTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 @@ -203,4 +204,28 @@ class SchemaTest extends AnyFlatSpec with Matchers {
implicitly[Schema[Option[Double]]].format shouldBe Some("double")
}

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 {

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(
SName("Either", List("sttp.tapir.SchemaTest.SomeValueInt", "sttp.tapir.SchemaTest.SomeValueInt"))
)
implicitly[Schema[Either[SomeValueInt, Node[SomeValueString[Boolean]]]]].name shouldBe Some(
SName("Either", List("sttp.tapir.SchemaTest.SomeValueInt", "sttp.tapir.SchemaTest.Node", "sttp.tapir.SchemaTest.SomeValueString", "scala.Boolean"))
)
implicitly[Schema[Either[SomeValueInt, Node[String]]]].name shouldBe Some(
SName("Either", List("sttp.tapir.SchemaTest.SomeValueInt", "sttp.tapir.SchemaTest.Node", "java.lang.String"))
)
implicitly[Schema[Either[Node[Boolean], SomeValueInt]]].name shouldBe Some(
SName("Either", List("sttp.tapir.SchemaTest.Node", "scala.Boolean", "sttp.tapir.SchemaTest.SomeValueInt"))
)

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -118,4 +118,26 @@ class JsonSchemasTest extends AnyFlatSpec with Matchers with OptionValues with E
// then
result.asJson.deepDropNullValues shouldBe json"""{"$$schema":"http://json-schema.org/draft-04/schema#","title":"Parent","required":["innerChildField"],"type":"object","properties":{"innerChildField":{"$$ref":"#/$$defs/Child"}},"$$defs":{"Child":{"title":"MyChild","type":"object","properties":{"childName":{"type":["string","null"]}}}}}"""
}

it should "Generate correct names for Eithers with parameterized types" in {
case class SomeValueString[A](value: String, v2: A)
final case class SomeValueInt(value: Int)

final case class Node[A](values: List[A])

TapirSchemaToJsonSchema(implicitly[Schema[Either[Int, Int]]], true).title shouldBe None
TapirSchemaToJsonSchema(implicitly[Schema[Either[SomeValueInt, Int]]], true).title shouldBe None
TapirSchemaToJsonSchema(implicitly[Schema[Either[SomeValueInt, SomeValueInt]]], true).title shouldBe Some(
"Either_SomeValueInt_SomeValueInt"
)
TapirSchemaToJsonSchema(implicitly[Schema[Either[SomeValueInt, Node[SomeValueString[Boolean]]]]], true).title shouldBe Some(
"Either_SomeValueInt_Node_SomeValueString_Boolean"
)
TapirSchemaToJsonSchema(implicitly[Schema[Either[SomeValueInt, Node[String]]]], true).title shouldBe Some(
"Either_SomeValueInt_Node_String"
)
TapirSchemaToJsonSchema(implicitly[Schema[Either[Node[Boolean], SomeValueInt]]], true).title shouldBe Some(
"Either_Node_Boolean_SomeValueInt"
)
}
}

0 comments on commit 16ae14c

Please sign in to comment.