From e16a5fe34fe0c86cf9ae0b23086ec15e4a792079 Mon Sep 17 00:00:00 2001 From: Michael Bryzek Date: Fri, 21 Jun 2024 10:17:44 -0400 Subject: [PATCH] Fix mock generator for primitives inside a union --- .../mock/MockFactoriesGenerator.scala | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/scala-generator/src/main/scala/models/generator/mock/MockFactoriesGenerator.scala b/scala-generator/src/main/scala/models/generator/mock/MockFactoriesGenerator.scala index 74286dc5..68f3b60c 100644 --- a/scala-generator/src/main/scala/models/generator/mock/MockFactoriesGenerator.scala +++ b/scala-generator/src/main/scala/models/generator/mock/MockFactoriesGenerator.scala @@ -2,6 +2,7 @@ package scala.generator.mock import generator.ServiceFileNames import io.apibuilder.generator.v0.models.{File, InvocationForm} +import lib.Text import lib.Text._ import lib.generator.CodeGenerator @@ -53,10 +54,21 @@ object MockFactoriesGenerator extends CodeGenerator { } private[mock] def makeUnion(union: ScalaUnion): String = { - val typ = union.types.headOption.getOrElse { - sys.error(s"Union type[${union.qualifiedName}] does not have any times") + val impl = union.types.headOption match { + case Some(typ) => { + val value = mockValue(typ.datatype) + typ.datatype match { + case p: ScalaPrimitive => { + // Union type wrapping a primitive + val className = Text.pascalCase(union.name) + Text.pascalCase(p.shortName) + union.ssd.namespaces.unions + "." + className + s"($value)" + } + case _ => value + } + } + case None => "???" } - s"def make${union.name}(): ${union.qualifiedName} = ${mockValue(typ.datatype)}" + s"def make${union.name}(): ${union.qualifiedName} = $impl" } private[mock] def mockValue(