Skip to content

Commit 1d72b45

Browse files
doljack-berg
andauthored
refactor: replace patternProperties with additionalProperties for improved type safety (#323)
- Replace `"additionalProperties": true` with typed `additionalProperties` schemas - Remove redundant `patternProperties` in favor of `additionalProperties` - Update wildcard pattern matching from ".*" to ".+" for more precise matching - Affects schema files: instrumentation, logger_provider, meter_provider, propagator, resource, tracer_provider - Update documentation and script comments to reflect pattern change from ".*" to ".+" This change improves JSON schema validation by providing explicit typing for additional properties instead of allowing any properties, while maintaining backward compatibility for configuration extensions. Co-authored-by: Jack Berg <[email protected]>
1 parent 7ccd71b commit 1d72b45

File tree

8 files changed

+38
-74
lines changed

8 files changed

+38
-74
lines changed

schema/instrumentation.json

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22
"$id": "https://opentelemetry.io/otelconfig/instrumentation.json",
33
"$schema": "https://json-schema.org/draft/2020-12/schema",
44
"type": "object",
5-
"additionalProperties": false,
5+
"additionalProperties": {
6+
"$ref": "#/$defs/ExperimentalLanguageSpecificInstrumentation"
7+
},
68
"properties": {
79
"general": {
810
"$ref": "#/$defs/ExperimentalGeneralInstrumentation"
@@ -41,11 +43,6 @@
4143
"$ref": "#/$defs/ExperimentalLanguageSpecificInstrumentation"
4244
}
4345
},
44-
"patternProperties": {
45-
".*": {
46-
"$ref": "#/$defs/ExperimentalLanguageSpecificInstrumentation"
47-
}
48-
},
4946
"$defs": {
5047
"ExperimentalGeneralInstrumentation": {
5148
"type": "object",
@@ -128,11 +125,8 @@
128125
},
129126
"ExperimentalLanguageSpecificInstrumentation": {
130127
"type": "object",
131-
"additionalProperties": true,
132-
"patternProperties": {
133-
".*": {
134-
"type": "object"
135-
}
128+
"additionalProperties": {
129+
"type": "object"
136130
}
137131
}
138132
}

schema/logger_provider.json

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@
6464
},
6565
"LogRecordExporter": {
6666
"type": "object",
67-
"additionalProperties": true,
67+
"additionalProperties": {
68+
"type": ["object", "null"]
69+
},
6870
"minProperties": 1,
6971
"maxProperties": 1,
7072
"properties": {
@@ -80,11 +82,6 @@
8082
"console": {
8183
"$ref": "common.json#/$defs/ConsoleExporter"
8284
}
83-
},
84-
"patternProperties": {
85-
".*": {
86-
"type": ["object", "null"]
87-
}
8885
}
8986
},
9087
"LogRecordLimits": {
@@ -103,7 +100,9 @@
103100
},
104101
"LogRecordProcessor": {
105102
"type": "object",
106-
"additionalProperties": true,
103+
"additionalProperties": {
104+
"type": ["object"]
105+
},
107106
"minProperties": 1,
108107
"maxProperties": 1,
109108
"properties": {
@@ -113,11 +112,6 @@
113112
"simple": {
114113
"$ref": "#/$defs/SimpleLogRecordProcessor"
115114
}
116-
},
117-
"patternProperties": {
118-
".*": {
119-
"type": ["object"]
120-
}
121115
}
122116
},
123117
"ExperimentalLoggerConfigurator": {

schema/meter_provider.json

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@
126126
},
127127
"PushMetricExporter": {
128128
"type": "object",
129-
"additionalProperties": true,
129+
"additionalProperties": {
130+
"type": ["object", "null"]
131+
},
130132
"minProperties": 1,
131133
"maxProperties": 1,
132134
"properties": {
@@ -142,43 +144,32 @@
142144
"console": {
143145
"$ref": "common.json#/$defs/ConsoleExporter"
144146
}
145-
},
146-
"patternProperties": {
147-
".*": {
148-
"type": ["object", "null"]
149-
}
150147
}
151148
},
152149
"PullMetricExporter": {
153150
"type": ["object"],
154-
"additionalProperties": true,
151+
"additionalProperties": {
152+
"type": ["object", "null"]
153+
},
155154
"minProperties": 1,
156155
"maxProperties": 1,
157156
"properties": {
158157
"prometheus/development": {
159158
"$ref": "#/$defs/ExperimentalPrometheusMetricExporter"
160159
}
161-
},
162-
"patternProperties": {
163-
".*": {
164-
"type": ["object", "null"]
165-
}
166160
}
167161
},
168162
"MetricProducer": {
169163
"type": "object",
170-
"additionalProperties": true,
164+
"additionalProperties": {
165+
"type": ["object", "null"]
166+
},
171167
"minProperties": 1,
172168
"maxProperties": 1,
173169
"properties": {
174170
"opencensus": {
175171
"$ref": "#/$defs/OpenCensusMetricProducer"
176172
}
177-
},
178-
"patternProperties": {
179-
".*": {
180-
"type": ["object", "null"]
181-
}
182173
}
183174
},
184175
"OpenCensusMetricProducer": {

schema/propagator.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616
"$defs": {
1717
"TextMapPropagator": {
1818
"type": "object",
19-
"additionalProperties": true,
19+
"additionalProperties": {
20+
"type": ["object", "null"]
21+
},
2022
"minProperties": 1,
2123
"maxProperties": 1,
2224
"properties": {
@@ -38,11 +40,6 @@
3840
"ottrace": {
3941
"$ref": "#/$defs/OpenTracingPropagator"
4042
}
41-
},
42-
"patternProperties": {
43-
".*": {
44-
"type": ["object", "null"]
45-
}
4643
}
4744
},
4845
"TraceContextPropagator": {

schema/resource.json

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@
7878
},
7979
"ExperimentalResourceDetector": {
8080
"type": "object",
81-
"additionalProperties": true,
81+
"additionalProperties": {
82+
"type": ["object", "null"]
83+
},
8284
"minProperties": 1,
8385
"maxProperties": 1,
8486
"properties": {
@@ -94,11 +96,6 @@
9496
"service": {
9597
"$ref": "#/$defs/ExperimentalServiceResourceDetector"
9698
}
97-
},
98-
"patternProperties": {
99-
".*": {
100-
"type": ["object", "null"]
101-
}
10299
}
103100
},
104101
"ExperimentalContainerResourceDetector": {

schema/tracer_provider.json

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@
5555
},
5656
"Sampler": {
5757
"type": "object",
58-
"additionalProperties": true,
58+
"additionalProperties": {
59+
"type": ["object", "null"]
60+
},
5961
"minProperties": 1,
6062
"maxProperties": 1,
6163
"properties": {
@@ -74,11 +76,6 @@
7476
"trace_id_ratio_based": {
7577
"$ref": "#/$defs/TraceIdRatioBasedSampler"
7678
}
77-
},
78-
"patternProperties": {
79-
".*": {
80-
"type": ["object", "null"]
81-
}
8279
}
8380
},
8481
"AlwaysOffSampler": {
@@ -149,7 +146,9 @@
149146
},
150147
"SpanExporter": {
151148
"type": "object",
152-
"additionalProperties": true,
149+
"additionalProperties": {
150+
"type": ["object", "null"]
151+
},
153152
"minProperties": 1,
154153
"maxProperties": 1,
155154
"properties": {
@@ -168,11 +167,6 @@
168167
"zipkin": {
169168
"$ref": "#/$defs/ZipkinSpanExporter"
170169
}
171-
},
172-
"patternProperties": {
173-
".*": {
174-
"type": ["object", "null"]
175-
}
176170
}
177171
},
178172
"SpanLimits": {
@@ -207,7 +201,9 @@
207201
},
208202
"SpanProcessor": {
209203
"type": "object",
210-
"additionalProperties": true,
204+
"additionalProperties": {
205+
"type": ["object", "null"]
206+
},
211207
"minProperties": 1,
212208
"maxProperties": 1,
213209
"properties": {
@@ -217,11 +213,6 @@
217213
"simple": {
218214
"$ref": "#/$defs/SimpleSpanProcessor"
219215
}
220-
},
221-
"patternProperties": {
222-
".*": {
223-
"type": ["object", "null"]
224-
}
225216
}
226217
},
227218
"ZipkinSpanExporter": {

schema/type_descriptions.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
# propertyB: The propertyB description. # The description for MyType.propertyB
99
# path_patterns: # Array of patterns where this type occurs in the schema. Each key-value in an example configuration file is transformed
1010
# - .foo # into its JSON dot-notation location in the file, which is matched against these patterns. Each pattern is turned in a regex,
11-
# after escaping special characters and replacing "*" with ".*".
11+
# after escaping special characters and replacing "*" with ".+".
1212

1313
# START OpenTelemetryConfiguration
1414
- type: OpenTelemetryConfiguration

scripts/generate-descriptions.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,15 @@ if (outputFile === null) {
135135
// Helper functions
136136

137137
// Converts an input pattern to a regular expression.
138-
// Converts any "*" to a ".*".
138+
// Converts any "*" to a ".+".
139139
// Escapes all other regex special characters.
140140
// Prefixes with "^", and adds "$" suffix.
141141
function toRegex(pattern) {
142142
let parts = pattern.split("*");
143143
if (parts.length === 0) parts = [pattern];
144144
const escaped = parts
145145
.map(chunk => chunk.replace(/[-[\]{}()*+?.,\\^$|]/g, "\\$&"))
146-
.join("(.*)");
146+
.join("(.+)");
147147
return "^" + escaped + "$";
148148
}
149149

0 commit comments

Comments
 (0)