Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 6 commits into from
Jul 26, 2024

Conversation

adamw
Copy link
Member

@adamw adamw commented Jul 25, 2024

An alternative approach to #3763

cc @kamilkloch @zorba128 @seveneves

Closes #3909

@@ -361,6 +361,16 @@ object Schema extends LowPrioritySchema with SchemaCompanionMacros {
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
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
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
Copy link
Member Author

adamw commented Jul 25, 2024

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
Contributor

Thank you, will do!

@seveneves
Copy link

@adamw thank you!

I will test this with my setup

@zorba128
Copy link
Contributor

zorba128 commented Jul 26, 2024

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
Contributor

See softwaremill/sttp-apispec#178

@adamw
Copy link
Member Author

adamw commented Jul 26, 2024

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

@zorba128
Copy link
Contributor

Yay :)

image

@kamilkloch
Copy link
Contributor

Looks good, thank you @adamw @zorba128 !

@adamw adamw merged commit f2c22af into master Jul 26, 2024
27 checks passed
@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