-
Notifications
You must be signed in to change notification settings - Fork 426
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2376 from softwaremill/docs-class-used-directly-a…
…nd-in-coproduct Properly support classes which are both used directly and as a member of a coproduct
- Loading branch information
Showing
17 changed files
with
273 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 0 additions & 16 deletions
16
docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/NameToSchemaReference.scala
This file was deleted.
Oops, something went wrong.
22 changes: 22 additions & 0 deletions
22
docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/SchemaKey.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package sttp.tapir.docs.apispec.schema | ||
|
||
import sttp.tapir.{SchemaType, Schema => TSchema} | ||
|
||
/** A schema key consists of both the name, and the schema's fields, in case this is a product. This is needed as the same schema name can | ||
* have two different sets of fields, in case the class is a member of an inheritance hierarchy, and a discriminator field is used (#2358). | ||
*/ | ||
private[docs] case class SchemaKey(name: TSchema.SName, fields: Set[String]) | ||
|
||
private[docs] object SchemaKey { | ||
def apply(schema: TSchema[_]): Option[SchemaKey] = schema.name.map(apply(schema, _)) | ||
|
||
def apply(schema: TSchema[_], name: TSchema.SName): SchemaKey = { | ||
val fields = schema.schemaType match { | ||
case SchemaType.SProduct(fields) => fields.map(_.name.name).toSet | ||
case SchemaType.SOpenProduct(fields, _) => fields.map(_.name.name).toSet | ||
case _ => Set.empty[String] | ||
} | ||
|
||
SchemaKey(name, fields) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
docs/apispec-docs/src/main/scala/sttp/tapir/docs/apispec/schema/ToSchemaReference.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package sttp.tapir.docs.apispec.schema | ||
|
||
import sttp.apispec.Reference | ||
import sttp.tapir.{Schema => TSchema} | ||
|
||
private[schema] class ToSchemaReference(keyToId: Map[SchemaKey, SchemaId]) { | ||
|
||
def map(key: SchemaKey): Reference = map(key.name, keyToId.get(key)) | ||
|
||
/** When mapping a reference used directly (which contains a name only), in case there are multiple schema keys with that name, we use the | ||
* one with a smaller number of fields - as these duplicates must be because one is a member of an inheritance hierarchy; then, we choose | ||
* the variant without the extra discriminator field (#2358). | ||
* | ||
* When mapping a referenced used in a discriminator, we choose the variant with the higher number of fields in [[mapDiscriminator]]. | ||
*/ | ||
def mapDirect(name: TSchema.SName): Reference = | ||
map(name, keyToId.filter(_._1.name == name).toList.sortBy(_._1.fields.size).headOption.map(_._2)) | ||
|
||
def mapDiscriminator(name: TSchema.SName): Reference = | ||
map(name, keyToId.filter(_._1.name == name).toList.sortBy(-_._1.fields.size).headOption.map(_._2)) | ||
|
||
private def map(name: TSchema.SName, maybeId: Option[SchemaId]): Reference = maybeId match { | ||
case Some(id) => | ||
Reference.to("#/components/schemas/", id) | ||
case None => | ||
// no reference to internal model found. assuming external reference | ||
Reference(name.fullName) | ||
} | ||
} |
Oops, something went wrong.