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

Add support for general info #44

Merged
merged 2 commits into from
Feb 13, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 24 additions & 0 deletions core/src/main/scala/tapir/Api.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tapir
import tapir.Api.Info

case class Api(info: Info)
ghostbuster91 marked this conversation as resolved.
Show resolved Hide resolved

object Api {
case class Info(
title: String,
version: String,
description: Option[String] = None,
termsOfService: Option[URL] = None,
contact: Option[Contact] = None,
license: Option[License] = None
)
case class Contact(name: Option[String], email: Option[Email], url: Option[URL])
case class License(name: String, url: Option[URL])

def apply(title: String, version: String): Api = {
new Api(info = Info(title, version))
}

type Email = String
type URL = String
ghostbuster91 marked this conversation as resolved.
Show resolved Hide resolved
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,53 @@
package tapir.docs.openapi

import tapir.Api.{Contact, License}
import tapir.docs.openapi.schema.ObjectSchemasForEndpoints
import tapir.openapi._
import tapir.openapi.{Contact => OpenApiContact, License => OpenApiLicense, _}
import tapir.{EndpointInput, _}

object EndpointToOpenAPIDocs {
def toOpenAPI(title: String, version: String, es: Iterable[Endpoint[_, _, _, _]], options: OpenAPIDocsOptions): OpenAPI = {
def toOpenAPI(api: Api, es: Iterable[Endpoint[_, _, _, _]], options: OpenAPIDocsOptions): OpenAPI = {
val es2 = es.map(nameAllPathCapturesInEndpoint)
val objectSchemas = ObjectSchemasForEndpoints(es2)
val pathCreator = new EndpointToOpenApiPaths(objectSchemas, options)
val componentsCreator = new EndpointToOpenApiComponents(objectSchemas)

val base = OpenAPI(
info = Info(title, None, None, version),
servers = List.empty,
paths = Map.empty,
components = componentsCreator.components
)
val base = apiToOpenApi(api, componentsCreator)

es2.map(pathCreator.pathItem).foldLeft(base) {
case (current, (path, pathItem)) =>
current.addPathItem(path, pathItem)
}
}

private def apiToOpenApi(api: Api, componentsCreator: EndpointToOpenApiComponents): OpenAPI = {
OpenAPI(
info = toOpenAPI(api),
servers = List.empty,
paths = Map.empty,
components = componentsCreator.components
)
}

private def toOpenAPI(api: Api): Info = {
Info(
api.info.title,
api.info.version,
api.info.description,
api.info.termsOfService,
api.info.contact.map(toOpenAPI),
api.info.license.map(toOpenAPI)
)
}

private def toOpenAPI(contact: Contact): OpenApiContact = {
OpenApiContact(contact.name, contact.email, contact.url)
}

private def toOpenAPI(license: License): OpenApiLicense = {
OpenApiLicense(license.name, license.url)
}

private def nameAllPathCapturesInEndpoint(e: Endpoint[_, _, _, _]): Endpoint[_, _, _, _] = {
val (input2, _) = new EndpointInputMapper[Int](
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package tapir.docs.openapi
import tapir.Endpoint
import tapir.{Api, Endpoint}
import tapir.openapi.OpenAPI

trait OpenAPIDocs {
implicit class RichOpenAPIEndpoint[I, E, O, S](e: Endpoint[I, E, O, S]) {
def toOpenAPI(title: String, version: String)(implicit options: OpenAPIDocsOptions): OpenAPI =
EndpointToOpenAPIDocs.toOpenAPI(title, version, Seq(e), options)
def toOpenAPI(api: Api)(implicit options: OpenAPIDocsOptions): OpenAPI =
EndpointToOpenAPIDocs.toOpenAPI(api, Seq(e), options)
}

implicit class RichOpenAPIEndpoints(es: Iterable[Endpoint[_, _, _, _]]) {
def toOpenAPI(title: String, version: String)(implicit options: OpenAPIDocsOptions): OpenAPI =
EndpointToOpenAPIDocs.toOpenAPI(title, version, es, options)
def toOpenAPI(api: Api)(implicit options: OpenAPIDocsOptions): OpenAPI =
EndpointToOpenAPIDocs.toOpenAPI(api, es, options)
}
}
35 changes: 35 additions & 0 deletions docs/openapi-docs/src/test/resources/expected_general_info.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
openapi: 3.0.1
info:
title: Fruits
version: '1.0'
description: Fruits are awesome
termsOfService: our.terms.of.service
contact:
name: Author
email: [email protected]
url: tapir.io
license:
name: MIT
url: mit.license
paths:
/:
get:
operationId: root-get
parameters:
- name: fruit
in: query
required: true
schema:
type: string
- name: amount
in: query
required: false
schema:
type: integer
responses:
'200':
description: ''
content:
text/plain:
schema:
type: string
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
package tapir.docs.openapi

import org.scalatest.{FunSuite, Matchers}
import tapir.Api
import tapir.tests._

class EndpointToOpenAPIDocsTest extends FunSuite with Matchers {
for (e <- allTestEndpoints) {
test(s"${e.show} should convert to open api") {
e.toOpenAPI("title", "19.2-beta-RC1")
e.toOpenAPI(Api("title", "19.2-beta-RC1"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tapir.docs.openapi

import io.circe.generic.auto._
import org.scalatest.{FunSuite, Matchers}
import tapir.Api.{Contact, Info, License}
import tapir._
import tapir.json.circe._
import tapir.openapi.circe.yaml._
Expand All @@ -20,7 +21,7 @@ class VerifyYamlTest extends FunSuite with Matchers {
test("should match the expected yaml") {
val expectedYaml = loadYaml("expected.yml")

val actualYaml = List(in_query_query_out_string, all_the_way).toOpenAPI("Fruits", "1.0").toYaml
val actualYaml = List(in_query_query_out_string, all_the_way).toOpenAPI(Api("Fruits", "1.0")).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
Expand All @@ -32,7 +33,7 @@ class VerifyYamlTest extends FunSuite with Matchers {
test("should match the expected yaml when schema is recursive") {
val expectedYaml = loadYaml("expected_recursive.yml")

val actualYaml = endpoint_wit_recursive_structure.toOpenAPI("Fruits", "1.0").toYaml
val actualYaml = endpoint_wit_recursive_structure.toOpenAPI(Api("Fruits", "1.0")).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
Expand All @@ -46,7 +47,7 @@ class VerifyYamlTest extends FunSuite with Matchers {
val actualYaml = in_query_query_out_string
.in("add")
.in("path")
.toOpenAPI("Fruits", "1.0")(options)
.toOpenAPI(Api("Fruits", "1.0"))(options)
.toYaml
noIndentation(actualYaml) shouldBe expectedYaml
}
Expand All @@ -58,7 +59,7 @@ class VerifyYamlTest extends FunSuite with Matchers {
test("should match the expected yaml for streaming endpoints") {
val expectedYaml = loadYaml("expected_streaming.yml")

val actualYaml = streaming_endpoint.toOpenAPI("Fruits", "1.0").toYaml
val actualYaml = streaming_endpoint.toOpenAPI(Api("Fruits", "1.0")).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
Expand All @@ -71,7 +72,26 @@ class VerifyYamlTest extends FunSuite with Matchers {

val expectedYaml = loadYaml("expected_tags.yml")

val actualYaml = List(userTaggedEndpointShow, userTaggedEdnpointSearch, adminTaggedEndpointAdd).toOpenAPI("Fruits", "1.0").toYaml
val actualYaml = List(userTaggedEndpointShow, userTaggedEdnpointSearch, adminTaggedEndpointAdd).toOpenAPI(Api("Fruits", "1.0")).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
}

test("should match the expected yaml for general info") {
val expectedYaml = loadYaml("expected_general_info.yml")

val api = Api(
Info(
"Fruits",
"1.0",
description = Some("Fruits are awesome"),
termsOfService = Some("our.terms.of.service"),
contact = Some(Contact(Some("Author"), Some("[email protected]"), Some("tapir.io"))),
license = Some(License("MIT", Some("mit.license")))
))

val actualYaml = in_query_query_out_string.toOpenAPI(api).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ trait Encoders {
implicit val encoderComponents: Encoder[Components] = deriveMagnoliaEncoder[Components]
implicit val encoderServer: Encoder[Server] = deriveMagnoliaEncoder[Server]
implicit val encoderInfo: Encoder[Info] = deriveMagnoliaEncoder[Info]
implicit val encoderContact: Encoder[Contact] = deriveMagnoliaEncoder[Contact]
implicit val encoderLicense: Encoder[License] = deriveMagnoliaEncoder[License]
implicit val encoderOpenAPI: Encoder[OpenAPI] = deriveMagnoliaEncoder[OpenAPI]
implicit def encodeList[T: Encoder]: Encoder[List[T]] = {
case Nil => Json.Null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,18 @@ object OpenAPI {
type ReferenceOr[T] = Either[Reference, T]
}

// todo: contact, license
case class Info(
title: String,
version: String,
description: Option[String],
termsOfService: Option[String],
version: String
contact: Option[Contact],
license: Option[License]
)

case class Contact(name: Option[String], email: Option[String], url: Option[String])
case class License(name: String, url: Option[String])

// todo: variables
case class Server(
url: String,
Expand Down
5 changes: 4 additions & 1 deletion playground/src/main/scala/tapir/example/BooksExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ object Endpoints {
// All endpoints report errors as strings, and have the common path prefix '/books'
private val baseEndpoint = endpoint.errorOut(stringBody).in("books")


// The path for this endpoint will be '/books/add', as we are using the base endpoint
val addBook: Endpoint[(Book, AuthToken), String, Unit, Nothing] = baseEndpoint.post
.in("add")
Expand All @@ -37,6 +38,8 @@ object Endpoints {
val booksListingByGenre: Endpoint[BooksQuery, String, Vector[Book], Nothing] = baseEndpoint.get
.in(("list" / path[String]("genre").map(Some(_))(_.get)).and(limitParameter).mapTo(BooksQuery))
.out(jsonBody[Vector[Book]])

val api = Api("The Tapir Library", "1.0")
}

object BooksExample extends App with StrictLogging {
Expand All @@ -49,7 +52,7 @@ object BooksExample extends App with StrictLogging {
import tapir.openapi.circe.yaml._

// interpreting the endpoint description to generate yaml openapi documentation
val docs = List(addBook, booksListing, booksListingByGenre).toOpenAPI("The Tapir Library", "1.0")
val docs = List(addBook, booksListing, booksListingByGenre).toOpenAPI(api)
docs.toYaml
}

Expand Down