Skip to content

Commit

Permalink
Merge pull request #2257 from softwaremill/byte-buffer-example
Browse files Browse the repository at this point in the history
Support examples for byte buffer bodies
  • Loading branch information
adamw authored Jun 21, 2022
2 parents 7b84410 + 854b107 commit 25fc32e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
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
}
}

0 comments on commit 25fc32e

Please sign in to comment.