From e3813baa7ca2f8bf833188a8b10f3cc8ce83b242 Mon Sep 17 00:00:00 2001 From: Devon Stewart Date: Sat, 30 Dec 2023 22:56:03 -0800 Subject: [PATCH] Including Tracker info in lookupStatusCode --- .../src/main/scala/dev/guardrail/terms/Responses.scala | 2 +- .../dev/guardrail/terms/framework/FrameworkTerms.scala | 7 ++++--- .../generators/java/dropwizard/DropwizardGenerator.scala | 8 ++++---- .../generators/java/springMvc/SpringMvcGenerator.scala | 7 ++++--- .../generators/scala/akkaHttp/AkkaHttpGenerator.scala | 7 ++++--- .../generators/scala/dropwizard/DropwizardGenerator.scala | 7 ++++--- .../generators/scala/http4s/Http4sGenerator.scala | 7 ++++--- .../src/test/scala/tests/circe/ArrayValidationTest.scala | 6 +++--- .../src/test/scala/tests/circe/BigObjectSpec.scala | 6 +++--- .../src/test/scala/tests/circe/ValidationTest.scala | 4 ++-- 10 files changed, 33 insertions(+), 28 deletions(-) diff --git a/modules/core/src/main/scala/dev/guardrail/terms/Responses.scala b/modules/core/src/main/scala/dev/guardrail/terms/Responses.scala index 61030fd414..c9e9059e5d 100644 --- a/modules/core/src/main/scala/dev/guardrail/terms/Responses.scala +++ b/modules/core/src/main/scala/dev/guardrail/terms/Responses.scala @@ -47,7 +47,7 @@ object Responses { instances <- responses .traverse { case (key, resp) => for { - httpCode <- lookupStatusCode(key) + httpCode <- lookupStatusCode(Tracker.cloneHistory(resp, key)) (statusCode, statusCodeName) = httpCode valueTypes <- (for { (rawContentType, content) <- resp.downField("content", _.getContent()).indexedDistribute.value diff --git a/modules/core/src/main/scala/dev/guardrail/terms/framework/FrameworkTerms.scala b/modules/core/src/main/scala/dev/guardrail/terms/framework/FrameworkTerms.scala index 04ea315279..d1efb87751 100644 --- a/modules/core/src/main/scala/dev/guardrail/terms/framework/FrameworkTerms.scala +++ b/modules/core/src/main/scala/dev/guardrail/terms/framework/FrameworkTerms.scala @@ -1,12 +1,13 @@ package dev.guardrail.terms.framework import dev.guardrail.languages.LA +import dev.guardrail.core.Tracker abstract class FrameworkTerms[L <: LA, F[_]] { self => def getFrameworkImports(tracing: Boolean): F[List[L#Import]] def getFrameworkImplicits(): F[Option[(L#TermName, L#ObjectDefinition)]] def getFrameworkDefinitions(tracing: Boolean): F[List[(L#TermName, List[L#Definition])]] - def lookupStatusCode(key: String): F[(Int, L#TermName)] + def lookupStatusCode(key: Tracker[String]): F[(Int, L#TermName)] def fileType(format: Option[String]): F[L#Type] def objectType(format: Option[String]): F[L#Type] @@ -14,7 +15,7 @@ abstract class FrameworkTerms[L <: LA, F[_]] { self => getFrameworkImports: Boolean => F[List[L#Import]] = self.getFrameworkImports _, getFrameworkImplicits: () => F[Option[(L#TermName, L#ObjectDefinition)]] = self.getFrameworkImplicits _, getFrameworkDefinitions: Boolean => F[List[(L#TermName, List[L#Definition])]] = self.getFrameworkDefinitions _, - lookupStatusCode: String => F[(Int, L#TermName)] = self.lookupStatusCode _, + lookupStatusCode: Tracker[String] => F[(Int, L#TermName)] = self.lookupStatusCode _, fileType: Option[String] => F[L#Type] = self.fileType _, objectType: Option[String] => F[L#Type] = self.objectType _ ) = { @@ -29,7 +30,7 @@ abstract class FrameworkTerms[L <: LA, F[_]] { self => def getFrameworkImports(tracing: Boolean) = newGetFrameworkImports(tracing) def getFrameworkImplicits() = newGetFrameworkImplicits() def getFrameworkDefinitions(tracing: Boolean) = newGetFrameworkDefinitions(tracing) - def lookupStatusCode(key: String) = newLookupStatusCode(key) + def lookupStatusCode(key: Tracker[String]) = newLookupStatusCode(key) def fileType(format: Option[String]) = newFileType(format) def objectType(format: Option[String]) = newObjectType(format) } diff --git a/modules/java-dropwizard/src/main/scala/dev/guardrail/generators/java/dropwizard/DropwizardGenerator.scala b/modules/java-dropwizard/src/main/scala/dev/guardrail/generators/java/dropwizard/DropwizardGenerator.scala index 563ce684c2..ae958d4360 100644 --- a/modules/java-dropwizard/src/main/scala/dev/guardrail/generators/java/dropwizard/DropwizardGenerator.scala +++ b/modules/java-dropwizard/src/main/scala/dev/guardrail/generators/java/dropwizard/DropwizardGenerator.scala @@ -7,7 +7,7 @@ import com.github.javaparser.ast.expr._ import scala.reflect.runtime.universe.typeTag import dev.guardrail.Target -import dev.guardrail.core.SupportDefinition +import dev.guardrail.core.{ SupportDefinition, Tracker } import dev.guardrail.generators.java.JavaCollectionsGenerator import dev.guardrail.generators.java.JavaLanguage import dev.guardrail.generators.java.JavaVavrCollectionsGenerator @@ -58,10 +58,10 @@ class DropwizardGenerator private extends FrameworkTerms[JavaLanguage, Target] { ) ) - def lookupStatusCode(key: String) = { + def lookupStatusCode(key: Tracker[String]) = { def parseStatusCode(code: Int, termName: String): Target[(Int, Name)] = safeParseName(termName).map(name => (code, name)) - key match { + key.unwrapTracker match { case "100" => parseStatusCode(100, "Continue") case "101" => parseStatusCode(101, "SwitchingProtocols") case "102" => parseStatusCode(102, "Processing") @@ -128,7 +128,7 @@ class DropwizardGenerator private extends FrameworkTerms[JavaLanguage, Target] { case "511" => parseStatusCode(511, "NetworkAuthenticationRequired") case "598" => parseStatusCode(598, "NetworkReadTimeout") case "599" => parseStatusCode(599, "NetworkConnectTimeout") - case _ => Target.raiseUserError(s"Unknown HTTP status code: ${key}") + case code => Target.raiseUserError(s"Unknown HTTP status code: ${code} (${key.showHistory})") } } } diff --git a/modules/java-spring-mvc/src/main/scala/dev/guardrail/generators/java/springMvc/SpringMvcGenerator.scala b/modules/java-spring-mvc/src/main/scala/dev/guardrail/generators/java/springMvc/SpringMvcGenerator.scala index 2be9632d40..7e2e597c06 100644 --- a/modules/java-spring-mvc/src/main/scala/dev/guardrail/generators/java/springMvc/SpringMvcGenerator.scala +++ b/modules/java-spring-mvc/src/main/scala/dev/guardrail/generators/java/springMvc/SpringMvcGenerator.scala @@ -4,6 +4,7 @@ import com.github.javaparser.ast.expr.Name import scala.reflect.runtime.universe.typeTag import dev.guardrail.Target +import dev.guardrail.core.Tracker import dev.guardrail.generators.java.JavaCollectionsGenerator import dev.guardrail.generators.java.JavaLanguage import dev.guardrail.generators.java.JavaVavrCollectionsGenerator @@ -40,11 +41,11 @@ class SpringMvcGenerator private (implicit Cl: CollectionsLibTerms[JavaLanguage, def getFrameworkDefinitions(tracing: Boolean) = Target.pure(List.empty) - def lookupStatusCode(key: String) = { + def lookupStatusCode(key: Tracker[String]) = { def parseStatusCode(code: Int, termName: String): Target[(Int, Name)] = safeParseName(termName).map(name => (code, name)) - key match { + key.unwrapTracker match { case "100" => parseStatusCode(100, "Continue") case "101" => parseStatusCode(101, "SwitchingProtocols") case "102" => parseStatusCode(102, "Processing") @@ -111,7 +112,7 @@ class SpringMvcGenerator private (implicit Cl: CollectionsLibTerms[JavaLanguage, case "511" => parseStatusCode(511, "NetworkAuthenticationRequired") case "598" => parseStatusCode(598, "NetworkReadTimeout") case "599" => parseStatusCode(599, "NetworkConnectTimeout") - case _ => Target.raiseUserError(s"Unknown HTTP status code: ${key}") + case code => Target.raiseUserError(s"Unknown HTTP status code: ${code} (${key.showHistory})") } } } diff --git a/modules/scala-akka-http/src/main/scala/dev/guardrail/generators/scala/akkaHttp/AkkaHttpGenerator.scala b/modules/scala-akka-http/src/main/scala/dev/guardrail/generators/scala/akkaHttp/AkkaHttpGenerator.scala index 5005dc2e59..98d9b2adf4 100644 --- a/modules/scala-akka-http/src/main/scala/dev/guardrail/generators/scala/akkaHttp/AkkaHttpGenerator.scala +++ b/modules/scala-akka-http/src/main/scala/dev/guardrail/generators/scala/akkaHttp/AkkaHttpGenerator.scala @@ -4,6 +4,7 @@ import scala.meta._ import scala.reflect.runtime.universe.typeTag import dev.guardrail.{ RuntimeFailure, Target } +import dev.guardrail.core.Tracker import dev.guardrail.generators.scala.{ CirceModelGenerator, CirceRefinedModelGenerator, JacksonModelGenerator, ModelGeneratorType, ScalaLanguage } import dev.guardrail.generators.spi.{ FrameworkGeneratorLoader, ModuleLoadResult, ProtocolGeneratorLoader } import dev.guardrail.terms.framework._ @@ -383,8 +384,8 @@ class AkkaHttpGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGenerato override def getFrameworkDefinitions(tracing: Boolean) = Target.pure(List.empty) - override def lookupStatusCode(key: String) = - key match { + override def lookupStatusCode(key: Tracker[String]) = + key.unwrapTracker match { case "100" => Target.pure((100, q"Continue")) case "101" => Target.pure((101, q"SwitchingProtocols")) case "102" => Target.pure((102, q"Processing")) @@ -464,6 +465,6 @@ class AkkaHttpGenerator private (akkaHttpVersion: AkkaHttpVersion, modelGenerato case "511" => Target.pure((511, q"NetworkAuthenticationRequired")) case "598" => Target.pure((598, q"NetworkReadTimeout")) case "599" => Target.pure((599, q"NetworkConnectTimeout")) - case _ => Target.raiseUserError(s"Unknown HTTP status code: ${key}") + case code => Target.raiseUserError(s"Unknown HTTP status code: ${code} (${key.showHistory})") } } diff --git a/modules/scala-dropwizard/src/main/scala/dev/guardrail/generators/scala/dropwizard/DropwizardGenerator.scala b/modules/scala-dropwizard/src/main/scala/dev/guardrail/generators/scala/dropwizard/DropwizardGenerator.scala index ea89a08b7b..eb21269910 100644 --- a/modules/scala-dropwizard/src/main/scala/dev/guardrail/generators/scala/dropwizard/DropwizardGenerator.scala +++ b/modules/scala-dropwizard/src/main/scala/dev/guardrail/generators/scala/dropwizard/DropwizardGenerator.scala @@ -1,6 +1,7 @@ package dev.guardrail.generators.scala.dropwizard import dev.guardrail.{ Target, UserError } +import dev.guardrail.core.Tracker import dev.guardrail.generators.scala.ScalaLanguage import dev.guardrail.terms.framework.FrameworkTerms import dev.guardrail.generators.spi.{ FrameworkGeneratorLoader, ModuleLoadResult } @@ -30,8 +31,8 @@ class DropwizardGenerator private extends FrameworkTerms[ScalaLanguage, Target] // jaxrs has a Status enum, but it is missing a _lot_ of codes, // so we'll make our own here and use ints in the generated code - override def lookupStatusCode(key: String): Target[(Int, Term.Name)] = - key match { + override def lookupStatusCode(key: Tracker[String]): Target[(Int, Term.Name)] = + key.unwrapTracker match { case "100" => Target.pure((100, q"Continue")) case "101" => Target.pure((101, q"SwitchingProtocols")) case "102" => Target.pure((102, q"Processing")) @@ -108,6 +109,6 @@ class DropwizardGenerator private extends FrameworkTerms[ScalaLanguage, Target] .filter(code => code >= 100 && code <= 599) .map((_, Term.Name(s"StatusCode$custom"))) .toOption - Target.fromOption(customCode, UserError(s"'$custom' is not a valid HTTP status code")) + Target.fromOption(customCode, UserError(s"'$custom' is not a valid HTTP status code (${key.showHistory})")) } } diff --git a/modules/scala-http4s/src/main/scala/dev/guardrail/generators/scala/http4s/Http4sGenerator.scala b/modules/scala-http4s/src/main/scala/dev/guardrail/generators/scala/http4s/Http4sGenerator.scala index 4470cf6c5e..80b2cbd617 100644 --- a/modules/scala-http4s/src/main/scala/dev/guardrail/generators/scala/http4s/Http4sGenerator.scala +++ b/modules/scala-http4s/src/main/scala/dev/guardrail/generators/scala/http4s/Http4sGenerator.scala @@ -4,6 +4,7 @@ import scala.meta._ import scala.reflect.runtime.universe.typeTag import dev.guardrail.Target +import dev.guardrail.core.Tracker import dev.guardrail.generators.scala._ import dev.guardrail.generators.spi.{ FrameworkGeneratorLoader, ModuleLoadResult } import dev.guardrail.terms.framework._ @@ -89,8 +90,8 @@ class Http4sGenerator private extends FrameworkTerms[ScalaLanguage, Target] { def getFrameworkDefinitions(tracing: Boolean) = Target.pure(List.empty) - def lookupStatusCode(key: String): Target[(Int, scala.meta.Term.Name)] = - key match { + def lookupStatusCode(key: Tracker[String]): Target[(Int, scala.meta.Term.Name)] = + key.unwrapTracker match { case "100" => Target.pure((100, q"Continue")) case "101" => Target.pure((101, q"SwitchingProtocols")) case "102" => Target.pure((102, q"Processing")) @@ -149,6 +150,6 @@ class Http4sGenerator private extends FrameworkTerms[ScalaLanguage, Target] { case "508" => Target.pure((508, q"LoopDetected")) case "510" => Target.pure((510, q"NotExtended")) case "511" => Target.pure((511, q"NetworkAuthenticationRequired")) - case _ => Target.raiseUserError(s"Unknown HTTP status code: ${key}") + case code => Target.raiseUserError(s"Unknown HTTP status code: ${code} (${key.showHistory})") } } diff --git a/modules/scala-support/src/test/scala/tests/circe/ArrayValidationTest.scala b/modules/scala-support/src/test/scala/tests/circe/ArrayValidationTest.scala index 915a6f8fa5..5670b4fd88 100644 --- a/modules/scala-support/src/test/scala/tests/circe/ArrayValidationTest.scala +++ b/modules/scala-support/src/test/scala/tests/circe/ArrayValidationTest.scala @@ -56,9 +56,9 @@ class ArrayValidationTest extends AnyFreeSpec with Matchers with SwaggerSpecRunn def getFrameworkImplicits(): dev.guardrail.Target[Option[ (dev.guardrail.generators.scala.ScalaLanguage#TermName, dev.guardrail.generators.scala.ScalaLanguage#ObjectDefinition) ]] = ??? - def getFrameworkImports(tracing: Boolean): dev.guardrail.Target[List[dev.guardrail.generators.scala.ScalaLanguage#Import]] = ??? - def lookupStatusCode(key: String): dev.guardrail.Target[(Int, dev.guardrail.generators.scala.ScalaLanguage#TermName)] = ??? - def objectType(format: Option[String]): dev.guardrail.Target[dev.guardrail.generators.scala.ScalaLanguage#Type] = Target.pure(t"io.circe.Json") + def getFrameworkImports(tracing: Boolean): dev.guardrail.Target[List[dev.guardrail.generators.scala.ScalaLanguage#Import]] = ??? + def lookupStatusCode(key: Tracker[String]): dev.guardrail.Target[(Int, dev.guardrail.generators.scala.ScalaLanguage#TermName)] = ??? + def objectType(format: Option[String]): dev.guardrail.Target[dev.guardrail.generators.scala.ScalaLanguage#Type] = Target.pure(t"io.circe.Json") } implicit val circeProtocolGenerator: ProtocolTerms[ScalaLanguage, Target] = CirceRefinedProtocolGenerator(CirceRefinedModelGenerator.V012) implicit val scalaGenerator = ScalaGenerator() diff --git a/modules/scala-support/src/test/scala/tests/circe/BigObjectSpec.scala b/modules/scala-support/src/test/scala/tests/circe/BigObjectSpec.scala index 3e027bb757..e15793244f 100644 --- a/modules/scala-support/src/test/scala/tests/circe/BigObjectSpec.scala +++ b/modules/scala-support/src/test/scala/tests/circe/BigObjectSpec.scala @@ -139,9 +139,9 @@ class BigObjectSpec extends AnyFunSuite with Matchers with SwaggerSpecRunner wit def getFrameworkImplicits(): dev.guardrail.Target[Option[ (dev.guardrail.generators.scala.ScalaLanguage#TermName, dev.guardrail.generators.scala.ScalaLanguage#ObjectDefinition) ]] = ??? - def getFrameworkImports(tracing: Boolean): dev.guardrail.Target[List[dev.guardrail.generators.scala.ScalaLanguage#Import]] = ??? - def lookupStatusCode(key: String): dev.guardrail.Target[(Int, dev.guardrail.generators.scala.ScalaLanguage#TermName)] = ??? - def objectType(format: Option[String]): dev.guardrail.Target[dev.guardrail.generators.scala.ScalaLanguage#Type] = Target.pure(t"io.circe.Json") + def getFrameworkImports(tracing: Boolean): dev.guardrail.Target[List[dev.guardrail.generators.scala.ScalaLanguage#Import]] = ??? + def lookupStatusCode(key: Tracker[String]): dev.guardrail.Target[(Int, dev.guardrail.generators.scala.ScalaLanguage#TermName)] = ??? + def objectType(format: Option[String]): dev.guardrail.Target[dev.guardrail.generators.scala.ScalaLanguage#Type] = Target.pure(t"io.circe.Json") } implicit val circeProtocolGenerator = CirceProtocolGenerator(CirceModelGenerator.V012) implicit val scalaGenerator = ScalaGenerator() diff --git a/modules/scala-support/src/test/scala/tests/circe/ValidationTest.scala b/modules/scala-support/src/test/scala/tests/circe/ValidationTest.scala index c734988a92..aa1fc34807 100644 --- a/modules/scala-support/src/test/scala/tests/circe/ValidationTest.scala +++ b/modules/scala-support/src/test/scala/tests/circe/ValidationTest.scala @@ -59,8 +59,8 @@ class ValidationTest extends AnyFreeSpec with Matchers with SwaggerSpecRunner wi def getFrameworkImplicits(): dev.guardrail.Target[Option[ (dev.guardrail.generators.scala.ScalaLanguage#TermName, dev.guardrail.generators.scala.ScalaLanguage#ObjectDefinition) ]] = ??? - def getFrameworkImports(tracing: Boolean): dev.guardrail.Target[List[dev.guardrail.generators.scala.ScalaLanguage#Import]] = ??? - def lookupStatusCode(key: String): dev.guardrail.Target[(Int, dev.guardrail.generators.scala.ScalaLanguage#TermName)] = ??? + def getFrameworkImports(tracing: Boolean): dev.guardrail.Target[List[dev.guardrail.generators.scala.ScalaLanguage#Import]] = ??? + def lookupStatusCode(key: Tracker[String]): dev.guardrail.Target[(Int, dev.guardrail.generators.scala.ScalaLanguage#TermName)] = ??? def objectType(format: Option[String]): dev.guardrail.Target[dev.guardrail.generators.scala.ScalaLanguage#Type] = Target.pure(t"io.circe.Json") } implicit val circeProtocolGenerator: ProtocolTerms[ScalaLanguage, Target] = CirceRefinedProtocolGenerator(CirceRefinedModelGenerator.V012)