Skip to content

Commit

Permalink
Do not always inline option schemas
Browse files Browse the repository at this point in the history
Also bump version to 0.0.4
  • Loading branch information
Andrea Fiore committed Feb 10, 2017
1 parent cd67802 commit 48e4fea
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ organization := "com.timeout"

name := "docless"

version := "0.3.0"
version := "0.4.0"

val circeVersion = "0.6.1"
val enumeratumVersion = "1.5.1"
Expand Down
12 changes: 11 additions & 1 deletion src/main/scala/com/timeout/docless/schema/JsonSchema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ trait JsonSchema[A] extends JsonSchema.HasRef {

def asJsonRef: Json = asObjectRef.asJson

def namedDefinition(fieldName: String): NamedDefinition =
def NamedDefinition(fieldName: String): NamedDefinition =
JsonSchema.NamedDefinition(
id,
fieldName,
Expand Down Expand Up @@ -125,6 +125,16 @@ object JsonSchema
override def relatedDefinitions = Set.empty
}

def functorInstance[F[_],A](
obj: => JsonObject
)(implicit tag: ru.WeakTypeTag[A]): JsonSchema[F[A]] =
new JsonSchema[F[A]] {
override def id = tag.tpe.typeSymbol.fullName
override def inline = false
override def jsonObject = obj
override def relatedDefinitions = Set.empty
}

def instanceAndRelated[A](
pair: => (JsonObject, Set[Definition])
)(implicit tag: ru.WeakTypeTag[A]): JsonSchema[A] = new JsonSchema[A] {
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/com/timeout/docless/schema/Primitives.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package com.timeout.docless.schema

import java.time.{LocalDate, LocalDateTime}

import com.timeout.docless.schema.JsonSchema.{inlineInstance, PatternProperty}
import com.timeout.docless.schema.JsonSchema._
import io.circe._
import io.circe.syntax._
import scala.reflect.runtime.{universe => ru}

trait Primitives {
implicit val boolSchema: JsonSchema[Boolean] =
Expand Down Expand Up @@ -81,8 +82,9 @@ trait Primitives {
)
}

implicit def optSchema[A: JsonSchema]: JsonSchema[Option[A]] =
inlineInstance[Option[A]](implicitly[JsonSchema[A]].jsonObject)
implicit def optSchema[A](implicit ev: JsonSchema[A], tag: ru.WeakTypeTag[A]): JsonSchema[Option[A]] =
if (ev.inline) inlineInstance[Option[A]](ev.jsonObject)
else functorInstance[Option, A](ev.jsonObject)(tag)

implicit def mapSchema[K, V](implicit kPattern: PatternProperty[K],
vSchema: JsonSchema[V]): JsonSchema[Map[K, V]] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ trait HListInstances {
hSchema.asJson -> tSchema.relatedDefinitions
else
hSchema.asJsonRef -> (tSchema.relatedDefinitions + hSchema
.namedDefinition(fieldName))
.NamedDefinition(fieldName))

val hField = fieldName -> hValue
val tFields = tSchema.jsonObject.toList
Expand Down
31 changes: 29 additions & 2 deletions src/test/scala/com/timeout/docless/schema/JsonSchemaTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ object JsonSchemaTest {
}

case class Nested(name: String, foo: Foo)
case class NestedOpt(name: String, fooOpt: Option[Foo])

case class X(e: E, f: F)

Expand Down Expand Up @@ -111,7 +112,33 @@ class JsonSchemaTest extends FreeSpec {
""".stripMargin) should ===(Right(schema.asJson))

schema.id should ===(id[Nested])
schema.relatedDefinitions should ===(Set(fs.namedDefinition("foo")))
schema.relatedDefinitions should ===(Set(fs.NamedDefinition("foo")))
}

"handles nested case classes within options" in {
implicit val fs: JsonSchema[Foo] = fooSchema

val schema = JsonSchema.deriveFor[NestedOpt]
parser.parse(s"""
|{
| "type": "object",
| "required" : [
| "name"
| ],
| "properties" : {
| "name" : {
| "type" : "string"
| },
| "fooOpt" : {
| "$ref" : "#/definitions/${id[Foo]}"
| }
| }
|}
|
""".stripMargin) should ===(Right(schema.asJson))

schema.id should ===(id[NestedOpt])
schema.relatedDefinitions should ===(Set(fs.NamedDefinition("fooOpt")))
}

"with types extending enumeratum.EnumEntry" - {
Expand Down Expand Up @@ -186,7 +213,7 @@ class JsonSchemaTest extends FreeSpec {
aSchema.definition,
bSchema.definition,
cSchema.definition,
fooSchema.namedDefinition("foo")
fooSchema.NamedDefinition("foo")
)
)
}
Expand Down

0 comments on commit 48e4fea

Please sign in to comment.