-
Notifications
You must be signed in to change notification settings - Fork 427
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
113 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
OrganizeImports { | ||
groupedImports = Merge | ||
removeUnused = false | ||
} | ||
OrganizeImports.groupedImports = AggressiveMerge | ||
OrganizeImports.targetDialect = Scala3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
28 changes: 28 additions & 0 deletions
28
examples/src/main/scala/sttp/tapir/examples/json/circeAutoDerivationNettySyncServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// {cat=JSON; effects=Direct; server=Netty; JSON=circe}: Return a JSON response with Circe and auto-dervied codecs | ||
|
||
//> using dep com.softwaremill.sttp.tapir::tapir-core:1.11.4 | ||
//> using dep com.softwaremill.sttp.tapir::tapir-json-circe:1.11.4 | ||
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.11.4 | ||
//> using dep ch.qos.logback:logback-classic:1.5.8 | ||
|
||
package sttp.tapir.examples.json | ||
|
||
import sttp.tapir.* | ||
import sttp.tapir.server.netty.sync.NettySyncServer | ||
import sttp.tapir.json.circe.* | ||
import sttp.tapir.generic.auto.* | ||
import io.circe.generic.auto.* | ||
|
||
@main def circeNettySyncServer(): Unit = | ||
case class Country(name: String) | ||
case class Author(name: String, country: Country) | ||
case class Genre(name: String) | ||
case class Book(title: String, genre: Genre, year: Int, author: Author) | ||
|
||
val helloWorld = endpoint.get | ||
.in("hello") | ||
// both Tapir's Schema & circe's Encoder/Decoder are automatically derived thanks to the `auto` imports | ||
.out(jsonBody[Book]) | ||
.handleSuccess(_ => Book("The Witcher: The Last Wish", Genre("Fantasy"), 1993, Author("Andrzej Sapkowski", Country("Poland")))) | ||
|
||
NettySyncServer().addEndpoint(helloWorld).startAndWait() |
42 changes: 42 additions & 0 deletions
42
examples/src/main/scala/sttp/tapir/examples/json/circeNullBody.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// {cat=JSON; effects=Direct; server=Netty; JSON=circe}: Return a JSON body which optionally serializes as `null` | ||
|
||
//> using dep com.softwaremill.sttp.tapir::tapir-core:1.11.4 | ||
//> using dep com.softwaremill.sttp.tapir::tapir-json-circe:1.11.4 | ||
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.11.4 | ||
//> using dep ch.qos.logback:logback-classic:1.5.8 | ||
|
||
package sttp.tapir.examples.json | ||
|
||
import sttp.tapir.* | ||
import sttp.tapir.server.netty.sync.NettySyncServer | ||
import sttp.tapir.json.circe.* | ||
import sttp.tapir.generic.auto.* | ||
import io.circe.generic.auto.* | ||
import io.circe.{Encoder, Json} | ||
|
||
@main def circeNullBody(): Unit = | ||
// the data class | ||
case class Data(value: Int) | ||
|
||
// helper class, which will allow us to serialize the response body to `null` | ||
// this is the same as an `Option`, however a `None` body by default serializes as an empty body (always) | ||
// integration as below is needed for clients which require the body to be exactly `null` | ||
enum OrNull[+T]: | ||
case Value(value: T) | ||
case Null | ||
|
||
// we need to overwrite the encoder that is derived for `OrNull` by default using auto-derivation | ||
// if needed, a `Decoder` would have to be defined in a similar way | ||
given encodeOrNull[A](using Encoder[A]): Encoder[OrNull[A]] = new Encoder[OrNull[A]]: | ||
def apply(a: OrNull[A]): Json = | ||
a match | ||
case OrNull.Value(v) => summon[Encoder[A]](v) | ||
case OrNull.Null => Json.Null | ||
|
||
val helloWorld = endpoint.get | ||
.in("hello") | ||
.in(query[Int]("value")) | ||
.out(jsonBody[OrNull[Data]]) | ||
.handleSuccess(v => if v == 0 then OrNull.Null else OrNull.Value(Data(v))) | ||
|
||
NettySyncServer().addEndpoint(helloWorld).startAndWait() |
33 changes: 33 additions & 0 deletions
33
examples/src/main/scala/sttp/tapir/examples/json/jsoniterNettySyncServer.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// {cat=JSON; effects=Direct; server=Netty; JSON=jsoniter}: Return a JSON response with Jsoniter | ||
|
||
//> using dep com.softwaremill.sttp.tapir::tapir-core:1.11.4 | ||
//> using dep com.softwaremill.sttp.tapir::tapir-jsoniter-scala:1.11.4 | ||
//> using dep com.softwaremill.sttp.tapir::tapir-netty-server-sync:1.11.4 | ||
//> using dep ch.qos.logback:logback-classic:1.5.8 | ||
//> using dep com.github.plokhotnyuk.jsoniter-scala::jsoniter-scala-macros:2.30.11 | ||
|
||
package sttp.tapir.examples.json | ||
|
||
import sttp.tapir.* | ||
import sttp.tapir.server.netty.sync.NettySyncServer | ||
import sttp.tapir.json.jsoniter.* | ||
import sttp.tapir.generic.auto.* | ||
import com.github.plokhotnyuk.jsoniter_scala.core.* | ||
import com.github.plokhotnyuk.jsoniter_scala.macros.* | ||
|
||
@main def jsoniterNettySyncServer(): Unit = | ||
case class Country(name: String) | ||
case class Author(name: String, country: Country) | ||
case class Genre(name: String) | ||
case class Book(title: String, genre: Genre, year: Int, author: Author) | ||
|
||
// we need to derive jsoniter's codec for the top-level class | ||
given bookCodec: JsonValueCodec[Book] = JsonCodecMaker.make | ||
|
||
val helloWorld = endpoint.get | ||
.in("hello") | ||
// uses the derived JsonValueCodec & an auto-derived Tapir's Schema, through the `auto` import | ||
.out(jsonBody[Book]) | ||
.handleSuccess(_ => Book("The Witcher: The Last Wish", Genre("Fantasy"), 1993, Author("Andrzej Sapkowski", Country("Poland")))) | ||
|
||
NettySyncServer().addEndpoint(helloWorld).startAndWait() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters