Skip to content

Commit

Permalink
Add schema for uri (#3672)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeriaux committed Apr 12, 2024
1 parent cd49efa commit 4b1833d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 12 deletions.
25 changes: 14 additions & 11 deletions core/src/main/scala/sttp/tapir/Codec.scala
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
package sttp.tapir

import java.io.InputStream
import java.math.{BigDecimal => JBigDecimal, BigInteger => JBigInteger}
import java.nio.ByteBuffer
import java.nio.charset.{Charset, StandardCharsets}
import java.time._
import java.time.format.{DateTimeFormatter, DateTimeParseException}
import java.util.{Base64, Date, UUID}
import sttp.model._
import sttp.model.headers.{CacheDirective, Cookie, CookieWithMeta, ETag, Range, ContentRange}
import sttp.model.headers.{CacheDirective, ContentRange, Cookie, CookieWithMeta, ETag, Range}
import sttp.tapir.CodecFormat.{MultipartFormData, OctetStream, TextPlain, XWwwFormUrlencoded}
import sttp.tapir.DecodeResult.Error.MultipartDecodeException
import sttp.tapir.DecodeResult._
Expand All @@ -18,6 +11,13 @@ import sttp.tapir.macros.{CodecMacros, FormCodecMacros, MultipartCodecMacros}
import sttp.tapir.model.{UnsupportedWebSocketFrameException, UsernamePassword}
import sttp.ws.WebSocketFrame

import java.io.InputStream
import java.math.{BigDecimal => JBigDecimal, BigInteger => JBigInteger}
import java.nio.ByteBuffer
import java.nio.charset.{Charset, StandardCharsets}
import java.time._
import java.time.format.{DateTimeFormatter, DateTimeParseException}
import java.util.{Base64, Date, UUID}
import scala.annotation.{implicitNotFound, tailrec}
import scala.collection.immutable.ListMap
import scala.concurrent.duration.{Duration => SDuration}
Expand Down Expand Up @@ -238,10 +238,13 @@ object Codec extends CodecExtensions with CodecExtensions2 with FormCodecMacros
}
}(h => OffsetDateTime.of(h, ZoneOffset.UTC).toString)
.schema(Schema.schemaForLocalDateTime)

implicit val uri: PlainCodec[Uri] =
string.mapDecode(raw => Uri.parse(raw).fold(e => DecodeResult.Error(raw, new IllegalArgumentException(e)), DecodeResult.Value(_)))(
_.toString()
)
string
.mapDecode(raw => Uri.parse(raw).fold(e => DecodeResult.Error(raw, new IllegalArgumentException(e)), DecodeResult.Value(_)))(
_.toString()
)
.schema(Schema.schemaForUri)

def parsedString[T: Schema](parse: String => T): Codec[String, T, TextPlain] =
string.map(parse)(_.toString).schema(implicitly[Schema[T]])
Expand Down
4 changes: 3 additions & 1 deletion core/src/main/scala/sttp/tapir/Schema.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sttp.tapir

import sttp.model.Part
import sttp.model.{Part, Uri}
import sttp.tapir.Schema.{SName, Title}
import sttp.tapir.SchemaType._
import sttp.tapir.generic.{Configuration, Derived}
Expand Down Expand Up @@ -297,6 +297,8 @@ object Schema extends LowPrioritySchema with SchemaCompanionMacros {
implicit val schemaForBigInt: Schema[BigInt] = Schema(SInteger())
implicit val schemaForJBigInteger: Schema[JBigInteger] = Schema(SInteger())
implicit val schemaForFile: Schema[TapirFile] = Schema(SBinary())
implicit val schemaForUri: Schema[Uri] = Schema(SString())
.encodedExample(Uri("https", "example.com"))

implicit def schemaForOption[T: Schema]: Schema[Option[T]] = implicitly[Schema[T]].asOption
implicit def schemaForArray[T: Schema]: Schema[Array[T]] = implicitly[Schema[T]].asArray
Expand Down

0 comments on commit 4b1833d

Please sign in to comment.