Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ trait SchemaMagnoliaDerivation {
val schemaName = subtypeNameToSchemaName(s)
genericDerivationConfig.toDiscriminatorValue(schemaName) -> SRef(schemaName)
}.toMap
// TODO
baseCoproduct.addDiscriminatorField(FieldName(d), discriminatorMapping = discriminatorMapping)
case None => baseCoproduct
}
Expand Down
10 changes: 10 additions & 0 deletions core/src/main/scala/sttp/tapir/Schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
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?

* added to the discriminator field schemas in each of the coproduct's subtypes. When rendering OpenAPI/JSON schema, these values are
* converted to `const` constraints on fields.
*/
case class EncodedDiscriminatorValue(v: String)
object EncodedDiscriminatorValue {
val Attribute: AttributeKey[EncodedDiscriminatorValue] =
new AttributeKey[EncodedDiscriminatorValue]("sttp.tapir.Schema.EncodedDiscriminatorValue")
}

/** @param typeParameterShortNames
* full name of type parameters, name is legacy and kept only for backward compatibility
*/
Expand Down
34 changes: 31 additions & 3 deletions core/src/main/scala/sttp/tapir/SchemaType.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,39 @@ object SchemaType {
discriminatorSchema: Schema[D] = Schema.string,
discriminatorMapping: Map[String, SRef[_]] = Map.empty
): SCoproduct[T] = {
// used to add encoded discriminator value attributes
val reverseDiscriminatorByNameMapping: Map[SName, String] = discriminatorMapping.toList.map { case (v, ref) => (ref.name, v) }.toMap

SCoproduct(
subtypes.map {
case s @ Schema(st: SchemaType.SProduct[Any @unchecked], _, _, _, _, _, _, _, _, _, _)
if st.fields.forall(_.name != discriminatorName) =>
s.copy(schemaType = st.copy(fields = st.fields :+ SProductField[Any, D](discriminatorName, discriminatorSchema, _ => None)))
case s @ Schema(st: SchemaType.SProduct[Any @unchecked], _, _, _, _, _, _, _, _, _, _) =>
// first, ensuring that the discriminator field is added to the schema type - it might already be present
var targetSt =
if (st.fields.forall(_.name != discriminatorName))
st.copy(fields = st.fields :+ SProductField[Any, D](discriminatorName, discriminatorSchema, _ => None))
else st

// next, modifying the discriminator field, by adding the value attribute (if a value can be found)
targetSt = targetSt.copy(fields = targetSt.fields.map { field =>
if (field.name == discriminatorName) {
val discriminatorValue = s.name.flatMap { subtypeName =>
reverseDiscriminatorByNameMapping.get(subtypeName)
}

discriminatorValue match {
case Some(v) =>
SProductField(
field.name,
field.schema.attribute(Schema.EncodedDiscriminatorValue.Attribute, Schema.EncodedDiscriminatorValue(v)),
field.get
)
case None => field
}

} else field
})

s.copy(schemaType = targetSt)
case s => s
},
Some(SDiscriminator(discriminatorName, discriminatorMapping))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,13 @@ private[docs] class TSchemaToASchema(
// The primary motivation for using schema name as fallback title is to improve Swagger UX with
// `oneOf` schemas in OpenAPI 3.1. See https://github.com/softwaremill/tapir/issues/3447 for details.
def fallbackTitle = tschema.name.map(fallbackSchemaTitle)

val const = tschema.attribute(TSchema.EncodedDiscriminatorValue.Attribute).map(_.v).map(v => ExampleSingleValue(v))

oschema
.copy(title = titleFromAttr orElse fallbackTitle)
.copy(title = titleFromAttr.orElse(fallbackTitle))
.copy(uniqueItems = tschema.attribute(UniqueItems.Attribute).map(_.uniqueItems))
.copy(const = const)
}

private def addMetadata(oschema: ASchema, tschema: TSchema[_]): ASchema = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ components:
properties:
name:
type: string
const: sml
Person:
title: Person
type: object
Expand All @@ -42,6 +43,7 @@ components:
properties:
name:
type: string
const: john
age:
type: integer
format: int32
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ components:
properties:
name:
type: string
const: sml
Person:
title: Person
type: object
Expand All @@ -50,6 +51,7 @@ components:
properties:
name:
type: string
const: john
age:
type: integer
format: int32
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ components:
- red
shapeType:
type: string
const: Square
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ components:
type: string
kind:
type: string
const: organization
Person:
title: Person
type: object
Expand All @@ -100,4 +101,5 @@ components:
type: integer
format: int32
kind:
type: string
type: string
const: person