Skip to content

Commit

Permalink
Catch anonymous oneOf instances
Browse files Browse the repository at this point in the history
  • Loading branch information
blast-hardcheese committed Dec 25, 2023
1 parent ef1d382 commit b3e0edd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 13 deletions.
45 changes: 32 additions & 13 deletions modules/sample-akkaHttp/src/test/scala/core/issues/Issue195.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,45 @@ import io.circe._, io.circe.syntax._
import issues.issue195.server.akkaHttp.definitions._

class Issue195 extends AnyFunSpec with Matchers {
def emit[A](label: String, func: () => A, expected: Json)(implicit ev: A => Foo): Unit =
describe(label) {
it("should encode") {
Foo(func()).asJson shouldBe expected
}
describe("oneOf mapping") {
def emit[A](label: String, func: () => A, expected: Json)(implicit ev: A => Foo): Unit =
describe(label) {
it("should encode") {
Foo(func()).asJson shouldBe expected
}

it("should decode") {
expected.as[Foo] shouldBe Right(Foo(func()))
}
it("should decode") {
expected.as[Foo] shouldBe Right(Foo(func()))
}

it("should round-trip") {
Foo(func()).asJson.as[Foo] shouldBe Right(Foo(func()))
it("should round-trip") {
Foo(func()).asJson.as[Foo] shouldBe Right(Foo(func()))
}
}
}

describe("oneOf mapping") {
emit("TypeA", () => TypeA("foo"), Json.obj("type" -> Json.fromString("foo"), "beepBorp" -> Json.fromString("typea")))
emit("TypeB", () => TypeB("foo"), Json.obj("type" -> Json.fromString("foo"), "beepBorp" -> Json.fromString("TypeB")))
emit("TypeC", () => Foo.WrappedC(TypeC("foo")), Json.obj("C" -> Json.obj("type" -> Json.fromString("foo")), "beepBorp" -> Json.fromString("WrappedC")))
emit("typed", () => Foo.Nested2(Typed("foo")), Json.obj("D" -> Json.obj("type" -> Json.fromString("foo")), "beepBorp" -> Json.fromString("Nested2")))
emit("TypeE", () => Foo.Nested3(TypeE("foo")), Json.obj("E" -> Json.obj("type" -> Json.fromString("foo")), "beepBorp" -> Json.fromString("Nested3")))
}

describe("anonymous oneOf mapping") {
def emit[A](label: String, func: () => A, expected: Json)(implicit ev: A => X.Links): Unit =
describe(label) {
it("should encode") {
X(func()).asJson shouldBe expected
}

it("should decode") {
expected.as[X] shouldBe Right(X(func()))
}

it("should round-trip") {
X(func()).asJson.as[X] shouldBe Right(X(func()))
}
}

emit("first branch, Vector[String]", () => X.Links(Vector("1", "2")), Json.obj("links" -> Json.arr(Json.fromString("1"), Json.fromString("2"))))
emit("second branch, object", () => X.Links(Json.obj("a" -> Json.fromString("hey"))), Json.obj("links" -> Json.obj("a" -> Json.fromString("hey"))))
}
}
10 changes: 10 additions & 0 deletions modules/sample/src/main/resources/issues/issue195.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ components:
propertyName: beepBorp
mapping:
typea: '#/components/schemas/TypeA'
X:
type: object
required: [ links ]
properties:
links:
oneOf:
- type: array
items:
type: string
- type: object
paths:
/foobar:
get:
Expand Down

0 comments on commit b3e0edd

Please sign in to comment.