Skip to content

Commit

Permalink
Move isFile out into ProtocolGenerators
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Oct 10, 2023
1 parent 9108d60 commit 0eef007
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 19 deletions.
12 changes: 2 additions & 10 deletions modules/core/src/main/scala/dev/guardrail/SwaggerUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,12 @@ import io.swagger.v3.oas.models.security.{ SecurityScheme => SwSecurityScheme }
import cats.syntax.all._
import dev.guardrail.core.Tracker
import dev.guardrail.core.implicits._
import dev.guardrail.terms.{ CollectionsLibTerms, LanguageTerms, SecurityScheme, SwaggerTerms }
import dev.guardrail.core.extract.{ CustomArrayTypeName, CustomMapTypeName, CustomTypeName, VendorExtension }
import dev.guardrail.terms.{ LanguageTerms, SecurityScheme, SwaggerTerms }
import dev.guardrail.core.extract.CustomTypeName
import dev.guardrail.core.extract.VendorExtension.VendorExtensible._
import dev.guardrail.languages.LA

object SwaggerUtil {
def isFile(typeName: String, format: Option[String]): Boolean =
(typeName, format) match {
case ("string", Some("binary")) => true
case ("file", _) => true
case ("binary", _) => true
case _ => false
}

def extractSecuritySchemes[L <: LA, F[_]](
spec: OpenAPI,
prefixes: List[String]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import dev.guardrail.terms.{
StringHeldEnum,
SwaggerTerms
}
import dev.guardrail.{ SwaggerUtil, Target, UserError }
import dev.guardrail.{ Target, UserError }

class CirceProtocolGeneratorLoader extends ProtocolGeneratorLoader {
type L = ScalaLanguage
Expand Down Expand Up @@ -192,6 +192,14 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
} yield ProtocolDefinitions[ScalaLanguage](strictElems ++ polyADTElems, protoImports, pkgImports, pkgObjectContents, implicitsObject))
}

private[this] def isFile(typeName: String, format: Option[String]): Boolean =
(typeName, format) match {
case ("string", Some("binary")) => true
case ("file", _) => true
case ("binary", _) => true
case _ => false
}

private[this] def getRequiredFieldsRec(root: Tracker[Schema[_]]): List[String] = {
@scala.annotation.tailrec
def work(values: List[Tracker[Schema[_]]], acc: List[String]): List[String] = {
Expand Down Expand Up @@ -993,8 +1001,7 @@ class CirceProtocolGenerator private (circeVersion: CirceModelGenerator, applyVa
dataRedaction = DataRedaction(property).getOrElse(DataVisible)

(tpe, classDep, rawType) <- meta match {
case core.Resolved(declType, classDep, _, rawType @ LiteralRawType(Some(rawTypeStr), rawFormat))
if SwaggerUtil.isFile(rawTypeStr, rawFormat) && !isCustomType =>
case core.Resolved(declType, classDep, _, rawType @ LiteralRawType(Some(rawTypeStr), rawFormat)) if isFile(rawTypeStr, rawFormat) && !isCustomType =>
// assume that binary data are represented as a string. allow users to override.
Target.pure((t"String", classDep, rawType))
case core.Resolved(declType, classDep, _, rawType) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import dev.guardrail.terms.{
StringHeldEnum,
SwaggerTerms
}
import dev.guardrail.{ SwaggerUtil, Target, UserError }
import dev.guardrail.{ Target, UserError }

class CirceRefinedProtocolGeneratorLoader extends ProtocolGeneratorLoader {
type L = ScalaLanguage
Expand Down Expand Up @@ -261,6 +261,14 @@ class CirceRefinedProtocolGenerator private (circeVersion: CirceModelGenerator,
} yield ProtocolDefinitions[ScalaLanguage](strictElems ++ polyADTElems, protoImports, pkgImports, pkgObjectContents, implicitsObject))
}

private[this] def isFile(typeName: String, format: Option[String]): Boolean =
(typeName, format) match {
case ("string", Some("binary")) => true
case ("file", _) => true
case ("binary", _) => true
case _ => false
}

private[this] def getRequiredFieldsRec(root: Tracker[Schema[_]]): List[String] = {
@scala.annotation.tailrec
def work(values: List[Tracker[Schema[_]]], acc: List[String]): List[String] = {
Expand Down Expand Up @@ -1062,8 +1070,7 @@ class CirceRefinedProtocolGenerator private (circeVersion: CirceModelGenerator,
dataRedaction = DataRedaction(property).getOrElse(DataVisible)

(tpe, classDep, rawType) <- meta match {
case core.Resolved(declType, classDep, _, rawType @ LiteralRawType(Some(rawTypeStr), rawFormat))
if SwaggerUtil.isFile(rawTypeStr, rawFormat) && !isCustomType =>
case core.Resolved(declType, classDep, _, rawType @ LiteralRawType(Some(rawTypeStr), rawFormat)) if isFile(rawTypeStr, rawFormat) && !isCustomType =>
// assume that binary data are represented as a string. allow users to override.
Target.pure((t"String", classDep, rawType))
case core.Resolved(declType, classDep, _, rawType) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import dev.guardrail.terms.{
StringHeldEnum,
SwaggerTerms
}
import dev.guardrail.{ RuntimeFailure, SwaggerUtil, Target, UserError }
import dev.guardrail.{ RuntimeFailure, Target, UserError }

class JacksonProtocolGeneratorLoader extends ProtocolGeneratorLoader {
type L = ScalaLanguage
Expand Down Expand Up @@ -282,6 +282,14 @@ class JacksonProtocolGenerator private extends ProtocolTerms[ScalaLanguage, Targ
} yield ProtocolDefinitions[ScalaLanguage](strictElems ++ polyADTElems, protoImports, pkgImports, pkgObjectContents, implicitsObject))
}

private[this] def isFile(typeName: String, format: Option[String]): Boolean =
(typeName, format) match {
case ("string", Some("binary")) => true
case ("file", _) => true
case ("binary", _) => true
case _ => false
}

private[this] def getRequiredFieldsRec(root: Tracker[Schema[_]]): List[String] = {
@scala.annotation.tailrec
def work(values: List[Tracker[Schema[_]]], acc: List[String]): List[String] = {
Expand Down Expand Up @@ -1118,8 +1126,7 @@ class JacksonProtocolGenerator private extends ProtocolTerms[ScalaLanguage, Targ
dataRedaction = DataRedaction(property).getOrElse(DataVisible)

(tpe, classDep, rawType) <- meta match { // TODO: Target is not used
case core.Resolved(declType, classDep, _, rawType @ LiteralRawType(Some(rawTypeStr), rawFormat))
if SwaggerUtil.isFile(rawTypeStr, rawFormat) && !isCustomType =>
case core.Resolved(declType, classDep, _, rawType @ LiteralRawType(Some(rawTypeStr), rawFormat)) if isFile(rawTypeStr, rawFormat) && !isCustomType =>
// assume that binary data are represented as a string. allow users to override.
Target.pure((t"String", classDep, rawType))
case core.Resolved(declType, classDep, _, rawType) =>
Expand Down

0 comments on commit 0eef007

Please sign in to comment.