Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -77,13 +77,13 @@ class OpenAPIModelRegistration(
else
tpe.dealias match {
case t if tpe.typeSymbol.isClass && tpe.typeSymbol.asClass.isCaseClass => handleCaseType(t)
case t if t <:< typeOf[Map[_, _]] => handleMap(t)
case t if t <:< typeOf[Option[_]] => handleType(t.typeArgs.head)
case t if t <:< typeOf[Seq[_]] || t <:< typeOf[Array[_]] => handleSeqLike(t)
case t if t <:< typeOf[Set[_]] => handleSet(t)
case t if t <:< typeOf[Enumeration#Value] => handleEnum(t)
case t if t.typeSymbol.isClass && t.typeSymbol.asClass.isSealed => handleSealedType(t)
case t => handleSimpleType(t)
case t if t <:< typeOf[Map[_, _]] => handleMap(t.typeArgs(0), t.typeArgs(1))
case t if t <:< typeOf[Option[_]] => handleType(t.typeArgs.head)
case t if t <:< typeOf[Seq[_]] || t <:< typeOf[Array[_]] => handleSeqLike(t)
case t if t <:< typeOf[Set[_]] => handleSet(t)
case t if t <:< typeOf[Enumeration#Value] => handleEnum(t)
case t if t.typeSymbol.isClass && t.typeSymbol.asClass.isSealed => handleSealedType(t)
case t => handleSimpleType(t)
}
}

Expand Down Expand Up @@ -112,10 +112,13 @@ class OpenAPIModelRegistration(
registerAsReference(name, schema)
}

private def handleMap(tpe: Type): Schema[_] = {
val schema = new Schema
schema.setType("object")
schema
private def handleMap(keyType: Type, valueType: Type): Schema[_] = keyType match {
case _ if keyType <:< typeOf[String] =>
val schema = new Schema
schema.setType("object")
schema.setAdditionalProperties(handleType(valueType))
schema
case _ => throw new IllegalArgumentException("In OpenAPI 3.0.x Map must have String key type.")
}

private def handleSeqLike(tpe: Type): Schema[_] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,12 @@ class OpenAPIModelRegistrationSpec extends AnyFlatSpec {

private case class Maps(
a: Map[String, Int],
b: Map[Int, Arrays]
b: Map[String, Arrays]
)

private case class WrongMaps(
a: Map[Int, Int],
b: Map[Boolean, Arrays]
)

private object Things extends Enumeration {
Expand Down Expand Up @@ -262,7 +267,7 @@ class OpenAPIModelRegistrationSpec extends AnyFlatSpec {
assertIsArrayOfExpectedType(actualSchemas, "Arrays.h", "integer")
}

it should "make Map an OpenAPI object type" in {
it should "make Map an OpenAPI object type with value schema as additionalProperties" in {
val components = new Components
val openAPIModelRegistration = new OpenAPIModelRegistration(components)

Expand All @@ -271,7 +276,26 @@ class OpenAPIModelRegistrationSpec extends AnyFlatSpec {
val actualSchemas = components.getSchemas

assertTypeAndFormatAreAsExpected(actualSchemas, "Maps.a", "object")
assertAdditionalPropertiesAreAsExpected(
actualSchemas,
"Maps.a",
(new Schema).`type`("integer").format("int32")
)
assertTypeAndFormatAreAsExpected(actualSchemas, "Maps.b", "object")
assertAdditionalPropertiesAreAsExpected(
actualSchemas,
"Maps.b",
(new Schema).$ref("#/components/schemas/Arrays")
)
}

it should "throw IllegalArgumentException if Map has key type different than String" in {
val components = new Components
val openAPIModelRegistration = new OpenAPIModelRegistration(components)

intercept[IllegalArgumentException] {
openAPIModelRegistration.register[WrongMaps]()
}
}

it should "make simple Enumeration (without renaming) an OpenAPI enum" in {
Expand Down Expand Up @@ -805,6 +829,16 @@ class OpenAPIModelRegistrationSpec extends AnyFlatSpec {
schema => schema.getType === expectedType && Option(schema.getFormat) === expectedFormat
)

private def assertAdditionalPropertiesAreAsExpected(
actualSchemas: java.util.Map[String, Schema[_]],
fieldPath: String,
expectedAdditionalProperties: AnyRef
): scalatest.Assertion = assertPredicateForPath(
actualSchemas,
fieldPath,
schema => Option(schema.getAdditionalProperties).map(_ === expectedAdditionalProperties).getOrElse(false)
)

private def assertRefIsAsExpected(
actualSchemas: java.util.Map[String, Schema[_]],
fieldPath: String,
Expand Down