-
Notifications
You must be signed in to change notification settings - Fork 12
/
JsonSchemaCirceEncoders.scala
240 lines (206 loc) · 8.87 KB
/
JsonSchemaCirceEncoders.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package sttp.apispec
package internal
import io.circe._
import io.circe.generic.semiauto.deriveEncoder
import io.circe.parser.parse
import io.circe.syntax._
import scala.collection.immutable.ListMap
trait JsonSchemaCirceEncoders {
def anyObjectEncoding: AnySchema.Encoding
def openApi30: Boolean = false
implicit lazy val encoderSchema: Encoder[Schema] = Encoder.AsObject
.instance { (s: Schema) =>
val nullSchema = Schema(`type` = Some(List(SchemaType.Null)))
// Nullable $ref Schema is represented as {"anyOf": [{"$ref": "some-ref"}, {"type": "null"}]}
// In OpenAPI 3.0, we need to translate it to {"allOf": [{"$ref": "some-ref"}], "nullable": true}
val wrappedNullableRef30 = s.anyOf match {
case List(refSchema: Schema, `nullSchema`) if refSchema.$ref.isDefined && openApi30 =>
Some(refSchema)
case _ => None
}
val typeAndNullable = s.`type` match {
case Some(List(tpe)) =>
List("type" := tpe)
case Some(List(tpe, SchemaType.Null)) if openApi30 =>
List("type" := tpe, "nullable" := true)
case None if wrappedNullableRef30.isDefined =>
List("nullable" := true)
case t =>
List("type" := t)
}
val minFields = (s.minimum, s.exclusiveMinimum) match {
case (None, Some(min)) if openApi30 =>
Vector("minimum" := min, "exclusiveMinimum" := true)
case _ =>
Vector("minimum" := s.minimum, "exclusiveMinimum" := s.exclusiveMinimum)
}
val maxFields = (s.maximum, s.exclusiveMaximum) match {
case (None, Some(max)) if openApi30 =>
Vector("maximum" := max, "exclusiveMaximum" := true)
case _ =>
Vector("maximum" := s.maximum, "exclusiveMaximum" := s.exclusiveMaximum)
}
val exampleFields = s.examples match {
case Some(List(example)) if openApi30 =>
Vector("example" := example)
case _ =>
Vector("examples" := s.examples)
}
JsonObject.fromIterable(
Vector(
"$schema" := s.$schema,
"$vocabulary" := s.$vocabulary,
"$id" := s.$id,
"$anchor" := s.$anchor,
"$dynamicAnchor" := s.$dynamicAnchor,
"$ref" := s.$ref,
"$dynamicRef" := s.$dynamicRef,
"$comment" := s.$comment,
"$defs" := s.$defs,
"title" := s.title,
"description" := s.description,
"default" := s.default,
"deprecated" := s.deprecated,
"readOnly" := s.readOnly,
"writeOnly" := s.writeOnly
) ++ exampleFields ++ typeAndNullable ++ Vector(
"enum" := s.`enum`,
"const" := s.const,
"format" := s.format,
"allOf" := wrappedNullableRef30.map(List(_)).getOrElse(s.allOf),
"anyOf" := (if (wrappedNullableRef30.isDefined) Nil else s.anyOf),
"oneOf" := s.oneOf,
"not" := s.not,
"if" := s.`if`,
"then" := s.`then`,
"else" := s.`else`,
"dependentSchemas" := s.dependentSchemas,
"multipleOf" := s.multipleOf
) ++ minFields ++ maxFields ++ Vector(
"maxLength" := s.maxLength,
"minLength" := s.minLength,
"pattern" := s.pattern,
"maxItems" := s.maxItems,
"minItems" := s.minItems,
"uniqueItems" := s.uniqueItems,
"maxContains" := s.maxContains,
"minContains" := s.minContains,
"prefixItems" := s.prefixItems,
"items" := s.items,
"contains" := s.contains,
"unevaluatedItems" := s.unevaluatedItems,
"maxProperties" := s.maxProperties,
"minProperties" := s.minProperties,
"required" := s.required,
"dependentRequired" := s.dependentRequired,
"discriminator" := s.discriminator,
"properties" := s.properties,
"patternProperties" := s.patternProperties,
"additionalProperties" := s.additionalProperties,
"propertyNames" := s.propertyNames,
"unevaluatedProperties" := s.unevaluatedProperties,
"externalDocs" := s.externalDocs,
"extensions" := s.extensions
)
)
}
.mapJsonObject(expandExtensions)
// note: these are strict val-s, order matters!
implicit val extensionValue: Encoder[ExtensionValue] =
Encoder.instance(e => parse(e.value).getOrElse(Json.fromString(e.value)))
implicit val encoderExampleSingleValue: Encoder[ExampleSingleValue] = {
case ExampleSingleValue(value: String) => parse(value).getOrElse(Json.fromString(value))
case ExampleSingleValue(value: Int) => Json.fromInt(value)
case ExampleSingleValue(value: Long) => Json.fromLong(value)
case ExampleSingleValue(value: Float) => Json.fromFloatOrString(value)
case ExampleSingleValue(value: Double) => Json.fromDoubleOrString(value)
case ExampleSingleValue(value: Boolean) => Json.fromBoolean(value)
case ExampleSingleValue(value: BigDecimal) => Json.fromBigDecimal(value)
case ExampleSingleValue(value: BigInt) => Json.fromBigInt(value)
case ExampleSingleValue(null) => Json.Null
case ExampleSingleValue(value) => Json.fromString(value.toString)
}
implicit val encoderMultipleExampleValue: Encoder[ExampleMultipleValue] = { e =>
Json.arr(e.values.map(v => encoderExampleSingleValue(ExampleSingleValue(v))): _*)
}
implicit val encoderExampleValue: Encoder[ExampleValue] = {
case e: ExampleMultipleValue => encoderMultipleExampleValue.apply(e)
case e: ExampleSingleValue => encoderExampleSingleValue.apply(e)
}
implicit val encoderSchemaType: Encoder[SchemaType] =
_.value.asJson
implicit val encoderKeyPattern: KeyEncoder[Pattern] =
KeyEncoder.encodeKeyString.contramap(_.value)
implicit val encoderPattern: Encoder[Pattern] =
Encoder.encodeString.contramap(_.value)
implicit val encoderDiscriminator: Encoder[Discriminator] =
deriveEncoder[Discriminator]
implicit val encoderExternalDocumentation: Encoder[ExternalDocumentation] =
deriveEncoder[ExternalDocumentation].mapJsonObject(expandExtensions)
implicit val encoderAnySchema: Encoder[AnySchema] = Encoder.instance {
case AnySchema.Anything =>
anyObjectEncoding match {
case AnySchema.Encoding.Object => Json.obj()
case AnySchema.Encoding.Boolean => Json.True
}
case AnySchema.Nothing =>
anyObjectEncoding match {
case AnySchema.Encoding.Object =>
Json.obj("not" := Json.obj())
case AnySchema.Encoding.Boolean => Json.False
}
}
implicit val encoderSchemaLike: Encoder[SchemaLike] = Encoder.instance {
case s: AnySchema => encoderAnySchema(s)
case s: Schema => encoderSchema(s)
}
implicit def encodeList[T: Encoder]: Encoder[List[T]] = {
case Nil => Json.Null
case l: List[T] => Json.arr(l.map(i => implicitly[Encoder[T]].apply(i)): _*)
}
implicit def encodeListMap[K: KeyEncoder, V: Encoder]: Encoder[ListMap[K, V]] = doEncodeListMap(nullWhenEmpty = true)
private[apispec] def doEncodeListMap[K: KeyEncoder, V: Encoder](nullWhenEmpty: Boolean): Encoder[ListMap[K, V]] = {
case m: ListMap[K, V] if m.isEmpty && nullWhenEmpty => Json.Null
case m: ListMap[K, V] =>
val properties = m.map { case (k, v) => KeyEncoder[K].apply(k) -> Encoder[V].apply(v) }.toList
Json.obj(properties: _*)
}
/*
Openapi extensions are arbitrary key-value data that could be added to some of models in specifications, such
as `OpenAPI` itself, `License`, `Parameter`, etc.
The key could be any string (that starts with 'x-' by convention) and value is arbitrary Json (string, object,
array, etc.)
To be able to encode such arbitrary data and apply it to the final Json it passed through the `extensions` field
in models and moved (or expanded) to the object level while encoding
Example:
```
case class License(
name: String,
url: Option[String],
extensions: ListMap[String, ExtensionValue] = ListMap.empty
)
val licenseWithExtension = License("hello", None, ListMap("x-foo", ExtensionValue("42"))
```
Applying the transformation below we end up with the following schema in the specification:
```
license:
name: hello
x-foo: 42
```
*/
private[apispec] def expandExtensions(jsonObject: JsonObject): JsonObject = {
val jsonWithoutExt = jsonObject.filterKeys(_ != "extensions")
jsonObject("extensions")
.flatMap(_.asObject)
.map { extObject =>
val allKeys = (jsonWithoutExt.keys ++ extObject.keys).toSeq.distinct
allKeys.foldLeft(JsonObject.empty) { case (acc, key) =>
extObject(key).orElse(jsonWithoutExt(key)) match {
case Some(value) => acc.add(key, value)
case None => acc
}
}
}
.getOrElse(jsonWithoutExt)
}
}