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 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 @@ -5,25 +5,29 @@ import tapir.openapi._
import tapir.{EndpointInput, _}

object EndpointToOpenAPIDocs {
def toOpenAPI(title: String, version: String, es: Iterable[Endpoint[_, _, _, _]], options: OpenAPIDocsOptions): OpenAPI = {
def toOpenAPI(api: Info, 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(info: Info, componentsCreator: EndpointToOpenApiComponents): OpenAPI = {
OpenAPI(
info = info,
servers = List.empty,
paths = Map.empty,
components = componentsCreator.components
)
}

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.openapi.OpenAPI
import tapir.openapi.{Info, 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(info: Info)(implicit options: OpenAPIDocsOptions): OpenAPI =
EndpointToOpenAPIDocs.toOpenAPI(info, 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(info: Info)(implicit options: OpenAPIDocsOptions): OpenAPI =
Copy link
Member

Choose a reason for hiding this comment

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

a może zostawić oba warianty: jeden z title: String, version: String i drugi z info: Info? Ten drugi by dawał pełną elastycznośc, pierwszy by zapewniał "szybką ścieżkę"?

EndpointToOpenAPIDocs.toOpenAPI(info, 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.openapi.Info
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(Info("title", "19.2-beta-RC1"))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import org.scalatest.{FunSuite, Matchers}
import tapir._
import tapir.json.circe._
import tapir.openapi.circe.yaml._
import tapir.openapi.{Contact, Info, License}
import tapir.tests._

import scala.io.Source
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(Info("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(Info("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(Info("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(Info("Fruits", "1.0")).toYaml
val actualYamlNoIndent = noIndentation(actualYaml)

actualYamlNoIndent shouldBe expectedYaml
Expand All @@ -71,7 +72,25 @@ 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(Info("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 = 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
14 changes: 9 additions & 5 deletions openapi/openapi-model/src/main/scala/tapir/openapi/OpenAPI.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package tapir.openapi

import OpenAPI.ReferenceOr
import tapir.openapi.OpenAPI.ReferenceOr

// todo security, tags, externaldocs
case class OpenAPI(openapi: String = "3.0.1",
Expand All @@ -23,14 +23,18 @@ object OpenAPI {
type ReferenceOr[T] = Either[Reference, T]
}

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

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
4 changes: 3 additions & 1 deletion playground/src/main/scala/tapir/example/BooksExample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package tapir.example

import com.typesafe.scalalogging.StrictLogging
import tapir.example.Endpoints.Limit
import tapir.openapi.Info

case class Book(title: String, genre: String, year: Int)
case class BooksQuery(genre: Option[String], limit: Limit)
Expand Down Expand Up @@ -49,7 +50,8 @@ 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 api = Info("The Tapir Library", "1.0")
val docs = List(addBook, booksListing, booksListingByGenre).toOpenAPI(api)
docs.toYaml
}

Expand Down
3 changes: 2 additions & 1 deletion playground/src/main/scala/tapir/tests/Tests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import tapir._
import tapir.server.akkahttp._
import tapir.client.sttp._
import tapir.docs.openapi._
import tapir.openapi.Info

import scala.concurrent.{Await, Future}
import scala.concurrent.duration._
Expand Down Expand Up @@ -50,7 +51,7 @@ object Tests extends App {

import tapir.openapi.circe.yaml._

val docs = e.toOpenAPI("Example 1", "1.0")
val docs = e.toOpenAPI(Info("Example 1", "1.0"))
println("XXX")
println(docs.toYaml)
println("YYY")
Expand Down
3 changes: 2 additions & 1 deletion playground/src/main/scala/tapir/tests/Tests2.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import tapir.docs.openapi._
import tapir.openapi.circe.yaml._
import io.circe.generic.auto._
import tapir.json.circe._
import tapir.openapi.Info

object Tests2 extends App {
case class Address(street: String, number: Option[Int])
Expand All @@ -16,7 +17,7 @@ object Tests2 extends App {
.in(query[Option[String]]("q3"))
.out(jsonBody[User].example(User("x", 10, Address("y", Some(20)))))

val docs = e.toOpenAPI("Example 1", "1.0")
val docs = e.toOpenAPI(Info("Example 1", "1.0"))
println(docs.toYaml)
}

Expand Down
3 changes: 2 additions & 1 deletion playground/src/main/scala/tapir/tests/Tests4.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package tapir.tests
import tapir.openapi.Info

object Tests4 {
import tapir._
Expand All @@ -24,7 +25,7 @@ object Tests4 {
import tapir.openapi.circe._
import tapir.openapi.circe.yaml._

val docs = booksListing.toOpenAPI("My Bookshop", "1.0")
val docs = booksListing.toOpenAPI(Info("My Bookshop", "1.0"))
println(docs.toYaml)
}

Expand Down