Skip to content

Commit f454039

Browse files
committed
chore(osv-client): Revert unnecessary changes from 5f8f378
The mix of single-field and typed event serializations that was seen was caused by creating the new expected results with the wrong serializer. Reverting those parts of the expected results shows that the actual results do not require the serializer changes. To see how the diff of 5f8f378 should have looked like, run git diff 5f8f378^ HEAD -- clients/osv/src on this commit. Signed-off-by: Sebastian Schuberth <[email protected]>
1 parent 1641080 commit f454039

File tree

12 files changed

+88
-128
lines changed

12 files changed

+88
-128
lines changed

clients/osv/src/funTest/assets/vulnerabilities-by-commit-expected-result.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,10 @@
1919
"repo": "https://github.com/harfbuzz/harfbuzz.git",
2020
"events": [
2121
{
22-
"type": "INTRODUCED",
23-
"value": "4009a05ca7de21fff2176621597cd0cd01e9d80e"
22+
"introduced": "4009a05ca7de21fff2176621597cd0cd01e9d80e"
2423
},
2524
{
26-
"type": "FIXED",
27-
"value": "cc8e9a436fa408a1c63f4b9afb7643cea76a079c"
25+
"fixed": "cc8e9a436fa408a1c63f4b9afb7643cea76a079c"
2826
}
2927
]
3028
}

clients/osv/src/funTest/assets/vulnerabilities-by-name-and-version-expected-result.json

