Skip to content

Commit

Permalink
Add tags for OpenAPI endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
YassineMEJRI authored and Yassine Mejri committed Apr 21, 2024
1 parent ae85832 commit 67e3df2
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ object EndpointGen {
HttpContentCodec.responseErrorCodec,
doc,
EndpointMiddleware.None,
List.empty,
)

lazy val anyCliEndpoint: Gen[Any, CliReprOf[CliEndpoint]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2468,6 +2468,61 @@ object OpenAPIGenSpec extends ZIOSpecDefault {
| "components" : {}
|}""".stripMargin))
},
test("with tag") {
val endpoint = Endpoint(Method.GET / "static").tag("static")
val openApi =
OpenAPIGen.fromEndpoints(
title = "With tag example",
version = "1.0",
endpoint,
)
val json = toJsonAst(openApi)
assertTrue(json == toJsonAst("""{
| "openapi" : "3.1.0",
| "info" : {
| "title" : "With tag example",
| "version" : "1.0"
| },
| "paths" : {
| "/static" : {
| "get" : {
| "tags" : [
| "static"
| ]
| }
| }
| },
| "components" : {}
|}""".stripMargin))
},
test("with list of tags") {
val endpoint = Endpoint(Method.GET / "static").tags(List("static", "tags"))
val openApi =
OpenAPIGen.fromEndpoints(
title = "With tags example",
version = "1.0",
endpoint,
)
val json = toJsonAst(openApi)
assertTrue(json == toJsonAst("""{
| "openapi" : "3.1.0",
| "info" : {
| "title" : "With tags example",
| "version" : "1.0"
| },
| "paths" : {
| "/static" : {
| "get" : {
| "tags" : [
| "static",
| "tags"
| ]
| }
| }
| },
| "components" : {}
|}""".stripMargin))
},
)

}
40 changes: 39 additions & 1 deletion zio-http/shared/src/main/scala/zio/http/endpoint/Endpoint.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError: HttpCodec[HttpCodecType.ResponseType, HttpCodecError],
doc: Doc,
middleware: Middleware,
tags: List[String],
) { self =>
import self.{middleware => mw}

Expand Down Expand Up @@ -305,6 +306,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -322,6 +324,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -339,6 +342,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -356,6 +360,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -372,7 +377,16 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
errAlternator.Out,
outCombiner.Out,
]] =
Endpoint(route, input, output, error, codecError, doc, mw ++ that)
Endpoint(
route,
input,
output,
error,
codecError,
doc,
mw ++ that,
tags,
)

/**
* Returns a new endpoint derived from this one, whose output type is the
Expand All @@ -389,6 +403,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand Down Expand Up @@ -424,6 +439,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -442,6 +458,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
Doc.empty,
mw,
tags,
)

/**
Expand All @@ -460,6 +477,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -479,6 +497,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand All @@ -497,6 +516,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)

/**
Expand Down Expand Up @@ -562,6 +582,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)
}

Expand All @@ -586,6 +607,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)
}

Expand All @@ -610,6 +632,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)
}

Expand Down Expand Up @@ -646,6 +669,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)
}

Expand All @@ -664,6 +688,7 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
codecError,
doc,
mw,
tags,
)
}

Expand Down Expand Up @@ -709,6 +734,18 @@ final case class Endpoint[PathInput, Input, Err, Output, Middleware <: EndpointM
g: Err1 => Err,
): Endpoint[PathInput, Input, Err1, Output, Middleware] =
copy(error = self.error.transform(f)(g))

/**
* Returns a new API that is derived from this one, which includes a tag that
* will be included in OpenAPI generation.
*/
def tag(that: String): Endpoint[PathInput, Input, Err, Output, Middleware] = copy(tags = self.tags :+ that)

/**
* Returns a new API that is derived from this one, which includes a the list
* of tags that will be included in OpenAPI generation.
*/
def tags(that: List[String]): Endpoint[PathInput, Input, Err, Output, Middleware] = copy(tags = self.tags ++ that)
}

object Endpoint {
Expand All @@ -725,6 +762,7 @@ object Endpoint {
HttpContentCodec.responseErrorCodec,
Doc.empty,
EndpointMiddleware.None,
List.empty,
)

final case class OutErrors[PathInput, Input, Err, Output, Middleware <: EndpointMiddleware, Err2](
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@ object OpenAPIGen {
def operation(endpoint: Endpoint[_, _, _, _, _]): OpenAPI.Operation = {
val maybeDoc = Some(endpoint.doc + pathDoc).filter(!_.isEmpty)
OpenAPI.Operation(
tags = Nil,
tags = endpoint.tags,
summary = None,
description = maybeDoc,
externalDocs = None,
Expand Down

0 comments on commit 67e3df2

Please sign in to comment.