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

Better error messages when can't map because a class has more than 22 fields #3449

Merged
merged 1 commit into from
Jan 11, 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
17 changes: 12 additions & 5 deletions core/src/main/scala-2/sttp/tapir/internal/MapToMacro.scala
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,21 @@ private[tapir] object MapToMacro {
): Unit = {
val tupleSymbol = tupleType.typeSymbol
if (!tupleSymbol.fullName.startsWith("scala.Tuple") && caseClassUtil.fields.nonEmpty) {
c.abort(c.enclosingPosition, s"Expected source type to be a tuple, but got: $tupleType")
c.abort(c.enclosingPosition, s"Expected source type to be a tuple, but got: ${tupleType.dealias}")
}

if (caseClassUtil.fields.size != tupleTypeArgs.size) {
c.abort(
c.enclosingPosition,
s"The arity of the source type doesn't match the arity of the target type: $tupleType, ${caseClassUtil.t}"
)
if (caseClassUtil.fields.size > 22) {
c.abort(
c.enclosingPosition,
s"Cannot map to ${caseClassUtil.t}: arities of up to 22 are supported. If you need more inputs/outputs, map them to classes with less fields, and then combine these classes."
)
} else {
c.abort(
c.enclosingPosition,
s"The arity of the source type (${tupleTypeArgs.size}) doesn't match the arity of the target type (${caseClassUtil.fields.size}): ${tupleType.dealias}, ${caseClassUtil.t}"
)
}
}

caseClassUtil.fields.zip(tupleTypeArgs).foreach { case (caseClassField, tupleArg) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ private[tapir] object ComplietimeErrors {
private def reportIncorrectMappingImpl[SOURCE: Type, TARGET: Type](using Quotes): Expr[Unit] = {
import quotes.reflect.*

report.throwError(s"Failed to map ${Type.show[SOURCE]} into ${Type.show[TARGET]}")
if TypeRepr.of[TARGET].typeSymbol.declaredFields.size > 22 then
report.throwError(
s"Cannot map to ${Type.show[TARGET]}: arities of up to 22 are supported. If you need more inputs/outputs, map them to classes with less fields, and then combine these classes."
)
else report.throwError(s"Failed to map ${Type.show[SOURCE]} into ${Type.show[TARGET]}")
}

}
Loading