From 854b107d28f93ad45ccec080158e478aa932bcc2 Mon Sep 17 00:00:00 2001 From: adamw Date: Tue, 21 Jun 2022 15:53:15 +0200 Subject: [PATCH] Support examples for byte buffer bodies --- .../sttp/tapir/docs/apispec/package.scala | 9 +++++++- .../example/expected_byte_buffer_example.yml | 22 +++++++++++++++++++ .../docs/openapi/VerifyYamlExampleTest.scala | 14 ++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 docs/openapi-docs/src/test/resources/example/expected_byte_buffer_example.yml diff --git a/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/package.scala b/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/package.scala index 4931dea5d5..f3a3b6573e 100644 --- a/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/package.scala +++ b/docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/package.scala @@ -4,6 +4,9 @@ import sttp.apispec.{ExampleMultipleValue, ExampleSingleValue, ExampleValue, Sec import sttp.tapir.Schema.SName import sttp.tapir.{AnyEndpoint, Codec, EndpointInput, Schema, SchemaType} +import java.nio.ByteBuffer +import java.nio.charset.Charset + package object apispec { private[docs] type SchemeName = String private[docs] type SecuritySchemes = Map[EndpointInput.Auth[_, _], (SchemeName, SecurityScheme)] @@ -23,7 +26,11 @@ package object apispec { result } - private def rawToString[T](v: Any): String = v.toString + private def rawToString[T](v: Any): String = v match { + case a: Array[Byte] => new String(a, "UTF-8") + case b: ByteBuffer => Charset.forName("UTF-8").decode(b).toString + case _ => v.toString + } private[docs] def exampleValue[T](v: String): ExampleValue = ExampleSingleValue(v) private[docs] def exampleValue[T](codec: Codec[_, T, _], e: T): Option[ExampleValue] = exampleValue(codec.schema, codec.encode(e)) diff --git a/docs/openapi-docs/src/test/resources/example/expected_byte_buffer_example.yml b/docs/openapi-docs/src/test/resources/example/expected_byte_buffer_example.yml new file mode 100644 index 0000000000..8e11ddc87d --- /dev/null +++ b/docs/openapi-docs/src/test/resources/example/expected_byte_buffer_example.yml @@ -0,0 +1,22 @@ +openapi: 3.0.3 +info: + title: Users + version: '1.0' +paths: + /: + get: + operationId: getRoot + responses: + '200': + description: '' + headers: + Content-Type: + required: true + schema: + type: string + content: + text/csv: + schema: + type: string + format: binary + example: a,b,c,1024,e,f,42,g h,i \ No newline at end of file diff --git a/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlExampleTest.scala b/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlExampleTest.scala index 4a7ca7d701..375d336d12 100644 --- a/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlExampleTest.scala +++ b/docs/openapi-docs/src/test/scalajvm/sttp/tapir/docs/openapi/VerifyYamlExampleTest.scala @@ -14,6 +14,7 @@ import sttp.tapir.json.circe._ import sttp.tapir.tests.data.{Entity, Organization, Person} import sttp.tapir.{endpoint, _} +import java.nio.ByteBuffer import java.time.ZoneOffset.UTC import java.time.ZonedDateTime @@ -183,4 +184,17 @@ class VerifyYamlExampleTest extends AnyFunSuite with Matchers { noIndentation(actualYaml) shouldBe expectedYaml } + + test("should support byte buffer examples") { + val e = endpoint.out( + byteBufferBody + .example(Example.of(ByteBuffer.wrap("a,b,c,1024,e,f,42,g h,i".getBytes("UTF-8")))) + .and(header("Content-Type", "text/csv")) + ) + + val expectedYaml = load("example/expected_byte_buffer_example.yml") + val actualYaml = OpenAPIDocsInterpreter().toOpenAPI(e, Info("Users", "1.0")).toYaml + + noIndentation(actualYaml) shouldBe expectedYaml + } }