softwaremill / softwaremill/tapir

[ENHANCEMENT] Setting names on Schema.oneOfWrapped schemas

Open
#2,845 8 comments 2 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: 1.2.11
Scala version: 2.13.10

We have been using Schema.oneOfWrapped for endpoints that take a variety of inputs or generate more than one type of output. This has worked well, but the one issue has been the redocly OpenAPI docs just show 'Object' for each of the options, and there's no drop down to pick between the different types on the examples.

image

If we provide names for the generated schemas, the docs all work perfectly...

image

To generate the names in the screenshot above I modified the macro here
https://github.com/softwaremill/tapir/blob/master/core/src/main/scala-2/sttp/tapir/internal/OneOfMacro.scala#L90-L92

to the following...

      val subclassesSchemas = subclasses.map(
        subclass =>
          q"${subclass.name.encodedName.toString} -> Schema.wrapWithSingleFieldProduct(implicitly[Schema[${subclass.asType}]])($conf).name(SName($conf.toEncodedName($conf.toDiscriminatorValue(implicitly[Schema[${subclass.asType}]].name.getOrElse(SName.Unit)))))"
      )

This sets the schema name to the encoded name from the provided configuration. To generate the version in the screenshot I provided an instance that splits up the words with a space

  implicit val config: Configuration = Configuration.default.copy(toEncodedName = _.replaceAll("([a-z])([A-Z]+)", "$1 $2"))

I'm not sure if I'm using the Configuration as you intended, or if adding the names by default has other issues, but this patch has been very useful for us and it would be great if a variation of it was included with Tapir.

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 core/src/main/scala-2/sttp/tapir/internal/OneOfMacro.scala around the referenced schema construction, then review how Configuration supplies encoded names and discriminator values. Compare the current generated schemas with the proposed names in Redocly OpenAPI documentation; done means oneOfWrapped options have distinct usable names without breaking existing schema generation.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.