softwaremill / softwaremill/tapir

Automatic OpenAPI type renaming can break discriminators

Open
#886 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Scala
Stars
1.5k
Forks
468
Avg merge
5h 37m
Merged PRs (30d)
34

Description

Tapir version: 0.17.0-M11

Scala version: 2.13.4

Describe the bug

Type name collisions in OpenAPI are handled by renaming types. Unfortunately, the default discriminator behavior is to use type names in the schema as values. This means that the actual values of the discriminator are difficult to predict, and may differ from what Circe uses

How to reproduce?

See the full Scastie here.

Let's say we have a setup like this:

implicit val tapirConfig: Configuration = Configuration.default.withDiscriminator("__tag")
implicit val circeConfig: CirceConfiguration = CirceConfiguration.default.withDiscriminator("__tag")

sealed trait Foo

object Foo {
  final case class Bar(x: Int) extends Foo
}

final case class Bar()

val e = endpoint.get.in("foo").in(jsonBody[Bar]).out(jsonBody[Foo])

An example of Foo JSON generated by Circe:

{
  "x" : 123,
  "__tag" : "Bar"
}

The corresponding OpenAPI type:

Foo:
  oneOf:
  - $ref: '#/components/schemas/Bar1'
  discriminator:
    propertyName: __tag

Note that the implicit discriminator value (Bar1) does not match the actual JSON (Bar).

Additional information

This can be worked around using discriminatorMappingOverride in SchemaType, but it is painful to say the least.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the linked Scastie reproduction and inspect the discriminator configuration and SchemaType.discriminatorMappingOverride behavior. Compare the generated OpenAPI schema with the Circe JSON discriminator value; done means automatic type renaming no longer produces a mismatched discriminator value in this case.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, scala
Domain
api
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.