Lines changed: 28 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,10 @@
2727
"type": "ECOSYSTEM",
2828
"events": [
2929
{
30-
"type": "INTRODUCED",
31-
"value": "0"
30+
"introduced": "0"
3231
},
3332
{
34-
"type": "FIXED",
35-
"value": "2.10.1"
33+
"fixed": "2.10.1"
3634
}
3735
]
3836
}
@@ -178,7 +176,7 @@
178176
{
179177
"schema_version": "1.4.0",
180178
"id": "GHSA-8r7q-cvjq-x353",
181-
"modified": "2023-04-11T01:29:39.253214Z",
179+
"modified": "2023-03-28T05:34:47.958777Z",
182180
"published": "2022-05-14T04:04:14Z",
183181
"aliases": [
184182
"CVE-2014-1402"
@@ -197,12 +195,10 @@
197195
"type": "ECOSYSTEM",
198196
"events": [
199197
{
200-
"type": "INTRODUCED",
201-
"value": "0"
198+
"introduced": "0"
202199
},
203200
{
204-
"type": "FIXED",
205-
"value": "2.7.2"
201+
"fixed": "2.7.2"
206202
}
207203
]
208204
}
@@ -328,7 +324,7 @@
328324
{
329325
"schema_version": "1.4.0",
330326
"id": "GHSA-fqh9-2qgg-h84h",
331-
"modified": "2023-04-11T01:29:34.742416Z",
327+
"modified": "2023-03-28T05:32:13.513552Z",
332328
"published": "2022-05-17T04:01:00Z",
333329
"aliases": [
334330
"CVE-2014-0012"
@@ -347,12 +343,10 @@
347343
"type": "ECOSYSTEM",
348344
"events": [
349345
{
350-
"type": "INTRODUCED",
351-
"value": "0"
346+
"introduced": "0"
352347
},
353348
{
354-
"type": "FIXED",
355-
"value": "2.7.2"
349+
"fixed": "2.7.2"
356350
}
357351
]
358352
}
@@ -430,7 +424,7 @@
430424
{
431425
"schema_version": "1.4.0",
432426
"id": "GHSA-g3rq-g295-4j3m",
433-
"modified": "2023-04-11T01:27:03.685024Z",
427+
"modified": "2023-04-03T19:04:42.225806Z",
434428
"published": "2021-03-19T21:28:05Z",
435429
"aliases": [
436430
"CVE-2020-28493"
@@ -455,12 +449,10 @@
455449
"type": "ECOSYSTEM",
456450
"events": [
457451
{
458-
"type": "INTRODUCED",
459-
"value": "0"
452+
"introduced": "0"
460453
},
461454
{
462-
"type": "FIXED",
463-
"value": "2.11.3"
455+
"fixed": "2.11.3"
464456
}
465457
]
466458
}
@@ -561,7 +553,7 @@
561553
{
562554
"schema_version": "1.4.0",
563555
"id": "GHSA-hj2j-77xm-mc5v",
564-
"modified": "2023-04-11T01:41:57.013215Z",
556+
"modified": "2023-03-28T05:27:00.632427Z",
565557
"published": "2019-04-10T14:30:13Z",
566558
"aliases": [
567559
"CVE-2016-10745"
@@ -586,12 +578,10 @@
586578
"type": "ECOSYSTEM",
587579
"events": [
588580
{
589-
"type": "INTRODUCED",
590-
"value": "0"
581+
"introduced": "0"
591582
},
592583
{
593-
"type": "FIXED",
594-
"value": "2.8.1"
584+
"fixed": "2.8.1"
595585
}
596586
]
597587
}
@@ -715,12 +705,10 @@
715705
"type": "ECOSYSTEM",
716706
"events": [
717707
{
718-
"type": "INTRODUCED",
719-
"value": "0"
708+
"introduced": "0"
720709
},
721710
{
722-
"type": "FIXED",
723-
"value": "2.7.2"
711+
"fixed": "2.7.2"
724712
}
725713
]
726714
}
@@ -848,25 +836,21 @@
848836
"repo": "https://github.com/mitsuhiko/jinja2",
849837
"events": [
850838
{
851-
"type": "INTRODUCED",
852-
"value": "0"
839+
"introduced": "0"
853840
},
854841
{
855-
"type": "FIXED",
856-
"value": "acb672b6a179567632e032f547582f30fa2f4aa7"
842+
"fixed": "acb672b6a179567632e032f547582f30fa2f4aa7"
857843
}
858844
]
859845
},
860846
{
861847
"type": "ECOSYSTEM",
862848
"events": [
863849
{
864-
"type": "INTRODUCED",
865-
"value": "0"
850+
"introduced": "0"
866851
},
867852
{
868-
"type": "FIXED",
869-
"value": "2.7.3"
853+
"fixed": "2.7.3"
870854
}
871855
]
872856
}
@@ -955,12 +939,10 @@
955939
"type": "ECOSYSTEM",
956940
"events": [
957941
{
958-
"type": "INTRODUCED",
959-
"value": "0"
942+
"introduced": "0"
960943
},
961944
{
962-
"type": "FIXED",
963-
"value": "2.10.1"
945+
"fixed": "2.10.1"
964946
}
965947
]
966948
}
@@ -1109,25 +1091,21 @@
11091091
"repo": "https://github.com/pallets/jinja",
11101092
"events": [
11111093
{
1112-
"type": "INTRODUCED",
1113-
"value": "0"
1094+
"introduced": "0"
11141095
},
11151096
{
1116-
"type": "FIXED",
1117-
"value": "9b53045c34e61013dc8f09b7e52a555fa16bed16"
1097+
"fixed": "9b53045c34e61013dc8f09b7e52a555fa16bed16"
11181098
}
11191099
]
11201100
},
11211101
{
11221102
"type": "ECOSYSTEM",
11231103
"events": [
11241104
{
1125-
"type": "INTRODUCED",
1126-
"value": "0"
1105+
"introduced": "0"
11271106
},
11281107
{
1129-
"type": "FIXED",
1130-
"value": "2.8.1"
1108+
"fixed": "2.8.1"
11311109
}
11321110
]
11331111
}
@@ -1235,12 +1213,10 @@
12351213
"type": "ECOSYSTEM",
12361214
"events": [
12371215
{
1238-
"type": "INTRODUCED",
1239-
"value": "0"
1216+
"introduced": "0"
12401217
},
12411218
{
1242-
"type": "FIXED",
1243-
"value": "2.11.3"
1219+
"fixed": "2.11.3"
12441220
}
12451221
]
12461222
}

clients/osv/src/funTest/assets/vulnerability-by-id-expected-result.json

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,10 @@
2626
"type": "SEMVER",
2727
"events": [
2828
{
29-
"type": "INTRODUCED",
30-
"value": "1.0.0"
29+
"introduced": "1.0.0"
3130
},
3231
{
33-
"type": "FIXED",
34-
"value": "1.2.6"
32+
"fixed": "1.2.6"
3533
}
3634
]
3735
}
@@ -51,12 +49,10 @@
5149
"type": "SEMVER",
5250
"events": [
5351
{
54-
"type": "INTRODUCED",
55-
"value": "0"
52+
"introduced": "0"
5653
},
5754
{
58-
"type": "FIXED",
59-
"value": "0.2.4"
55+
"fixed": "0.2.4"
6056
}
6157
]
6258
}
@@ -125,12 +121,12 @@
125121
}
126122
],
127123
"database_specific": {
128-
"github_reviewed_at": "2022-03-18T23:13:40Z",
129-
"github_reviewed": true,
130-
"severity": "CRITICAL",
131124
"cwe_ids": [
132125
"CWE-1321"
133126
],
127+
"severity": "CRITICAL",
128+
"github_reviewed": true,
129+
"github_reviewed_at": "2022-03-18T23:13:40Z",
134130
"nvd_published_at": "2022-03-17T16:15:00Z"
135131
}
136132
}

clients/osv/src/main/kotlin/Model.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ object Ecosystem {
111111
const val RUBY_GEMS = "RubyGems"
112112
}
113113

114-
@Serializable
114+
@Serializable(EventSerializer::class)
115115
data class Event(
116116
val type: Type,
117117
val value: String
@@ -136,7 +136,6 @@ data class Package(
136136
data class Range(
137137
val type: Type,
138138
val repo: String? = null,
139-
@Serializable(EventListSerializer::class)
140139
val events: List<Event>,
141140
val databaseSpecific: JsonObject? = null
142141
) {

clients/osv/src/main/kotlin/Serializers.kt

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -23,35 +23,46 @@ import java.time.Instant
2323
import java.time.format.DateTimeFormatter
2424

2525
import kotlinx.serialization.KSerializer
26-
import kotlinx.serialization.builtins.ListSerializer
26+
import kotlinx.serialization.SerializationException
27+
import kotlinx.serialization.Serializer
2728
import kotlinx.serialization.descriptors.PrimitiveKind
2829
import kotlinx.serialization.descriptors.PrimitiveSerialDescriptor
2930
import kotlinx.serialization.descriptors.SerialDescriptor
3031
import kotlinx.serialization.encoding.Decoder
3132
import kotlinx.serialization.encoding.Encoder
32-
import kotlinx.serialization.json.JsonArray
33-
import kotlinx.serialization.json.JsonElement
33+
import kotlinx.serialization.json.JsonDecoder
34+
import kotlinx.serialization.json.JsonEncoder
3435
import kotlinx.serialization.json.JsonObject
3536
import kotlinx.serialization.json.JsonPrimitive
36-
import kotlinx.serialization.json.JsonTransformingSerializer
37-
import kotlinx.serialization.json.jsonArray
38-
import kotlinx.serialization.json.jsonObject
37+
import kotlinx.serialization.json.jsonPrimitive
3938

4039
/**
41-
* Use a custom serializer to transform the legacy single-field events into newer typed events.
40+
* Use a custom serializer in order to map the original data structure to a more strict and simple, enum based, model.
4241
*/
43-
internal object EventListSerializer : JsonTransformingSerializer<List<Event>>(ListSerializer(Event.serializer())) {
44-
override fun transformDeserialize(element: JsonElement): JsonElement =
45-
element.jsonArray.map {
46-
val event = it.jsonObject
47-
when (event.entries.size) {
48-
1 -> {
49-
val (type, value) = event.entries.first()
50-
JsonObject(mapOf("type" to JsonPrimitive(type.uppercase()), "value" to value))
51-
}
52-
else -> event
53-
}
54-
}.let { JsonArray(it) }
42+
@Serializer(Event::class)
43+
internal object EventSerializer : KSerializer<Event> {
44+
override fun deserialize(decoder: Decoder): Event {
45+
val input = decoder as? JsonDecoder
46+
?: throw SerializationException("This serializer only works with the JSON format.")
47+
48+
val element = input.decodeJsonElement()
49+
require(element is JsonObject)
50+
51+
require(element.entries.size == 1)
52+
val (key, value) = element.entries.first()
53+
val type = enumValueOf<Event.Type>(key.uppercase())
54+
55+
return Event(type, value.jsonPrimitive.content)
56+
}
57+
58+
override fun serialize(encoder: Encoder, value: Event) {
59+
val output = encoder as? JsonEncoder
60+
?: throw SerializationException("This serializer only works with the JSON format.")
61+
62+
val tree = JsonObject(mapOf(value.type.name.lowercase() to JsonPrimitive(value.value)))
63+
64+
output.encodeJsonElement(tree)
65+
}
5566
}
5667

5768
/**

clients/osv/src/test/assets/vulnerability/examples/1.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,16 @@
2929
"type": "SEMVER",
3030
"events": [
3131
{
32-
"type": "INTRODUCED",
33-
"value": "1.0.0"
32+
"introduced": "1.0.0"
3433
},
3534
{
36-
"type": "FIXED",
37-
"value": "1.14.14"
35+
"fixed": "1.14.14"
3836
},
3937
{
40-
"type": "INTRODUCED",
41-
"value": "1.15.0"
38+
"introduced": "1.15.0"
4239
},
4340
{
44-
"type": "FIXED",
45-
"value": "1.15.17"
41+
"fixed": "1.15.17"
4642
}
4743
]
4844
}

0 commit comments

Comments
 (0)