File tree 5 files changed +47
-8
lines changed
main/scala/com/timeout/docless/schema
test/scala/com/timeout/docless/schema
5 files changed +47
-8
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,7 @@ organization := "com.timeout"
3
3
4
4
name := " docless"
5
5
6
- version := " 0.3 .0"
6
+ version := " 0.4 .0"
7
7
8
8
val circeVersion = " 0.6.1"
9
9
val enumeratumVersion = " 1.5.1"
Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ trait JsonSchema[A] extends JsonSchema.HasRef {
34
34
35
35
def asJsonRef : Json = asObjectRef.asJson
36
36
37
- def namedDefinition (fieldName : String ): NamedDefinition =
37
+ def NamedDefinition (fieldName : String ): NamedDefinition =
38
38
JsonSchema .NamedDefinition (
39
39
id,
40
40
fieldName,
@@ -125,6 +125,16 @@ object JsonSchema
125
125
override def relatedDefinitions = Set .empty
126
126
}
127
127
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
+
128
138
def instanceAndRelated [A ](
129
139
pair : => (JsonObject , Set [Definition ])
130
140
)(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
2
2
3
3
import java .time .{LocalDate , LocalDateTime }
4
4
5
- import com .timeout .docless .schema .JsonSchema .{ inlineInstance , PatternProperty }
5
+ import com .timeout .docless .schema .JsonSchema ._
6
6
import io .circe ._
7
7
import io .circe .syntax ._
8
+ import scala .reflect .runtime .{universe => ru }
8
9
9
10
trait Primitives {
10
11
implicit val boolSchema : JsonSchema [Boolean ] =
@@ -81,8 +82,9 @@ trait Primitives {
81
82
)
82
83
}
83
84
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)
86
88
87
89
implicit def mapSchema [K , V ](implicit kPattern : PatternProperty [K ],
88
90
vSchema : JsonSchema [V ]): JsonSchema [Map [K , V ]] =
Original file line number Diff line number Diff line change @@ -26,7 +26,7 @@ trait HListInstances {
26
26
hSchema.asJson -> tSchema.relatedDefinitions
27
27
else
28
28
hSchema.asJsonRef -> (tSchema.relatedDefinitions + hSchema
29
- .namedDefinition (fieldName))
29
+ .NamedDefinition (fieldName))
30
30
31
31
val hField = fieldName -> hValue
32
32
val tFields = tSchema.jsonObject.toList
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ object JsonSchemaTest {
25
25
}
26
26
27
27
case class Nested (name : String , foo : Foo )
28
+ case class NestedOpt (name : String , fooOpt : Option [Foo ])
28
29
29
30
case class X (e : E , f : F )
30
31
@@ -111,7 +112,33 @@ class JsonSchemaTest extends FreeSpec {
111
112
""" .stripMargin) should === (Right (schema.asJson))
112
113
113
114
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" )))
115
142
}
116
143
117
144
" with types extending enumeratum.EnumEntry" - {
@@ -186,7 +213,7 @@ class JsonSchemaTest extends FreeSpec {
186
213
aSchema.definition,
187
214
bSchema.definition,
188
215
cSchema.definition,
189
- fooSchema.namedDefinition (" foo" )
216
+ fooSchema.NamedDefinition (" foo" )
190
217
)
191
218
)
192
219
}
You can’t perform that action at this time.
0 commit comments