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

Use operation id for endpoint name generation in codegen #3060

Merged
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 @@ -37,7 +37,8 @@ class EndpointGenerator {
|${indent(2)(tags(m.tags))}
|""".stripMargin

val name = m.methodType + p.url.split('/').map(_.replace("{", "").replace("}", "").toLowerCase.capitalize).mkString
val name = m.operationId
.getOrElse(m.methodType + p.url.split('/').map(_.replace("{", "").replace("}", "").toLowerCase.capitalize).mkString)
(name, definition)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package sttp.tapir.codegen.openapi.models

import cats.syntax.either._
import cats.syntax.either._

// https://swagger.io/specification/
object OpenapiModels {
Expand Down Expand Up @@ -30,7 +30,8 @@ object OpenapiModels {
responses: Seq[OpenapiResponse],
requestBody: Option[OpenapiRequestBody],
summary: Option[String] = None,
tags: Option[Seq[String]] = None
tags: Option[Seq[String]] = None,
operationId: Option[String] = None
)

case class OpenapiParameter(
Expand Down Expand Up @@ -141,18 +142,26 @@ object OpenapiModels {
implicit val OpenapiInfoDecoder: Decoder[OpenapiInfo] = deriveDecoder[OpenapiInfo]
implicit val OpenapiParameterDecoder: Decoder[OpenapiParameter] = deriveDecoder[OpenapiParameter]
implicit val OpenapiPathMethodDecoder: Decoder[Seq[OpenapiPathMethod]] = { (c: HCursor) =>
implicit val InnerDecoder
: Decoder[(Seq[OpenapiParameter], Seq[OpenapiResponse], Option[OpenapiRequestBody], Option[String], Option[Seq[String]])] = {
(c: HCursor) =>
for {
parameters <- c.downField("parameters").as[Seq[OpenapiParameter]].orElse(Right(List.empty[OpenapiParameter]))
responses <- c.downField("responses").as[Seq[OpenapiResponse]]
requestBody <- c.downField("requestBody").as[Option[OpenapiRequestBody]]
summary <- c.downField("summary").as[Option[String]]
tags <- c.downField("tags").as[Option[Seq[String]]]
} yield {
(parameters, responses, requestBody, summary, tags)
}
implicit val InnerDecoder: Decoder[
(
Seq[OpenapiParameter],
Seq[OpenapiResponse],
Option[OpenapiRequestBody],
Option[String],
Option[Seq[String]],
Option[String]
)
] = { (c: HCursor) =>
for {
parameters <- c.downField("parameters").as[Seq[OpenapiParameter]].orElse(Right(List.empty[OpenapiParameter]))
responses <- c.downField("responses").as[Seq[OpenapiResponse]]
requestBody <- c.downField("requestBody").as[Option[OpenapiRequestBody]]
summary <- c.downField("summary").as[Option[String]]
tags <- c.downField("tags").as[Option[Seq[String]]]
operationId <- c.downField("operationId").as[Option[String]]
} yield {
(parameters, responses, requestBody, summary, tags, operationId)
}
}
for {
methods <- c.as[
Expand All @@ -163,12 +172,13 @@ object OpenapiModels {
Seq[OpenapiResponse],
Option[OpenapiRequestBody],
Option[String],
Option[Seq[String]]
Option[Seq[String]],
Option[String],
)
]
]
} yield {
methods.map { case (t, (p, r, rb, s, tg)) => OpenapiPathMethod(t, p, r, rb, s, tg) }.toSeq
methods.map { case (t, (p, r, rb, s, tg, oid)) => OpenapiPathMethod(t, p, r, rb, s, tg, oid) }.toSeq
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,8 @@ object TestHelpers {
)
),
summary = None,
tags = Some(Seq("Bookshop"))
tags = Some(Seq("Bookshop")),
operationId = Some("postBooksGenreYear")
),
OpenapiPathMethod(
methodType = "get",
Expand All @@ -180,7 +181,8 @@ object TestHelpers {
),
requestBody = None,
summary = None,
tags = Some(Seq("Bookshop"))
tags = Some(Seq("Bookshop")),
operationId = Some("getBooksGenreYear")
)
)
)
Expand Down Expand Up @@ -284,7 +286,8 @@ object TestHelpers {
),
requestBody = None,
summary = None,
tags = None
tags = None,
operationId = Some("getHello")
)
)
),
Expand All @@ -303,7 +306,8 @@ object TestHelpers {
),
requestBody = None,
summary = None,
tags = None
tags = None,
operationId = Some("getBooksListAll")
)
)
)
Expand Down