-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanuelle-fikser-for-api.main.kts
267 lines (239 loc) · 8.45 KB
/
manuelle-fikser-for-api.main.kts
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
@file:DependsOn("no.nav.common:yaml:2.2021.12.09_11.56-a71c36a61ba3")
import Manuelle_fikser_for_api_main.ChangeUtils.JsonSource
import Manuelle_fikser_for_api_main.ChangeUtils.YamlSource
import Manuelle_fikser_for_api_main.ChangeUtils.changeFile
import Manuelle_fikser_for_api_main.ChangeUtils.forDefinition
import Manuelle_fikser_for_api_main.ChangeUtils.removeProperty
import Manuelle_fikser_for_api_main.ChangeUtils.setRequired
import com.fasterxml.jackson.core.type.TypeReference
import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory
import no.nav.common.json.JsonMapper
import java.nio.charset.Charset
import java.nio.file.Files
import java.nio.file.Path
changeFile(
from = JsonSource("norg-api/src/main/resources/norg/openapi.json"),
to = YamlSource("norg-api/src/main/resources/norg/openapi-fixed.yaml"),
) {
/**
* NorgApi specen bruker arv og subklasser
* `RsPostboksadresse` og `RsStedsadresse` arver begge fra `RsAdresse` som spesifiserer feltene `postnummer` og `poststed`
*
* For at Jackson skal lage riktig klasse ved deserialisering legges `x-discriminator-value` til.
* Dette feltet er spesifisert på modellen i norg, men er ikke tilgjengelig via openapi-specen.
*
* For å unngå kollisjoner på feltnavn fjernes `postnummer` og `poststed` fra klassene siden disse arves fra `RsAdresse`
*/
forDefinition("RsPostboksadresse") {
put("x-discriminator-value", "postboksadresse")
removeProperty("postnummer", "poststed")
}
forDefinition("RsStedsadresse") {
put("x-discriminator-value", "stedsadresse")
removeProperty("postnummer", "poststed")
}
}
changeFile(
from = JsonSource("sf-henvendelse-api/src/main/resources/sf-henvendelse/openapi.json"),
to = YamlSource("sf-henvendelse-api/src/main/resources/sf-henvendelse/openapi-fixed.yaml"),
) {
/**
* Vi har opplevd at disse kan være null selvom APIet sier de ikke skal være det.
* For å unngå at modia krasjer pga dette gjør vi de optional, og logger tilfellene fra SfHenvendelseService
*/
forDefinition("Henvendelse") {
setRequired("gjeldendeTemagruppe", false)
}
forDefinition("Markering") {
setRequired("markertDato", false)
setRequired("markertAv", false)
}
forDefinition("Journalpost") {
setRequired("journalforerNavIdent", false)
}
forDefinition("Melding") {
setRequired("meldingsId", false)
}
}
/**
* Må ligge i samme fil pga bug med kotlin-scripts sin @file:Import funksjon
* Når det blir løst kan dette flytte ut til egen fil
*/
typealias Json = MutableMap<String, Any>
object ChangeUtils {
val asJson = object : TypeReference<Json>() {}
val charset = Charset.forName("UTF-8")
val yamlParser =
JsonMapper
.applyDefaultConfiguration(ObjectMapper(YAMLFactory()))
.enable(SerializationFeature.INDENT_OUTPUT)
val jsonParser =
JsonMapper
.defaultObjectMapper()
.enable(SerializationFeature.INDENT_OUTPUT)
sealed class Source(
val mapper: ObjectMapper,
val source: String,
)
class JsonSource(
source: String,
) : Source(jsonParser, source)
class YamlSource(
source: String,
) : Source(yamlParser, source)
fun getFileName(file: String) = file.substring(file.lastIndexOf("/") + 1)
fun readFile(file: String): String = Files.readString(Path.of(file), charset)
fun writeFile(
file: String,
content: String,
) = Files.writeString(Path.of(file), content, charset)
fun changeFile(
from: Source,
to: Source,
block: Json.() -> Unit = {},
) {
runCatching {
val content = readFile(from.source)
val json = from.mapper.readValue(content, asJson)
block(json)
val mutatedJson = to.mapper.writeValueAsString(json)
writeFile(to.source, mutatedJson)
}.onSuccess { println("Mutated ${from.source} -> ${to.source}") }
.onFailure { println("Mutation of ${from.source} failed: $it") }
}
inline fun <reified T> Json.getTyped(key: String): T = this[key] as T
fun Json.has(key: String): Boolean = this[key] != null
fun Json.forDefinition(
name: String,
block: Json.() -> Unit,
) {
if (this.has("definitions")) {
block(this.getTyped<Json>("definitions").getTyped(name))
} else if (this.has("components")) {
block(this.getTyped<Json>("components").getTyped<Json>("schemas").getTyped(name))
} else {
throw IllegalStateException("Could not find definitions or compontes")
}
}
fun Json.addDefinition(
name: String,
definition: Any,
) {
assert(!this.has(name)) {
"$name already exist in schema definition"
}
this[name] = definition
}
fun Json.forEndpoint(
method: String,
path: String,
block: Json.() -> Unit,
) {
block(this.getTyped<Json>("paths").getTyped<Json>(path).getTyped(method))
}
fun Json.forProperty(
name: String,
block: Json.() -> Unit,
) {
block(this.getTyped<Json>("properties").getTyped(name))
}
fun Json.renameProperty(
from: String,
to: String,
) {
val properties = this.getTyped<Json>("properties")
properties[to] = checkNotNull(properties[from])
properties.remove(from)
val requiredList = this.getTyped<MutableList<String>?>("required") ?: mutableListOf()
if (requiredList.contains(from)) {
requiredList.add(to)
requiredList.remove(from)
}
}
fun Json.forParameter(
name: String,
block: Json.() -> Unit,
) {
block(
checkNotNull(
this.getTyped<List<Json>>("parameters").find { it["name"] == name },
) { "Could not get parameter $name" },
)
}
fun Json.removeProperty(vararg names: String) {
if (this.has("properties")) {
val properties = this.getTyped<Json>("properties")
names.forEach { properties.remove(it) }
}
if (this.has("allOf")) {
this.getTyped<List<Json>>("allOf").forEach {
it.removeProperty(*names)
}
}
}
fun Json.addResponse(
statusCode: String,
contentType: String,
schema: Any,
) {
require(this.has("responses")) {
"Json did not contain a 'responses' field: ${this.keys}"
}
val responses: Json = this.getTyped("responses")
val definition: Json = responses.getTyped(statusCode) ?: mutableMapOf()
val content: Json = definition.getTyped("content") ?: mutableMapOf()
val contentTypeValue: Json = definition.getTyped(contentType) ?: mutableMapOf()
contentTypeValue["schema"] = schema
content[contentType] = contentTypeValue
definition["content"] = content
responses[statusCode] = definition
}
fun Json.setRequired(
fieldName: String,
isRequired: Boolean,
) {
val requiredList = this.getTyped<MutableList<String>?>("required") ?: mutableListOf()
if (isRequired) {
requiredList.add(fieldName)
} else {
requiredList.remove(fieldName)
}
if (requiredList.isEmpty()) {
this.remove("required")
} else {
this.put("required", requiredList)
}
}
fun objectOf(vararg fields: Field): Json =
mutableMapOf(
"type" to "object",
"properties" to
fields
.map { it.toSpec() }
.reduce { s, t ->
t.forEach { (k, v) ->
s[k] = v
}
s
},
"required" to fields.filter { it.required }.map { it.name },
)
data class Field(
val name: String,
val type: String,
val isReference: Boolean = false,
val required: Boolean = false,
) {
fun toSpec(): Json {
val typeKey = if (isReference) "\$ref" else "type"
return mutableMapOf(
name to
mutableMapOf(
typeKey to type,
),
)
}
}
}