Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import sttp.capabilities.zio.ZioStreams
import sttp.tapir.client.sttp.WebSocketToPipe

trait TapirSttpClientZioWebSockets {
implicit val webSocketsSupportedForZioStreams: WebSocketToPipe[ZioStreams with WebSockets] =
implicit lazy val webSocketsSupportedForZioStreams: WebSocketToPipe[ZioStreams with WebSockets] =
new WebSocketToZioPipe[ZioStreams with WebSockets]
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ import sttp.capabilities.zio.ZioStreams
import sttp.tapir.client.sttp.WebSocketToPipe

trait TapirSttpClientZioWebSockets {
implicit val webSocketsSupportedForZioStreams: WebSocketToPipe[ZioStreams with WebSockets] =
implicit lazy val webSocketsSupportedForZioStreams: WebSocketToPipe[ZioStreams with WebSockets] =
new WebSocketToZioPipe[ZioStreams with WebSockets]
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import sttp.tapir.{DecodeResult, _}
import scala.concurrent.ExecutionContext

abstract class ClientTests[R] extends AsyncFunSuite with Matchers with BeforeAndAfterAll {
implicit val ioRT: IORuntime = ClientTestsPlatform.ioRT
implicit lazy val ioRT: IORuntime = ClientTestsPlatform.ioRT
implicit override val executionContext: ExecutionContext = ClientTestsPlatform.executionContext

type Port = Int
Expand Down
68 changes: 34 additions & 34 deletions core/src/main/scala/sttp/tapir/Codec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -191,21 +191,21 @@ object Codec extends CodecExtensions with CodecExtensions2 with FormCodecMacros
}
def idPlain[L](s: Schema[L] = Schema[L](SchemaType.SString())): Codec[L, L, CodecFormat.TextPlain] = id(CodecFormat.TextPlain(), s)

implicit val string: Codec[String, String, TextPlain] = id[String, TextPlain](TextPlain(), Schema.schemaForString)

implicit val byte: Codec[String, Byte, TextPlain] = parsedString[Byte](_.toByte).schema(Schema.schemaForByte)
implicit val short: Codec[String, Short, TextPlain] = parsedString[Short](_.toShort).schema(Schema.schemaForShort)
implicit val int: Codec[String, Int, TextPlain] = parsedString[Int](_.toInt).schema(Schema.schemaForInt)
implicit val long: Codec[String, Long, TextPlain] = parsedString[Long](_.toLong).schema(Schema.schemaForLong)
implicit val float: Codec[String, Float, TextPlain] = parsedString[Float](_.toFloat).schema(Schema.schemaForFloat)
implicit val double: Codec[String, Double, TextPlain] = parsedString[Double](_.toDouble).schema(Schema.schemaForDouble)
implicit val boolean: Codec[String, Boolean, TextPlain] = parsedString[Boolean](_.toBoolean).schema(Schema.schemaForBoolean)
implicit val uuid: Codec[String, UUID, TextPlain] = parsedString[UUID](UUID.fromString).schema(Schema.schemaForUUID)
implicit val bigDecimal: Codec[String, BigDecimal, TextPlain] = parsedString[BigDecimal](BigDecimal(_)).schema(Schema.schemaForBigDecimal)
implicit val javaBigDecimal: Codec[String, JBigDecimal, TextPlain] =
implicit lazy val string: Codec[String, String, TextPlain] = id[String, TextPlain](TextPlain(), Schema.schemaForString)

implicit lazy val byte: Codec[String, Byte, TextPlain] = parsedString[Byte](_.toByte).schema(Schema.schemaForByte)
implicit lazy val short: Codec[String, Short, TextPlain] = parsedString[Short](_.toShort).schema(Schema.schemaForShort)
implicit lazy val int: Codec[String, Int, TextPlain] = parsedString[Int](_.toInt).schema(Schema.schemaForInt)
implicit lazy val long: Codec[String, Long, TextPlain] = parsedString[Long](_.toLong).schema(Schema.schemaForLong)
implicit lazy val float: Codec[String, Float, TextPlain] = parsedString[Float](_.toFloat).schema(Schema.schemaForFloat)
implicit lazy val double: Codec[String, Double, TextPlain] = parsedString[Double](_.toDouble).schema(Schema.schemaForDouble)
implicit lazy val boolean: Codec[String, Boolean, TextPlain] = parsedString[Boolean](_.toBoolean).schema(Schema.schemaForBoolean)
implicit lazy val uuid: Codec[String, UUID, TextPlain] = parsedString[UUID](UUID.fromString).schema(Schema.schemaForUUID)
implicit lazy val bigDecimal: Codec[String, BigDecimal, TextPlain] = parsedString[BigDecimal](BigDecimal(_)).schema(Schema.schemaForBigDecimal)
implicit lazy val javaBigDecimal: Codec[String, JBigDecimal, TextPlain] =
parsedString[JBigDecimal](new JBigDecimal(_)).schema(Schema.schemaForJBigDecimal)
implicit val bigInt: Codec[String, BigInt, TextPlain] = parsedString[BigInt](BigInt(_)).schema(Schema.schemaForBigInt)
implicit val javaBigInteger: Codec[String, JBigInteger, TextPlain] =
implicit lazy val bigInt: Codec[String, BigInt, TextPlain] = parsedString[BigInt](BigInt(_)).schema(Schema.schemaForBigInt)
implicit lazy val javaBigInteger: Codec[String, JBigInteger, TextPlain] =
parsedString[JBigInteger](new JBigInteger(_)).schema(Schema.schemaForJBigInteger)

// These are marked as lazy as they involve scala-java-time constructs, which massively inflate the bundle size for scala.js applications, even if
Expand Down Expand Up @@ -252,20 +252,20 @@ object Codec extends CodecExtensions with CodecExtensions2 with FormCodecMacros
def parsedString[T: Schema](parse: String => T): Codec[String, T, TextPlain] =
string.map(parse)(_.toString).schema(implicitly[Schema[T]])

implicit val byteArray: Codec[Array[Byte], Array[Byte], OctetStream] =
implicit lazy val byteArray: Codec[Array[Byte], Array[Byte], OctetStream] =
id[Array[Byte], OctetStream](OctetStream(), Schema.schemaForByteArray)
implicit val inputStream: Codec[InputStream, InputStream, OctetStream] =
implicit lazy val inputStream: Codec[InputStream, InputStream, OctetStream] =
id[InputStream, OctetStream](OctetStream(), Schema.schemaForInputStream)
implicit val inputStreamRange: Codec[InputStreamRange, InputStreamRange, OctetStream] =
implicit lazy val inputStreamRange: Codec[InputStreamRange, InputStreamRange, OctetStream] =
id[InputStreamRange, OctetStream](OctetStream(), Schema.schemaForInputStreamRange)
implicit val byteBuffer: Codec[ByteBuffer, ByteBuffer, OctetStream] =
implicit lazy val byteBuffer: Codec[ByteBuffer, ByteBuffer, OctetStream] =
id[ByteBuffer, OctetStream](OctetStream(), Schema.schemaForByteBuffer)
implicit val fileRange: Codec[FileRange, FileRange, OctetStream] =
implicit lazy val fileRange: Codec[FileRange, FileRange, OctetStream] =
id[FileRange, OctetStream](OctetStream(), Schema.schemaForFileRange)
implicit val file: Codec[FileRange, TapirFile, OctetStream] = fileRange.map(_.file)(f => FileRange(f))
implicit lazy val file: Codec[FileRange, TapirFile, OctetStream] = fileRange.map(_.file)(f => FileRange(f))

implicit val formSeqUtf8: Codec[String, Seq[(String, String)], XWwwFormUrlencoded] = formSeq(StandardCharsets.UTF_8)
implicit val formMapUtf8: Codec[String, Map[String, String], XWwwFormUrlencoded] = formMap(StandardCharsets.UTF_8)
implicit lazy val formSeqUtf8: Codec[String, Seq[(String, String)], XWwwFormUrlencoded] = formSeq(StandardCharsets.UTF_8)
implicit lazy val formMapUtf8: Codec[String, Map[String, String], XWwwFormUrlencoded] = formMap(StandardCharsets.UTF_8)

def formSeq(charset: Charset): Codec[String, Seq[(String, String)], XWwwFormUrlencoded] =
string.format(XWwwFormUrlencoded()).map(UrlencodedData.decode(_, charset))(UrlencodedData.encode(_, charset))
Expand Down Expand Up @@ -398,7 +398,7 @@ object Codec extends CodecExtensions with CodecExtensions2 with FormCodecMacros

//

implicit val webSocketFrame: Codec[WebSocketFrame, WebSocketFrame, CodecFormat.TextPlain] = Codec.idPlain()
implicit lazy val webSocketFrame: Codec[WebSocketFrame, WebSocketFrame, CodecFormat.TextPlain] = Codec.idPlain()

/** A codec which expects only text frames (all other frames cause a decoding error) and handles the text using the given `stringCodec`.
*/
Expand Down Expand Up @@ -596,27 +596,27 @@ object Codec extends CodecExtensions with CodecExtensions2 with FormCodecMacros

// header values

implicit val mediaType: Codec[String, MediaType, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
implicit lazy val mediaType: Codec[String, MediaType, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
DecodeResult.fromEitherString(v, MediaType.parse(v))
}(_.toString)

implicit val etag: Codec[String, ETag, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
implicit lazy val etag: Codec[String, ETag, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
DecodeResult.fromEitherString(v, ETag.parse(v))
}(_.toString)

implicit val range: Codec[String, Range, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
implicit lazy val range: Codec[String, Range, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
DecodeResult.fromEitherString(v, Range.parse(v)).flatMap {
case Nil => DecodeResult.Missing
case List(r) => DecodeResult.Value(r)
case rs => DecodeResult.Multiple(rs)
}
}(_.toString)

implicit val contentRange: Codec[String, ContentRange, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
implicit lazy val contentRange: Codec[String, ContentRange, CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
DecodeResult.fromEitherString(v, ContentRange.parse(v))
}(_.toString)

implicit val cacheDirective: Codec[String, List[CacheDirective], CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
implicit lazy val cacheDirective: Codec[String, List[CacheDirective], CodecFormat.TextPlain] = Codec.string.mapDecode { v =>
@tailrec
def toEitherOrList[T, U](l: List[Either[T, U]], acc: List[U]): Either[T, List[U]] = l match {
case Nil => Right(acc.reverse)
Expand All @@ -632,17 +632,17 @@ object Codec extends CodecExtensions with CodecExtensions2 with FormCodecMacros
case Right(r) => DecodeResult.Value(r)
}

implicit val cookie: Codec[String, List[Cookie], TextPlain] = Codec.string.mapDecode(decodeCookie)(cs => Cookie.toString(cs))
implicit val cookies: Codec[List[String], List[Cookie], TextPlain] = Codec.list(cookie).map(_.flatten)(List(_))
implicit lazy val cookie: Codec[String, List[Cookie], TextPlain] = Codec.string.mapDecode(decodeCookie)(cs => Cookie.toString(cs))
implicit lazy val cookies: Codec[List[String], List[Cookie], TextPlain] = Codec.list(cookie).map(_.flatten)(List(_))

private[tapir] def decodeCookieWithMeta(cookie: String): DecodeResult[CookieWithMeta] =
CookieWithMeta.parse(cookie) match {
case Left(e) => DecodeResult.Error(cookie, new RuntimeException(e))
case Right(r) => DecodeResult.Value(r)
}

implicit val cookieWithMeta: Codec[String, CookieWithMeta, TextPlain] = Codec.string.mapDecode(decodeCookieWithMeta)(_.toString)
implicit val cookiesWithMeta: Codec[List[String], List[CookieWithMeta], TextPlain] = Codec.list(cookieWithMeta)
implicit lazy val cookieWithMeta: Codec[String, CookieWithMeta, TextPlain] = Codec.string.mapDecode(decodeCookieWithMeta)(_.toString)
implicit lazy val cookiesWithMeta: Codec[List[String], List[CookieWithMeta], TextPlain] = Codec.list(cookieWithMeta)

// raw tuples

Expand Down Expand Up @@ -740,9 +740,9 @@ case class MultipartCodec[T](rawBodyType: RawBodyType.MultipartBody, codec: Code
}

object MultipartCodec extends MultipartCodecMacros {
private val arrayBytePartListCodec = implicitly[Codec[List[Part[Array[Byte]]], List[Part[Array[Byte]]], OctetStream]]
private lazy val arrayBytePartListCodec = implicitly[Codec[List[Part[Array[Byte]]], List[Part[Array[Byte]]], OctetStream]]

val Default: MultipartCodec[Seq[Part[Array[Byte]]]] =
lazy val Default: MultipartCodec[Seq[Part[Array[Byte]]]] =
Codec
.multipart(Map.empty, Some(PartCodec(RawBodyType.ByteArrayBody, arrayBytePartListCodec)))
// we know that all parts will end up as byte arrays; also, removing/restoring the by-name grouping of parts
Expand Down
28 changes: 14 additions & 14 deletions core/src/main/scala/sttp/tapir/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -304,20 +304,20 @@ object Schema extends LowPrioritySchema with SchemaCompanionMacros {
/** Creates a schema for type `T`, where the low-level representation is binary. */
def binary[T]: Schema[T] = Schema(SBinary())

implicit val schemaForString: Schema[String] = Schema(SString())
implicit val schemaForByte: Schema[Byte] = Schema(SInteger())
implicit val schemaForShort: Schema[Short] = Schema(SInteger())
implicit val schemaForInt: Schema[Int] = Schema(SInteger[Int]()).format("int32")
implicit val schemaForLong: Schema[Long] = Schema(SInteger[Long]()).format("int64")
implicit val schemaForFloat: Schema[Float] = Schema(SNumber[Float]()).format("float")
implicit val schemaForDouble: Schema[Double] = Schema(SNumber[Double]()).format("double")
implicit val schemaForBoolean: Schema[Boolean] = Schema(SBoolean())
implicit val schemaForUnit: Schema[Unit] = Schema(SProduct.empty)
implicit val schemaForFileRange: Schema[FileRange] = Schema(SBinary())
implicit val schemaForByteArray: Schema[Array[Byte]] = Schema(SBinary())
implicit val schemaForByteBuffer: Schema[ByteBuffer] = Schema(SBinary())
implicit val schemaForInputStream: Schema[InputStream] = Schema(SBinary())
implicit val schemaForInputStreamRange: Schema[InputStreamRange] = Schema(SchemaType.SBinary())
implicit lazy val schemaForString: Schema[String] = Schema(SString())
implicit lazy val schemaForByte: Schema[Byte] = Schema(SInteger())
implicit lazy val schemaForShort: Schema[Short] = Schema(SInteger())
implicit lazy val schemaForInt: Schema[Int] = Schema(SInteger[Int]()).format("int32")
implicit lazy val schemaForLong: Schema[Long] = Schema(SInteger[Long]()).format("int64")
implicit lazy val schemaForFloat: Schema[Float] = Schema(SNumber[Float]()).format("float")
implicit lazy val schemaForDouble: Schema[Double] = Schema(SNumber[Double]()).format("double")
implicit lazy val schemaForBoolean: Schema[Boolean] = Schema(SBoolean())
implicit lazy val schemaForUnit: Schema[Unit] = Schema(SProduct.empty)
implicit lazy val schemaForFileRange: Schema[FileRange] = Schema(SBinary())
implicit lazy val schemaForByteArray: Schema[Array[Byte]] = Schema(SBinary())
implicit lazy val schemaForByteBuffer: Schema[ByteBuffer] = Schema(SBinary())
implicit lazy val schemaForInputStream: Schema[InputStream] = Schema(SBinary())
implicit lazy val schemaForInputStreamRange: Schema[InputStreamRange] = Schema(SchemaType.SBinary())

// These are marked as lazy as they involve scala-java-time constructs, which massively inflate the bundle size for scala.js applications, even if
// they are unused within the program. By marking them lazy, it allows them to be *tree shaken* and removed by the optimiser.
Expand Down
2 changes: 1 addition & 1 deletion core/src/main/scala/sttp/tapir/generic/Configuration.scala
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,6 @@ object Configuration {
private val shortKebabCaseSubtypeTransformation: SName => String =
shortIdentitySubtypeTransformation.andThen(kebabCaseTransformation)

implicit val default: Configuration = Configuration(Predef.identity, None, shortIdentitySubtypeTransformation)
implicit lazy val default: Configuration = Configuration(Predef.identity, None, shortIdentitySubtypeTransformation)

}
18 changes: 9 additions & 9 deletions core/src/main/scala/sttp/tapir/typelevel/MatchType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ trait MatchType[T] {

object MatchType extends MatchTypeMacros {

implicit val string: MatchType[String] = matchTypeFromPartial { case _: String => true }
implicit val bool: MatchType[Boolean] = matchTypeFromPartial[Boolean] { case _: Boolean => true }
implicit val char: MatchType[Char] = matchTypeFromPartial[Char] { case _: Char => true }
implicit val byte: MatchType[Byte] = matchTypeFromPartial[Byte] { case _: Byte => true }
implicit val short: MatchType[Short] = matchTypeFromPartial[Short] { case _: Short => true }
implicit val long: MatchType[Long] = matchTypeFromPartial { case _: Long => true }
implicit val float: MatchType[Float] = matchTypeFromPartial[Float] { case _: Float => true }
implicit val double: MatchType[Double] = matchTypeFromPartial[Double] { case _: Double => true }
implicit val int: MatchType[Int] = matchTypeFromPartial[Int] { case _: Int => true }
implicit lazy val string: MatchType[String] = matchTypeFromPartial { case _: String => true }
implicit lazy val bool: MatchType[Boolean] = matchTypeFromPartial[Boolean] { case _: Boolean => true }
implicit lazy val char: MatchType[Char] = matchTypeFromPartial[Char] { case _: Char => true }
implicit lazy val byte: MatchType[Byte] = matchTypeFromPartial[Byte] { case _: Byte => true }
implicit lazy val short: MatchType[Short] = matchTypeFromPartial[Short] { case _: Short => true }
implicit lazy val long: MatchType[Long] = matchTypeFromPartial { case _: Long => true }
implicit lazy val float: MatchType[Float] = matchTypeFromPartial[Float] { case _: Float => true }
implicit lazy val double: MatchType[Double] = matchTypeFromPartial[Double] { case _: Double => true }
implicit lazy val int: MatchType[Int] = matchTypeFromPartial[Int] { case _: Int => true }

private[typelevel] def matchTypeFromPartial[T](pf: PartialFunction[Any, Boolean]): MatchType[T] = { a => pf.lift(a).getOrElse(false) }
}
Loading
Loading