Skip to content

Add an encoded discriminator value attribute for coproducts, use it to render const constraints#3955

Merged
adamw merged 6 commits into
masterfrom
discriminator-value-as-const
Jul 26, 2024
Merged

Add an encoded discriminator value attribute for coproducts, use it to render const constraints#3955
adamw merged 6 commits into
masterfrom
discriminator-value-as-const

Conversation

@adamw

@adamw adamw commented Jul 25, 2024

Copy link
Copy Markdown
Member

An alternative approach to #3763

cc @kamilkloch @zorba128 @seveneves

Closes #3909

val Attribute: AttributeKey[Tuple] = new AttributeKey[Tuple]("sttp.tapir.Schema.Tuple")
}

/** For coproduct schemas, when there's a discriminator field, used to attach the encoded value of the discriminator field. Such value is

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coproduct may be also rendererd without a discriminator field (Json object nesting). We need to make sure this case is covered as well.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this applies only to discriminators. I think if there's no discriminator, then we don't have to do anything, that is we shouldn't add any constraints?

@adamw

adamw commented Jul 25, 2024

Copy link
Copy Markdown
Member Author

You can see how this is rendered in the OpenAPI yamls in the tests - let me know if that's what you were after

@kamilkloch

Copy link
Copy Markdown
Contributor

Thank you, will do!

@seveneves

Copy link
Copy Markdown

@adamw thank you!

I will test this with my setup

@zorba128

zorba128 commented Jul 26, 2024

Copy link
Copy Markdown
Contributor

Please take a look at test that illustrates issue with async api:

class VerifyAsyncAPIYamlTest extends AnyFunSuite with Matchers {
  // ...
 
  /** TODO: fixing this requires custom rendering of ASchema (no discriminator mapping, just the discriminator field name).
   * It seems best place to fix it is at `sttp.apispec.asyncapi.circe` - injecting custom `discriminator` encoder for ASchema.
   * 
   * Current output:
   * ```
   * Animal:
   *   title: Animal
   *   oneOf:
   *   - $ref: '#/components/schemas/Cat'
   *   - $ref: '#/components/schemas/Dog'
   *   discriminator:
   *     propertyName: pet
   *     mapping:
   *       Cat: '#/components/schemas/Cat'
   *       Dog: '#/components/schemas/Dog'
   * ```
   * Desired:
   * ```
   * Animal:
   *   title: Animal
   *   oneOf:
   *   - $ref: '#/components/schemas/Cat'
   *   - $ref: '#/components/schemas/Dog'
   *   discriminator: pet
   * ```
   */
  test("should render coproduct using discriminator correctly") {
    case class GetAnimal(name: String)
    sealed trait Animal
    case class Cat(name: String) extends Animal
    case class Dog(name: String, breed: String) extends Animal
    implicit val configuration: sttp.tapir.generic.Configuration = sttp.tapir.generic.Configuration.default.withDiscriminator("pet")

    val animalEndpoint = endpoint.get
      .in("animals")
      .out(webSocketBody[GetAnimal, CodecFormat.Json, Animal, CodecFormat.Json](AkkaStreams))
        // .responsesExamples(List(Cat("Tom"), Dog("Spike", "Bulldog")))

    val yaml = AsyncAPIInterpreter().toAsyncAPI(animalEndpoint, "discriminator mapping", "1.0").toYaml
    println(yaml)

    noIndentation(yaml) shouldBe loadYaml("expected_coproduct_with_discriminator.yml")
  }
}

@zorba128

Copy link
Copy Markdown
Contributor

See softwaremill/sttp-apispec#178

@adamw

adamw commented Jul 26, 2024

Copy link
Copy Markdown
Member Author

@zorba128 I've added your test, take a look

@zorba128

Copy link
Copy Markdown
Contributor

Yay :)

image

@kamilkloch

Copy link
Copy Markdown
Contributor

Looks good, thank you @adamw @zorba128 !

@adamw adamw merged commit f2c22af into master Jul 26, 2024
@adamw adamw deleted the discriminator-value-as-const branch July 26, 2024 13:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Validate discriminator values

4 participants