Skip to content

Commit

Permalink
Merge pull request #1341 from ae0n/optional-enum-in-api-docs-fix
Browse files Browse the repository at this point in the history
fixed docs generation of referenced enums when they're inside of a collection
  • Loading branch information
adamw authored Jun 29, 2021
2 parents f172d7f + 1f3a720 commit 091b5f3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 3 deletions.
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ components:
- blue
- red
shapeType:
type: string
type: string
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: 3.0.3
info:
title: Enums
version: '1.0'
paths:
/:
get:
operationId: getRoot
responses:
'200':
description: ''
content:
application/json:
schema:
$ref: '#/components/schemas/Square'
components:
schemas:
CornerStyle:
type: string
enum:
- rounded
- straight
Square:
type: object
properties:
cornerStyle:
$ref: '#/components/schemas/CornerStyle'
tags:
type: array
items:
$ref: '#/components/schemas/Tag'
Tag:
type: string
enum:
- tag1
- tag2
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import enumeratum.{EnumEntry, _}
import io.circe.generic.auto._
import org.scalatest.funsuite.AnyFunSuite
import org.scalatest.matchers.should.Matchers
import sttp.tapir.Schema.SName
import sttp.tapir.docs.openapi.VerifyYamlEnumerationTest._
import sttp.tapir.generic.auto._
import sttp.tapir.json.circe._
Expand Down Expand Up @@ -82,6 +83,16 @@ class VerifyYamlEnumerationTest extends AnyFunSuite with Matchers {

noIndentation(actualYaml) shouldBe expectedYaml
}

test("should support optional enums and sequences of enums") {
implicit val shapeCodec: io.circe.Codec[Square] = null
val e = endpoint.get.out(jsonBody[Square])

val expectedYaml = load("enum/expected_enum_collections.yml")
val actualYaml = OpenAPIDocsInterpreter().toOpenAPI(e, "Enums", "1.0").toYaml

noIndentation(actualYaml) shouldBe expectedYaml
}
}

object VerifyYamlEnumerationTest {
Expand Down Expand Up @@ -122,4 +133,26 @@ object VerifyYamlEnumerationTest {

case class Error1Response(error: ErrorCode)
case class Error2Response(error: ErrorCode)

object CornerStyle extends Enumeration {
type CornerStyle = Value

val Rounded = Value("rounded")
val Straight = Value("straight")

implicit def schemaForEnum: Schema[Value] =
Schema.string.validate(Validator.enumeration(values.toList, v => Option(v), Some(SName("CornerStyle"))))
}

object Tag extends Enumeration {
type Tag = Value

val Tag1 = Value("tag1")
val Tag2 = Value("tag2")

implicit def schemaForEnum: Schema[Value] =
Schema.string.validate(Validator.enumeration(values.toList, v => Option(v), Some(SName("Tag"))))
}

case class Square(cornerStyle: Option[CornerStyle.Value], tags: Seq[Tag.Value])
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ class VerifyYamlCoproductTest extends AnyFunSuite with Matchers {
val endpoint = sttp.tapir.endpoint.get.out(jsonBody[Shape])

val expectedYaml = load("coproduct/expected_coproduct_discriminator_with_enum_circe.yml")
val actualYaml = OpenAPIDocsInterpreter().toOpenAPI(endpoint, "My Bookshop", "1.0").toYaml

val actualYaml =
OpenAPIDocsInterpreter()
.toOpenAPI(endpoint, "My Bookshop", "1.0")
.toYaml
noIndentation(actualYaml) shouldBe expectedYaml
}

Expand Down

0 comments on commit 091b5f3

Please sign in to comment.