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

Support examples for byte buffer bodies #2257

Merged
merged 1 commit into from
Jun 21, 2022
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 @@ -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)]
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
}
}