diff --git a/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/BasicGenerator.scala b/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/BasicGenerator.scala index 8543f60c11..2cd122034c 100644 --- a/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/BasicGenerator.scala +++ b/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/BasicGenerator.scala @@ -63,24 +63,22 @@ object BasicGenerator { headTag -> taggedObj } - val specificationExtensions = doc.paths + val maybeSpecificationExtensionKeys = doc.paths .flatMap { p => p.specificationExtensions.toSeq ++ p.methods.flatMap(_.specificationExtensions.toSeq) } .groupBy(_._1) - val specificationExtensionWrapper = if (specificationExtensions.isEmpty) "" else "case class XSpecificationExtension[T](value: T)\n" - val maybeSpecificationExtensionKeys = specificationExtensions .map { case (keyName, pairs) => val values = pairs.map(_._2) val `type` = SpecificationExtensionRenderer.renderCombinedType(values) val name = strippedToCamelCase(keyName) val uncapitalisedName = name.head.toLower + name.tail val capitalisedName = name.head.toUpper + name.tail - s"""type ${capitalisedName}X = ${`type`} - |val ${uncapitalisedName}XKey = new sttp.tapir.AttributeKey[XSpecificationExtension[${capitalisedName}X]]("$packagePath.$objName.XSpecificationExtension[$packagePath.$objName.${capitalisedName}X]") + s"""type ${capitalisedName}Extension = ${`type`} + |val ${uncapitalisedName}ExtensionKey = new sttp.tapir.AttributeKey[${capitalisedName}Extension]("$packagePath.$objName.${capitalisedName}Extension") |""".stripMargin } - .mkString(specificationExtensionWrapper, "\n", "") + .mkString("\n") val mainObj = s"""| |package $packagePath diff --git a/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala b/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala index a521eec65e..3a7bda28dc 100644 --- a/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala +++ b/openapi-codegen/core/src/main/scala/sttp/tapir/codegen/EndpointGenerator.scala @@ -209,9 +209,7 @@ class EndpointGenerator { .map { case (k, v) => val camelCaseK = strippedToCamelCase(k) val uncapitalisedName = camelCaseK.head.toLower + camelCaseK.tail - val extensionType = s"${camelCaseK.capitalize}X" - val wrappedValue = s"XSpecificationExtension[$extensionType](${SpecificationExtensionRenderer.renderValue(v)})" - s""".attribute[XSpecificationExtension[$extensionType]](${uncapitalisedName}XKey, $wrappedValue)""" + s""".attribute[${camelCaseK.capitalize}Extension](${uncapitalisedName}ExtensionKey, ${SpecificationExtensionRenderer.renderValue(v)})""" } .mkString("\n") } diff --git a/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala b/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala index 6aad6688b7..283620ffe5 100644 --- a/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala +++ b/openapi-codegen/core/src/test/scala/sttp/tapir/codegen/EndpointGeneratorSpec.scala @@ -262,25 +262,25 @@ class EndpointGeneratorSpec extends CompileCheckTestBase { )("TapirGeneratedEndpoints") generatedCode shouldCompile () val expectedAttrDecls = Seq( - """.attribute[XSpecificationExtension[CustomStringExtensionOnPathX]](customStringExtensionOnPathXKey, XSpecificationExtension[CustomStringExtensionOnPathX]("another string"))""", - """.attribute[XSpecificationExtension[CustomStringExtensionOnOperationX]](customStringExtensionOnOperationXKey, XSpecificationExtension[CustomStringExtensionOnOperationX]("bazquux"))""", - """.attribute[XSpecificationExtension[CustomListExtensionOnOperationX]](customListExtensionOnOperationXKey, XSpecificationExtension[CustomListExtensionOnOperationX](Vector("baz", "quux")))""", - """.attribute[XSpecificationExtension[CustomMapExtensionOnPathX]](customMapExtensionOnPathXKey, XSpecificationExtension[CustomMapExtensionOnPathX](Map("bazkey" -> "bazval", "quuxkey" -> Vector("quux1", "quux2")))""", - """.attribute[XSpecificationExtension[CustomStringExtensionOnPathDoubleTypeX]](customStringExtensionOnPathDoubleTypeXKey, XSpecificationExtension[CustomStringExtensionOnPathDoubleTypeX](123L))""" + """.attribute[CustomStringExtensionOnPathExtension](customStringExtensionOnPathExtensionKey, "another string")""", + """.attribute[CustomStringExtensionOnOperationExtension](customStringExtensionOnOperationExtensionKey, "bazquux")""", + """.attribute[CustomListExtensionOnOperationExtension](customListExtensionOnOperationExtensionKey, Vector("baz", "quux"))""", + """.attribute[CustomMapExtensionOnPathExtension](customMapExtensionOnPathExtensionKey, Map("bazkey" -> "bazval", "quuxkey" -> Vector("quux1", "quux2"))""", + """.attribute[CustomStringExtensionOnPathDoubleTypeExtension](customStringExtensionOnPathDoubleTypeExtensionKey, 123L)""" ) expectedAttrDecls foreach (decl => generatedCode should include(decl)) generatedCode should include( - """val customMapExtensionOnOperationXKey = new sttp.tapir.AttributeKey[XSpecificationExtension[CustomMapExtensionOnOperationX]]("sttp.tapir.generated.TapirGeneratedEndpoints.XSpecificationExtension[sttp.tapir.generated.TapirGeneratedEndpoints.CustomMapExtensionOnOperationX]")""".stripMargin + """val customMapExtensionOnOperationExtensionKey = new sttp.tapir.AttributeKey[CustomMapExtensionOnOperationExtension]("sttp.tapir.generated.TapirGeneratedEndpoints.CustomMapExtensionOnOperationExtension")""".stripMargin ) val expectedKeyDeclarations = Seq( - """type CustomMapExtensionOnOperationX = Map[String, Any]""", - """type CustomListExtensionOnPathAnyTypeX = Seq[Any]""", - """type CustomMapExtensionOnPathSingleValueTypeX = Map[String, String]""", - """type CustomListExtensionOnOperationX = Seq[String]""", - """type CustomStringExtensionOnPathAnyTypeX = Any""", - """type CustomStringExtensionOnPathDoubleTypeX = Double""", - """type CustomListExtensionOnPathX = Seq[String]""", - """type CustomStringExtensionOnPathX = String""" + """type CustomMapExtensionOnOperationExtension = Map[String, Any]""", + """type CustomListExtensionOnPathAnyTypeExtension = Seq[Any]""", + """type CustomMapExtensionOnPathSingleValueTypeExtension = Map[String, String]""", + """type CustomListExtensionOnOperationExtension = Seq[String]""", + """type CustomStringExtensionOnPathAnyTypeExtension = Any""", + """type CustomStringExtensionOnPathDoubleTypeExtension = Double""", + """type CustomListExtensionOnPathExtension = Seq[String]""", + """type CustomStringExtensionOnPathExtension = String""" ) expectedKeyDeclarations foreach (decl => generatedCode should include(decl)) }