Skip to content

Commit 0924ea2

Browse files
Merge pull request #2937 from DataDog/aleksandr-gringauz/RUM-11784/update-vitals-schema
RUM-11784: Update RUM Schema to include app launch vitals
2 parents 18545bb + a22b805 commit 0924ea2

File tree

29 files changed

+1147
-553
lines changed

29 files changed

+1147
-553
lines changed

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ class JsonSchemaReader(
172172
TypeProperty(
173173
name = "",
174174
type = typeDef,
175-
optional = true,
176175
readOnly = readOnly ?: false,
177176
defaultValue = null
178177
)
@@ -185,7 +184,6 @@ class JsonSchemaReader(
185184
TypeProperty(
186185
name = "",
187186
type = typeDef,
188-
optional = true,
189187
readOnly = false,
190188
defaultValue = null
191189
)
@@ -358,7 +356,11 @@ class JsonSchemaReader(
358356
allOf: List<JsonDefinition>,
359357
fromFile: File
360358
): TypeDefinition {
361-
var mergedType: TypeDefinition = TypeDefinition.Class(typeName, emptyList())
359+
var mergedType: TypeDefinition = TypeDefinition.Class(
360+
name = typeName,
361+
properties = emptyList(),
362+
required = emptySet()
363+
)
362364

363365
allOf.forEach {
364366
val type = transform(it, typeName, fromFile)
@@ -374,7 +376,6 @@ class JsonSchemaReader(
374376
): TypeDefinition {
375377
val properties = mutableListOf<TypeProperty>()
376378
definition.properties?.forEach { (name, property) ->
377-
val required = (definition.required != null) && (name in definition.required)
378379
val readOnly = (property.readOnly == null) || (property.readOnly)
379380
val propertyType = transform(
380381
property,
@@ -385,7 +386,6 @@ class JsonSchemaReader(
385386
TypeProperty(
386387
name,
387388
propertyType,
388-
!required,
389389
readOnly,
390390
property.default
391391
)
@@ -397,7 +397,8 @@ class JsonSchemaReader(
397397
name = typeName,
398398
description = definition.description.orEmpty(),
399399
properties = properties,
400-
additionalProperties = additional
400+
additionalProperties = additional,
401+
required = definition.required.orEmpty().toSet()
401402
)
402403
}
403404

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ sealed class TypeDefinition {
101101
data class Class(
102102
val name: String,
103103
val properties: List<TypeProperty>,
104+
val required: Set<String>,
104105
override val description: String = "",
105106
val additionalProperties: TypeProperty? = null,
106107
val parentType: OneOfClass? = null
@@ -135,18 +136,22 @@ sealed class TypeDefinition {
135136
additionalProperties.mergedWith(other.additionalProperties)
136137
}
137138

139+
val mergedRequired = this.required + other.required
140+
138141
return Class(
139-
name,
140-
mergedFields,
141-
"$description\n${other.description}".trim(),
142-
mergedAdditionalProperties
142+
name = name,
143+
properties = mergedFields,
144+
required = mergedRequired,
145+
description = "$description\n${other.description}".trim(),
146+
additionalProperties = mergedAdditionalProperties
143147
)
144148
}
145149

146150
override fun matches(other: TypeDefinition): Boolean {
147151
return (other is Class) &&
148152
(other.properties == properties) &&
149-
(other.additionalProperties == additionalProperties)
153+
(other.additionalProperties == additionalProperties) &&
154+
(other.required == required)
150155
}
151156

152157
fun isConstantClass(): Boolean {

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ package com.datadog.gradle.plugin.jsonschema
99
data class TypeProperty(
1010
val name: String,
1111
val type: TypeDefinition,
12-
val optional: Boolean,
1312
val readOnly: Boolean = true,
1413
val defaultValue: Any? = null
1514
) {
@@ -18,9 +17,8 @@ data class TypeProperty(
1817
this
1918
} else {
2019
TypeProperty(
21-
name,
22-
type.mergedWith(other.type),
23-
optional && other.optional
20+
name = name,
21+
type = type.mergedWith(other.type)
2422
)
2523
}
2624
}

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

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ class ClassGenerator(
5959
typeBuilder.addKdoc(generateKDoc(definition))
6060

6161
definition.properties.forEach {
62-
typeBuilder.addProperty(generateProperty(it, rootTypeName))
62+
typeBuilder.addProperty(
63+
generateProperty(
64+
property = it,
65+
rootTypeName = rootTypeName,
66+
isRequired = definition.required.contains(it.name)
67+
)
68+
)
6369
}
6470
if (definition.additionalProperties != null) {
6571
typeBuilder.addProperty(
@@ -121,7 +127,10 @@ class ClassGenerator(
121127
funBuilder.addStatement("val json = %T()", ClassNameRef.JsonObject)
122128

123129
definition.properties.forEach { p ->
124-
funBuilder.appendPropertySerialization(p)
130+
funBuilder.appendPropertySerialization(
131+
property = p,
132+
isRequired = definition.required.contains(p.name)
133+
)
125134
}
126135

127136
if (definition.additionalProperties != null) {
@@ -137,11 +146,12 @@ class ClassGenerator(
137146
}
138147

139148
private fun FunSpec.Builder.appendPropertySerialization(
140-
property: TypeProperty
149+
property: TypeProperty,
150+
isRequired: Boolean
141151
) {
142152
val propertyName = property.name.variableName()
143153
val isNullable =
144-
property.optional && property.type !is TypeDefinition.Constant && property.type !is TypeDefinition.Null
154+
!isRequired && property.type !is TypeDefinition.Constant && property.type !is TypeDefinition.Null
145155
val refName = if (isNullable) {
146156
beginControlFlow("%L?.let·{·%LNonNull·->", propertyName, propertyName)
147157
"${propertyName}NonNull"
@@ -308,12 +318,17 @@ class ClassGenerator(
308318
definition.properties.forEach { p ->
309319
if (p.type !is TypeDefinition.Constant) {
310320
val propertyName = p.name.variableName()
311-
val isNullable = (p.optional || p.type is TypeDefinition.Null)
321+
val isRequired = definition.required.contains(p.name)
322+
val isNullable = (!isRequired || p.type is TypeDefinition.Null)
312323
val notNullableType = p.type.asKotlinTypeName(rootTypeName)
313324
val propertyType = notNullableType.copy(nullable = isNullable)
314325
constructorBuilder.addParameter(
315326
ParameterSpec.builder(propertyName, propertyType)
316-
.withDefaultValue(p, rootTypeName)
327+
.withDefaultValue(
328+
p = p,
329+
rootTypeName = rootTypeName,
330+
isRequired = isRequired
331+
)
317332
.build()
318333
)
319334
}
@@ -334,10 +349,10 @@ class ClassGenerator(
334349
return constructorBuilder.build()
335350
}
336351

337-
private fun generateProperty(property: TypeProperty, rootTypeName: String): PropertySpec {
352+
private fun generateProperty(property: TypeProperty, rootTypeName: String, isRequired: Boolean): PropertySpec {
338353
val propertyName = property.name.variableName()
339354
val propertyType = property.type
340-
val isNullable = (property.optional || propertyType is TypeDefinition.Null) &&
355+
val isNullable = (!isRequired || propertyType is TypeDefinition.Null) &&
341356
(propertyType !is TypeDefinition.Constant)
342357
val notNullableType = propertyType.asKotlinTypeName(rootTypeName)
343358
val type = notNullableType.copy(nullable = isNullable)
@@ -427,7 +442,8 @@ class ClassGenerator(
427442

428443
private fun ParameterSpec.Builder.withDefaultValue(
429444
p: TypeProperty,
430-
rootTypeName: String
445+
rootTypeName: String,
446+
isRequired: Boolean
431447
): ParameterSpec.Builder {
432448
val defaultValue = p.defaultValue
433449
if (defaultValue != null) {
@@ -456,7 +472,7 @@ class ClassGenerator(
456472
"This feature is not supported yet"
457473
)
458474
}
459-
} else if (p.optional || p.type is TypeDefinition.Null) {
475+
} else if (!isRequired || p.type is TypeDefinition.Null) {
460476
defaultValue("null")
461477
}
462478
return this

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class ClassJsonElementDeserializerGenerator(
6666
propertyType = p.type,
6767
assignee = "val ${p.name.variableName()}",
6868
getter = "${Identifier.PARAM_JSON_OBJ}.get(\"${p.name}\")",
69-
nullable = p.optional,
69+
nullable = !definition.required.contains(p.name),
7070
rootTypeName = rootTypeName
7171
)
7272
}
@@ -80,7 +80,7 @@ class ClassJsonElementDeserializerGenerator(
8080

8181
definition.properties
8282
.filter { it.type is TypeDefinition.Constant }
83-
.forEach { appendConstantPropertyCheck(it) }
83+
.forEach { appendConstantPropertyCheck(property = it, isRequired = definition.required.contains(it.name)) }
8484

8585
val nonConstantProperties = definition.properties
8686
.filter { it.type !is TypeDefinition.Constant }
@@ -91,11 +91,11 @@ class ClassJsonElementDeserializerGenerator(
9191
addStatement("return %L($constructorArguments)", definition.name)
9292
}
9393

94-
private fun FunSpec.Builder.appendConstantPropertyCheck(property: TypeProperty) {
94+
private fun FunSpec.Builder.appendConstantPropertyCheck(property: TypeProperty, isRequired: Boolean) {
9595
val constant = property.type as TypeDefinition.Constant
9696
val variableName = property.name.variableName()
9797

98-
if (property.optional) {
98+
if (!isRequired) {
9999
beginControlFlow("if (%L != null)", variableName)
100100
}
101101

@@ -113,7 +113,7 @@ class ClassJsonElementDeserializerGenerator(
113113
JsonType.ARRAY -> TODO()
114114
null -> TODO()
115115
}
116-
if (property.optional) {
116+
if (!isRequired) {
117117
endControlFlow()
118118
}
119119
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ class JsonSchemaReaderTest(
8686
arrayOf("all_of_merged", UserMerged),
8787
arrayOf("constant_number", Version),
8888
arrayOf("sets", Video),
89-
arrayOf("one_of_nested", WeirdCombo)
89+
arrayOf("one_of_nested", WeirdCombo),
90+
arrayOf("required_for_other_all_of", RequiredForOtherAllOf)
9091
)
9192
}
9293
}

0 commit comments

Comments
 (0)