Skip to content
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

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

amorfis
Copy link
Member

@amorfis amorfis commented Jan 10, 2025

No description provided.

@@ -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
Copy link
Member

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 ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@@ -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
Copy link
Member

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)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@adamw
Copy link
Member

adamw commented Jan 14, 2025

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 :)

@amorfis
Copy link
Member Author

amorfis commented Jan 14, 2025

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
Copy link
Member

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
Copy link
Member

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()
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants