-
Notifications
You must be signed in to change notification settings - Fork 423
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Example: respond with 404 if None returned from server logic #4257
base: master
Are you sure you want to change the base?
Conversation
@@ -56,6 +57,16 @@ import sttp.tapir.generic.auto.* | |||
val booksListingByGenre: PublicEndpoint[BooksQuery, String, Vector[Book], Any] = baseEndpoint.get | |||
.in(("list" / path[String]("genre").map(Option(_))(_.get)).and(limitParameter).mapTo[BooksQuery]) | |||
.out(jsonBody[Vector[Book]]) | |||
|
|||
// Optional value from serverLogic, responding with 404 when None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's create a stand-alone, separate example. Otherwise this one will become a monster-example ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
… None and 200 and body for Some
This reverts commit 746ad5f.
@@ -0,0 +1,131 @@ | |||
// {cat=Optional value; effects=Future; server=Pekko HTTP; JSON=circe; docs=Swagger UI}: Optional returned from the server logic, resulting in 404 if None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's put it in the "Error handling" category (https://tapir.softwaremill.com/en/latest/examples.html)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There's a lot that is copied from the books example, the example can be greatly simplified - by keeping only the crucial parts (a simply case class, json serialization to None/Some). Everything else just obscure the image :) |
It should be better now - much code removed |
.out(oneOf( | ||
oneOfVariantExactMatcher(StatusCode.NotFound, jsonBody[Option[Beer]])(None), | ||
oneOfVariantValueMatcher(StatusCode.Ok, jsonBody[Option[Beer]]) { | ||
case Some(book) => true |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
beer ;)
@@ -0,0 +1,65 @@ | |||
// {cat=Error handling; effects=Future; server=Pekko HTTP; JSON=circe; docs=Swagger UI}: Optional returned from the server logic, resulting in 404 if None |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no swagger here I suppose?
} | ||
|
||
|
||
implicit val actorSystem: ActorSystem = ActorSystem() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we're in scala 3, should be given
No description provided.