guardrail-dev / guardrail-dev/guardrail

Traits with discriminator are not sealed

Open
#447 4 comments 0 reactions 0 assignees View on GitHub
bug core scala
Dominant language
Scala
Stars
541
Forks
138
PR merge metrics
No merged PRs in 30d

Description

Hello !

When using the following schema :
```yaml
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 :
```scala
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 _ => `

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.