-
Notifications
You must be signed in to change notification settings - Fork 423
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1101 from softwaremill/yaml-test-cleanup
Yaml test cleanup
- Loading branch information
Showing
48 changed files
with
645 additions
and
579 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
149 changes: 149 additions & 0 deletions
149
docs/openapi-docs/src/test/scala/sttp/tapir/docs/openapi/VerifyYamlExampleTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
package sttp.tapir.docs.openapi | ||
|
||
import io.circe.generic.auto._ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.tapir.EndpointIO.Example | ||
import sttp.tapir.docs.openapi.dtos.{Author, Book, Country, Genre} | ||
import sttp.tapir.generic.auto._ | ||
import sttp.tapir.json.circe._ | ||
import sttp.tapir.openapi.Info | ||
import sttp.tapir.openapi.circe.yaml._ | ||
import sttp.tapir.tests.{Person, _} | ||
import sttp.tapir.{endpoint, _} | ||
|
||
import java.time.ZoneOffset.UTC | ||
import java.time.ZonedDateTime | ||
|
||
class VerifyYamlExampleTest extends AnyFunSuite with Matchers { | ||
|
||
test("support example of list and not-list types") { | ||
val expectedYaml = load("example/expected_examples_of_list_and_not_list_types.yml") | ||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint.post | ||
.in(query[List[String]]("friends").example(List("bob", "alice"))) | ||
.in(query[String]("current-person").example("alan")) | ||
.in(jsonBody[Person].example(Person("bob", 23))), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
|
||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("support multiple examples with explicit names") { | ||
val expectedYaml = load("example/expected_multiple_examples_with_names.yml") | ||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint.post | ||
.out( | ||
jsonBody[Entity].examples( | ||
List( | ||
Example.of(Person("michal", 40), Some("Michal"), Some("Some summary")), | ||
Example.of(Organization("acme"), Some("Acme")) | ||
) | ||
) | ||
), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
|
||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("support multiple examples with default names") { | ||
val expectedYaml = load("example/expected_multiple_examples_with_default_names.yml") | ||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint.post | ||
.in(jsonBody[Person].example(Person("bob", 23)).example(Person("matt", 30))), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
|
||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("support example name even if there is a single example") { | ||
val expectedYaml = load("example/expected_single_example_with_name.yml") | ||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint.post | ||
.out( | ||
jsonBody[Entity].example( | ||
Example(Person("michal", 40), Some("Michal"), Some("Some summary")) | ||
) | ||
), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
|
||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("support multiple examples with both explicit and default names ") { | ||
val expectedYaml = load("example/expected_multiple_examples_with_explicit_and_default_names.yml") | ||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint.post | ||
.in(jsonBody[Person].examples(List(Example.of(Person("bob", 23), name = Some("Bob")), Example.of(Person("matt", 30))))), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
|
||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("support examples in different IO params") { | ||
val expectedYaml = load("example/expected_multiple_examples.yml") | ||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint.post | ||
.in(path[String]("country").example("Poland").example("UK")) | ||
.in(query[String]("current-person").example("alan").example("bob")) | ||
.in(jsonBody[Person].example(Person("bob", 23)).example(Person("alan", 50))) | ||
.in(header[String]("X-Forwarded-User").example("user1").example("user2")) | ||
.in(cookie[String]("cookie-param").example("cookie1").example("cookie2")) | ||
.out(jsonBody[Entity].example(Person("michal", 40)).example(Organization("acme"))), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
|
||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("automatically add example for fixed header") { | ||
val expectedYaml = load("example/expected_fixed_header_example.yml") | ||
|
||
val e = endpoint.in(header("Content-Type", "application/json")) | ||
val actualYaml = OpenAPIDocsInterpreter.toOpenAPI(e, Info("Examples", "1.0")).toYaml | ||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
|
||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("should match the expected yaml when using schema with custom example") { | ||
val expectedYaml = load("example/expected_schema_example.yml") | ||
|
||
val expectedDateTime = ZonedDateTime.of(2021, 1, 1, 1, 1, 1, 1, UTC) | ||
val expectedBook = Book("title", Genre("name", "desc"), 2021, Author("name", Country("country"))) | ||
|
||
implicit val testSchemaZonedDateTime: Schema[ZonedDateTime] = Schema.schemaForZonedDateTime.encodedExample(expectedDateTime) | ||
implicit val testSchemaBook = implicitly[Schema[Book]].encodedExample(circeCodec[Book].encode(expectedBook)) | ||
|
||
val endpoint_with_dateTimes = endpoint.post.in(jsonBody[ZonedDateTime]).out(jsonBody[Book]) | ||
|
||
val actualYaml = OpenAPIDocsInterpreter.toOpenAPI(endpoint_with_dateTimes, Info("Examples", "1.0")).toYaml | ||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
|
||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
} |
79 changes: 79 additions & 0 deletions
79
docs/openapi-docs/src/test/scala/sttp/tapir/docs/openapi/VerifyYamlOneOfTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package sttp.tapir.docs.openapi | ||
|
||
import io.circe.generic.auto._ | ||
import org.scalatest.funsuite.AnyFunSuite | ||
import org.scalatest.matchers.should.Matchers | ||
import sttp.model.StatusCode | ||
import sttp.tapir.docs.openapi.VerifyYamlOneOfTest._ | ||
import sttp.tapir.generic.auto._ | ||
import sttp.tapir.json.circe.jsonBody | ||
import sttp.tapir.openapi.Info | ||
import sttp.tapir.openapi.circe.yaml._ | ||
import sttp.tapir.{Codec, CodecFormat, Schema, SchemaType, endpoint, header, plainBody, statusCode, statusDefaultMapping, statusMapping} | ||
|
||
class VerifyYamlOneOfTest extends AnyFunSuite with Matchers { | ||
|
||
test("should support multiple status codes") { | ||
val expectedYaml = load("oneOf/expected_status_codes.yml") | ||
|
||
// work-around for #10: unsupported sealed trait families | ||
implicit val schemaForErrorInfo: Schema[ErrorInfo] = Schema[ErrorInfo](SchemaType.SProduct(SchemaType.SObjectInfo("ErrorInfo"), Nil)) | ||
|
||
val e = endpoint.errorOut( | ||
sttp.tapir.oneOf( | ||
statusMapping(StatusCode.NotFound, jsonBody[NotFound].description("not found")), | ||
statusMapping(StatusCode.Unauthorized, jsonBody[Unauthorized].description("unauthorized")), | ||
statusDefaultMapping(jsonBody[Unknown].description("unknown")) | ||
) | ||
) | ||
|
||
val actualYaml = OpenAPIDocsInterpreter.toOpenAPI(e, Info("Fruits", "1.0")).toYaml | ||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
|
||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("should support multiple the same status codes") { | ||
val expectedYaml = load("oneOf/expected_the_same_status_codes.yml") | ||
|
||
implicit val unauthorizedTextPlainCodec: Codec[String, Unauthorized, CodecFormat.TextPlain] = | ||
Codec.string.map(Unauthorized.apply _)(_.realm) | ||
|
||
val e = endpoint.out( | ||
sttp.tapir.oneOf( | ||
statusMapping(StatusCode.Ok, jsonBody[NotFound].description("not found")), | ||
statusMapping(StatusCode.Ok, plainBody[Unauthorized]), | ||
statusMapping(StatusCode.NoContent, jsonBody[Unknown].description("unknown")) | ||
) | ||
) | ||
|
||
val actualYaml = OpenAPIDocsInterpreter.toOpenAPI(e, Info("Fruits", "1.0")).toYaml | ||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
|
||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
test("use status codes declared with description") { | ||
val expectedYaml = load("oneOf/expected_one_of_status_codes.yml") | ||
|
||
val actualYaml = OpenAPIDocsInterpreter | ||
.toOpenAPI( | ||
endpoint | ||
.out(header[String]("Location")) | ||
.errorOut(statusCode.description(StatusCode.NotFound, "entity not found").description(StatusCode.BadRequest, "")), | ||
Info("Entities", "1.0") | ||
) | ||
.toYaml | ||
val actualYamlNoIndent = noIndentation(actualYaml) | ||
|
||
actualYamlNoIndent shouldBe expectedYaml | ||
} | ||
|
||
} | ||
|
||
object VerifyYamlOneOfTest { | ||
sealed trait ErrorInfo | ||
case class NotFound(what: String) extends ErrorInfo | ||
case class Unauthorized(realm: String) extends ErrorInfo | ||
case class Unknown(code: Int, msg: String) extends ErrorInfo | ||
} |
Oops, something went wrong.