guardrail icon indicating copy to clipboard operation
guardrail copied to clipboard

Traits with discriminator are not sealed

Open gaspb opened this issue 6 years ago • 4 comments

Hello !

When using the following schema :

  Strategy:
    required:
      - type
    discriminator: type
    properties:
      type:
        type: string
  SomeStrategy:
    allOf:
      - $ref: '#/definitions/Strategy'
      - required:
          - type
          - description
        properties:
          type:
            type: string
            enum:
              - SomeStrategy
          description:
            type: string
  AnotherStrategy:
    allOf:
      - $ref: '#/definitions/Strategy'
      - required:
          - type
          - description
        properties:
          type:
            type: string
            enum:
              - AnotherStrategy
          description:
            type: string

I get the generated code :

trait Strategy
object Strategy {
  val discriminator: String = "type"
  implicit val encoder: Encoder[Strategy] = Encoder.instance({
    case e: SomeStrategy =>
      e.asJsonObject.add(discriminator, "SomeStrategy".asJson).asJson
    case e: AnotherStrategy =>
      e.asJsonObject.add(discriminator, "AnotherStrategy".asJson).asJson
  })

which is incorrect, the trait should be sealed or the pattern matching should include case _ =>

gaspb avatar Nov 12 '19 16:11 gaspb

@gaspb Thanks for the report, this is definitely not great. I think sealed was removed to support allOf.

The work required to re-introduce the sealed constraint would likely involve a mapping function that describes file membership as well as definition order.

Is this something you would be interested in helping with? I can provide some support on getting started, if so.

blast-hardcheese avatar Nov 14 '19 17:11 blast-hardcheese

@blast-hardcheese I would, i'll have a look this weekend :)

gaspb avatar Nov 15 '19 16:11 gaspb

I've just bumped into this as well.. :(

francescopellegrini avatar Apr 19 '20 15:04 francescopellegrini

@francescopellegrini Sorry to hear it 😞 Is this something you'd be able to spend some time investigating? I'm nearly done converting the codebase from Free to Tagless Final, I've got a branch that shouldn't change too much over the next week or so if so, blast-hardcheese/tagless-final-pt4

blast-hardcheese avatar Apr 19 '20 16:04 blast-hardcheese