File tree Expand file tree Collapse file tree
main/scala/com/timeout/docless/schema
test/scala/com/timeout/docless/schema Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ organization := "com.timeout"
33
44name := " docless"
55
6- version := " 0.3 .0"
6+ version := " 0.4 .0"
77
88val circeVersion = " 0.6.1"
99val enumeratumVersion = " 1.5.1"
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ trait JsonSchema[A] extends JsonSchema.HasRef {
3434
3535 def asJsonRef : Json = asObjectRef.asJson
3636
37- def namedDefinition (fieldName : String ): NamedDefinition =
37+ def NamedDefinition (fieldName : String ): NamedDefinition =
3838 JsonSchema .NamedDefinition (
3939 id,
4040 fieldName,
@@ -125,6 +125,16 @@ object JsonSchema
125125 override def relatedDefinitions = Set .empty
126126 }
127127
128+ def functorInstance [F [_],A ](
129+ obj : => JsonObject
130+ )(implicit tag : ru.WeakTypeTag [A ]): JsonSchema [F [A ]] =
131+ new JsonSchema [F [A ]] {
132+ override def id = tag.tpe.typeSymbol.fullName
133+ override def inline = false
134+ override def jsonObject = obj
135+ override def relatedDefinitions = Set .empty
136+ }
137+
128138 def instanceAndRelated [A ](
129139 pair : => (JsonObject , Set [Definition ])
130140 )(implicit tag : ru.WeakTypeTag [A ]): JsonSchema [A ] = new JsonSchema [A ] {
Original file line number Diff line number Diff line change @@ -2,9 +2,10 @@ package com.timeout.docless.schema
22
33import java .time .{LocalDate , LocalDateTime }
44
5- import com .timeout .docless .schema .JsonSchema .{ inlineInstance , PatternProperty }
5+ import com .timeout .docless .schema .JsonSchema ._
66import io .circe ._
77import io .circe .syntax ._
8+ import scala .reflect .runtime .{universe => ru }
89
910trait Primitives {
1011 implicit val boolSchema : JsonSchema [Boolean ] =
@@ -81,8 +82,9 @@ trait Primitives {
8182 )
8283 }
8384
84- implicit def optSchema [A : JsonSchema ]: JsonSchema [Option [A ]] =
85- inlineInstance[Option [A ]](implicitly[JsonSchema [A ]].jsonObject)
85+ implicit def optSchema [A ](implicit ev : JsonSchema [A ], tag : ru.WeakTypeTag [A ]): JsonSchema [Option [A ]] =
86+ if (ev.inline) inlineInstance[Option [A ]](ev.jsonObject)
87+ else functorInstance[Option , A ](ev.jsonObject)(tag)
8688
8789 implicit def mapSchema [K , V ](implicit kPattern : PatternProperty [K ],
8890 vSchema : JsonSchema [V ]): JsonSchema [Map [K , V ]] =
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ trait HListInstances {
2626 hSchema.asJson -> tSchema.relatedDefinitions
2727 else
2828 hSchema.asJsonRef -> (tSchema.relatedDefinitions + hSchema
29- .namedDefinition (fieldName))
29+ .NamedDefinition (fieldName))
3030
3131 val hField = fieldName -> hValue
3232 val tFields = tSchema.jsonObject.toList
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ object JsonSchemaTest {
2525 }
2626
2727 case class Nested (name : String , foo : Foo )
28+ case class NestedOpt (name : String , fooOpt : Option [Foo ])
2829
2930 case class X (e : E , f : F )
3031
@@ -111,7 +112,33 @@ class JsonSchemaTest extends FreeSpec {
111112 """ .stripMargin) should === (Right (schema.asJson))
112113
113114 schema.id should === (id[Nested ])
114- schema.relatedDefinitions should === (Set (fs.namedDefinition(" foo" )))
115+ schema.relatedDefinitions should === (Set (fs.NamedDefinition (" foo" )))
116+ }
117+
118+ " handles nested case classes within options" in {
119+ implicit val fs : JsonSchema [Foo ] = fooSchema
120+
121+ val schema = JsonSchema .deriveFor[NestedOpt ]
122+ parser.parse(s """
123+ |{
124+ | "type": "object",
125+ | "required" : [
126+ | "name"
127+ | ],
128+ | "properties" : {
129+ | "name" : {
130+ | "type" : "string"
131+ | },
132+ | "fooOpt" : {
133+ | " $ref" : "#/definitions/ ${id[Foo ]}"
134+ | }
135+ | }
136+ |}
137+ |
138+ """ .stripMargin) should === (Right (schema.asJson))
139+
140+ schema.id should === (id[NestedOpt ])
141+ schema.relatedDefinitions should === (Set (fs.NamedDefinition (" fooOpt" )))
115142 }
116143
117144 " with types extending enumeratum.EnumEntry" - {
@@ -186,7 +213,7 @@ class JsonSchemaTest extends FreeSpec {
186213 aSchema.definition,
187214 bSchema.definition,
188215 cSchema.definition,
189- fooSchema.namedDefinition (" foo" )
216+ fooSchema.NamedDefinition (" foo" )
190217 )
191218 )
192219 }
You can’t perform that action at this time.
0 commit comments