Skip to content

Commit

Permalink
Fixes async api encoding when using discriminator (softwaremill#43).
Browse files Browse the repository at this point in the history
Signed-off-by: Marcin Kielar <[email protected]>
  • Loading branch information
Marcin Kielar committed Jul 26, 2024
1 parent 79f5287 commit fe5bb68
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ package circe {
import sttp.apispec.internal.JsonSchemaCirceEncoders

trait SttpAsyncAPICirceEncoders extends JsonSchemaCirceEncoders {
// note: avoids rendering of (unsupported) discriminator mapping
override implicit val encoderDiscriminator: Encoder[Discriminator] = {
case Discriminator(propertyName, _) => Json.obj("discriminator" := propertyName)
}

// note: these are strict val-s, order matters!
implicit val encoderReference: Encoder[Reference] = deriveEncoder[Reference]
implicit def encoderReferenceOr[T: Encoder]: Encoder[ReferenceOr[T]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,33 @@ class EncoderTest extends AnyFunSuite {
assert(expected === comp.asJson.deepDropNullValues)
}

test("encode discriminator") {
val expected =
parse("""{
| "payload" : {
| "oneOf" : [
| {
| "$ref" : "Dog"
| },
| {
| "$ref" : "Cat"
| }
| ],
| "discriminator" : {
| "discriminator" : "pet"
| }
| }
|}""".stripMargin)

val schema = Schema.oneOf(
List(Schema.referenceTo("", "Dog"), Schema.referenceTo("", "Cat")),
Some(Discriminator("pet", Some(ListMap("Dog" -> "Dog", "Cat" -> "Cat")))))
val message = SingleMessage(payload = Some(Right(schema)))

assert(expected === message.asJson.deepDropNullValues)
}



def parse(s: String): Json = io.circe.parser.parse(s).fold(throw _, identity)
}

0 comments on commit fe5bb68

Please sign in to comment.