Skip to content
This repository was archived by the owner on Oct 20, 2023. It is now read-only.

invalid discriminator mapping after removing unused references when allOf is used #205

@ghostbuster91

Description

@ghostbuster91

input:

openapi: 3.0.3
info:
  title: Entities
  version: "1.0"
paths:
  /:
    get:
      operationId: getDog
      responses:
        "200":
          description: ""
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/Dog"
components:
  schemas:
    Pet:
      type: object
      required:
        - petType
      properties:
        petType:
          type: string
      discriminator:
        propertyName: petType
        mapping:
          dog: Dog
    Dog:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Dog`
          properties:
            bark:
              type: string
    Lizard:
      allOf:
        - $ref: '#/components/schemas/Pet'
        - type: object
          # all other properties specific to a `Lizard`
          properties:
            lovesRocks:
              type: boolean

output:

package io.github.ghostbuster91.sttp.client3.example
import _root_.sttp.client3._
import _root_.sttp.model._
import _root_.io.circe.Decoder
import _root_.io.circe.Encoder
import _root_.io.circe.HCursor
import _root_.io.circe.DecodingFailure
import _root_.io.circe.Decoder.Result
import _root_.sttp.client3.circe.SttpCirceApi
trait CirceCodecs extends SttpCirceApi {
  implicit lazy val dogDecoder: Decoder[Dog] =
    Decoder.forProduct1("bark")(Dog.apply)
  implicit lazy val dogEncoder: Encoder[Dog] =
    Encoder.forProduct2("petType", "bark")(p => ("dog", p.bark))
  implicit lazy val petDecoder: Decoder[Pet] = new Decoder[Pet]() {
    override def apply(c: HCursor): Result[Pet] = c
      .downField("petType")
      .as[String]
      .flatMap {
        case "Lizard" =>
          Decoder[Lizard].apply(c)
        case "dog" =>
          Decoder[Dog].apply(c)
        case other =>
          Left(DecodingFailure("Unexpected value for coproduct:" + other, Nil))
      }
  }
  implicit lazy val petEncoder: Encoder[Pet] = Encoder.instance {
    case dog: Dog =>
      Encoder[Dog].apply(dog)
    case lizard: Lizard =>
      Encoder[Lizard].apply(lizard)
  }
}
object CirceCodecs extends CirceCodecs
sealed trait Pet
case class Dog(bark: Option[String]) extends Pet()
class DefaultApi(baseUrl: String, circeCodecs: CirceCodecs = CirceCodecs) {
  import circeCodecs._
  def getDog(): Request[Dog, Any] = basicRequest
    .get(uri"$baseUrl")
    .response(
      fromMetadata(
        asJson[Dog].getRight,
        ConditionalResponseAs(
          _.code == StatusCode.unsafeApply(200),
          asJson[Dog].getRight
        )
      )
    )
}

commit hash: 90c99d6

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions