Skip to content

Commit c038e65

Browse files
authored
Merge pull request #2793 from DataDog/tvaleev/feature/RUM-10316-properties-ref-support
RUM-10316 - properties referencing support
2 parents 207affa + 819ecb7 commit c038e65

File tree

4 files changed

+61
-3
lines changed

4 files changed

+61
-3
lines changed

buildSrc/src/main/kotlin/com/datadog/gradle/plugin/jsonschema/JsonSchemaReader.kt

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,9 @@ class JsonSchemaReader(
6262
return loadDefinitionFromFileRef(path, localRef, fromFile)
6363
}
6464

65-
val name = REF_DEFINITION_REGEX.matchEntire(ref)?.groupValues?.get(1)
65+
val fieldGroups = REF_NAME_REGEX.matchEntire(ref)?.groupValues
66+
val type = fieldGroups?.get(1)
67+
val name = fieldGroups?.get(2)
6668
val id = REF_ID_REGEX.matchEntire(ref)?.groupValues?.get(0)
6769
if (name == null && id == null) return null
6870

@@ -73,7 +75,10 @@ class JsonSchemaReader(
7375
}
7476

7577
val match = knownSchemas[fromFile]
76-
?.definitions
78+
?.let {
79+
// only explicit properties lookup supported
80+
if (type == REF_TYPE_PROPERTIES) it.properties else it.definitions
81+
}
7782
?.entries
7883
?.firstOrNull { matcher(it.key, it.value) } ?: return null
7984

@@ -417,8 +422,10 @@ class JsonSchemaReader(
417422
// endregion
418423

419424
companion object {
425+
private const val REF_TYPE_PROPERTIES = "properties"
426+
private const val REF_TYPE_DEFINITIONS = "definitions"
420427

421-
private val REF_DEFINITION_REGEX = Regex("#/definitions/([\\w]+)")
428+
private val REF_NAME_REGEX = Regex("#/($REF_TYPE_DEFINITIONS|$REF_TYPE_PROPERTIES)/([\\w]+)")
422429
private val REF_ID_REGEX = Regex("#[\\w]+")
423430
private val REF_FILE_REGEX = Regex("(file:)?(([^/]+/)*([^/]+)\\.json)(#(.*))?")
424431
}

buildSrc/src/test/kotlin/com/datadog/gradle/plugin/jsonschema/JsonSchemaReaderTest.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class JsonSchemaReaderTest(
8080
arrayOf("minimal", Person),
8181
arrayOf("required", Product),
8282
arrayOf("external_nested_description", Shipping),
83+
arrayOf("external_nested_description_properties", Shipping),
8384
arrayOf("enum", Style),
8485
arrayOf("all_of", User),
8586
arrayOf("all_of_merged", UserMerged),
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Shipping",
4+
"type": "object",
5+
"properties": {
6+
"item": {
7+
"type": "string"
8+
},
9+
"destination": {
10+
"$ref": "properties.json#/properties/billing_address"
11+
}
12+
},
13+
"required": [
14+
"item",
15+
"destination"
16+
]
17+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
{
2+
"$schema": "http://json-schema.org/draft-07/schema#",
3+
"title": "Customer",
4+
"type": "object",
5+
"properties": {
6+
"name": {
7+
"type": "string"
8+
},
9+
"billing_address": { "$ref": "#/definitions/address" },
10+
"shipping_address": { "$ref": "#/definitions/address" }
11+
},
12+
"definitions": {
13+
"address": {
14+
"type": "object",
15+
"properties": {
16+
"street_address": {
17+
"type": "string"
18+
},
19+
"city": {
20+
"type": "string"
21+
},
22+
"state": {
23+
"type": "string"
24+
}
25+
},
26+
"required": [
27+
"street_address",
28+
"city",
29+
"state"
30+
]
31+
}
32+
}
33+
}

0 commit comments

Comments
 (0)