-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathschema.dwl
43 lines (43 loc) · 1.5 KB
/
schema.dwl
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
%dw 2.0
output application/yaml skipNullOn="objects"
import try from dw::Runtime
fun isDate(value: Any): Boolean = try(() -> value as Date).success
fun isDateTime(value: Any): Boolean = try(() -> value as DateTime).success
fun flattenObject(data:Any, result={}) = (
data match {
case is Object -> data mapObject ((value, key) ->
value match {
case is Object -> flattenObject(value, result)
else -> flattenObject(value, result ++ {(key):value})
}
)
case is Array -> flattenObject(data[0]) // only first item from array will be taken
else -> result
}
)
//payload must be an object
var flattenedObject = payload mapObject ((value, key, index) ->
(key): flattenObject(value)
)
---
{
openapi: attributes.queryParams.openapiversion default "3.0.3",
components: {
schemas: flattenedObject mapObject ((mainObj, mainObjKey) ->
(mainObjKey): {
"type": lower(typeOf(mainObj)),
properties: mainObj mapObject ((props, propsKey) ->
(propsKey): {
"type": lower(typeOf(props)),
format: (props match {
case is Number -> null
case d if isDateTime(d) -> "date-time"
case d if isDate(d) -> "date"
else -> null
})
}
)
}
)
}
}