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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
//! Tests to ensure we minimize serde_json::Value usage in generated code
//!
//! These tests track and verify that we properly generate typed structures
//! instead of falling back to serde_json::Value where possible.
use openapi_to_rust::test_helpers::*;
use serde_json::json;
#[test]
fn test_discriminated_union_with_discriminator_mapping() {
// This tests the most common pattern where we currently fall back to serde_json::Value
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"MessageResult": {
"oneOf": [
{"$ref": "#/components/schemas/SucceededResult"},
{"$ref": "#/components/schemas/ErroredResult"},
{"$ref": "#/components/schemas/CanceledResult"}
],
"discriminator": {
"propertyName": "type",
"mapping": {
"succeeded": "#/components/schemas/SucceededResult",
"errored": "#/components/schemas/ErroredResult",
"canceled": "#/components/schemas/CanceledResult"
}
}
},
"SucceededResult": {
"type": "object",
"properties": {
"type": {"const": "succeeded"},
"message": {"$ref": "#/components/schemas/Message"}
},
"required": ["type", "message"]
},
"ErroredResult": {
"type": "object",
"properties": {
"type": {"const": "errored"},
"error": {"type": "string"}
},
"required": ["type", "error"]
},
"CanceledResult": {
"type": "object",
"properties": {
"type": {"const": "canceled"},
"reason": {"type": "string"}
},
"required": ["type", "reason"]
},
"Message": {
"type": "object",
"properties": {
"id": {"type": "string"},
"content": {"type": "string"}
},
"required": ["id", "content"]
}
}
}
});
let result = test_generation("discriminated_union_test", spec).expect("Generation failed");
// The discriminated union should be generated as an enum, not serde_json::Value
assert!(
result.contains("pub enum MessageResult"),
"MessageResult should be generated as an enum"
);
// Should not contain serde_json::Value in the MessageResult definition
assert!(
!result.contains("MessageResult(serde_json::Value)"),
"MessageResult enum should not contain serde_json::Value"
);
// Verify the enum has proper variants - check for different possible formats
assert!(
result.contains("SucceededResult") || result.contains("Succeeded"),
"Should contain SucceededResult variant"
);
assert!(
result.contains("ErroredResult") || result.contains("Errored"),
"Should contain ErroredResult variant"
);
assert!(
result.contains("CanceledResult") || result.contains("Canceled"),
"Should contain CanceledResult variant"
);
}
#[test]
fn test_content_block_delta_union() {
// Test the ContentBlockDelta pattern which has multiple variant types
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"ContentBlockDelta": {
"oneOf": [
{"$ref": "#/components/schemas/TextDelta"},
{"$ref": "#/components/schemas/InputJsonDelta"}
],
"discriminator": {
"propertyName": "type",
"mapping": {
"text_delta": "#/components/schemas/TextDelta",
"input_json_delta": "#/components/schemas/InputJsonDelta"
}
}
},
"TextDelta": {
"type": "object",
"properties": {
"type": {"const": "text_delta"},
"text": {"type": "string"}
},
"required": ["type", "text"]
},
"InputJsonDelta": {
"type": "object",
"properties": {
"type": {"const": "input_json_delta"},
"partial_json": {"type": "string"}
},
"required": ["type", "partial_json"]
}
}
}
});
let result =
test_generation("content_block_delta_union_test", spec).expect("Generation failed");
assert!(
result.contains("pub enum ContentBlockDelta"),
"ContentBlockDelta should be generated as an enum"
);
// Should not use serde_json::Value for the delta types
assert!(
!result.contains("ContentBlockDelta(serde_json::Value)"),
"ContentBlockDelta should not contain serde_json::Value"
);
// Verify variants - check for different possible formats
assert!(
result.contains("TextDelta"),
"Should contain TextDelta variant"
);
assert!(
result.contains("InputJsonDelta"),
"Should contain InputJsonDelta variant"
);
}
#[test]
fn test_array_of_discriminated_unions() {
// Test that arrays of discriminated unions generate properly typed Vec<T>
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"ToolList": {
"type": "array",
"items": {"$ref": "#/components/schemas/Tool"}
},
"Tool": {
"oneOf": [
{"$ref": "#/components/schemas/FunctionTool"},
{"$ref": "#/components/schemas/RetrievalTool"}
],
"discriminator": {
"propertyName": "type"
}
},
"FunctionTool": {
"type": "object",
"properties": {
"type": {"const": "function"},
"name": {"type": "string"},
"description": {"type": "string"}
},
"required": ["type", "name"]
},
"RetrievalTool": {
"type": "object",
"properties": {
"type": {"const": "retrieval"},
"query": {"type": "string"}
},
"required": ["type", "query"]
}
}
}
});
let result = test_generation("array_union_test", spec).expect("Generation failed");
// Should have Tool enum
assert!(
result.contains("pub enum Tool"),
"Tool should be generated as an enum"
);
// ToolList should be Vec<Tool> type alias or similar
assert!(
result.contains("pub type ToolList = Vec<Tool>"),
"ToolList should be Vec<Tool>, not Vec<serde_json::Value>"
);
}
#[test]
fn test_reference_resolution_in_complex_schemas() {
// Test that schema references are properly resolved instead of becoming serde_json::Value
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"Message": {
"type": "object",
"properties": {
"id": {"type": "string"},
"usage": {"$ref": "#/components/schemas/Usage"}
},
"required": ["id", "usage"]
},
"Usage": {
"type": "object",
"properties": {
"input_tokens": {"type": "integer"},
"output_tokens": {"type": "integer"}
},
"required": ["input_tokens", "output_tokens"]
}
}
}
});
let result = test_generation("ref_resolution_test", spec).expect("Generation failed");
// Should have Usage struct
assert!(
result.contains("pub struct Usage"),
"Usage should be generated as a struct"
);
// Message should reference Usage, not serde_json::Value
assert!(
result.contains("pub usage: Usage"),
"Message should have usage field of type Usage"
);
assert!(
!result.contains("pub usage: serde_json::Value"),
"Message should not contain serde_json::Value for usage field"
);
}
#[test]
fn test_nested_allof_composition() {
// Test AllOf composition that should flatten properties instead of using serde_json::Value
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"TextMessage": {
"allOf": [
{"$ref": "#/components/schemas/BaseMessage"},
{
"type": "object",
"properties": {
"content": {"type": "string"}
},
"required": ["content"]
}
]
},
"BaseMessage": {
"type": "object",
"properties": {
"id": {"type": "string"},
"timestamp": {"type": "string"}
},
"required": ["id", "timestamp"]
}
}
}
});
let result = test_generation("allof_test", spec).expect("Generation failed");
// Should have TextMessage with all properties flattened
assert!(result.contains("pub struct TextMessage"));
assert!(result.contains("pub id: String"));
assert!(result.contains("pub timestamp: String"));
assert!(result.contains("pub content: String"));
}
#[test]
fn test_object_with_additional_properties() {
// Test that objects with additionalProperties correctly use BTreeMap<String, serde_json::Value>
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"DynamicObject": {
"type": "object",
"properties": {
"name": {"type": "string"}
},
"required": ["name"],
"additionalProperties": true
}
}
}
});
let result = test_generation("additional_props_test", spec).expect("Generation failed");
// Verify additionalProperties generates BTreeMap<String, serde_json::Value>
assert!(result.contains("pub struct DynamicObject"));
assert!(
result.contains("#[serde(flatten)]")
&& result.contains("BTreeMap<String, serde_json::Value>"),
"DynamicObject should have flattened HashMap for additional properties"
);
}
#[test]
fn test_error_handling_discriminated_unions() {
// Test error types that are currently falling back to serde_json::Value
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"StreamError": {
"oneOf": [
{"$ref": "#/components/schemas/ApiError"},
{"$ref": "#/components/schemas/ValidationError"}
],
"discriminator": {
"propertyName": "type"
}
},
"ApiError": {
"type": "object",
"properties": {
"type": {"const": "api_error"},
"message": {"type": "string"},
"code": {"type": "integer"}
},
"required": ["type", "message", "code"]
},
"ValidationError": {
"type": "object",
"properties": {
"type": {"const": "validation_error"},
"field": {"type": "string"},
"reason": {"type": "string"}
},
"required": ["type", "field", "reason"]
}
}
}
});
let result = test_generation("error_union_test", spec).expect("Generation failed");
// Verify error unions generate proper enum types
assert!(
result.contains("pub enum StreamError"),
"StreamError should be generated as an enum"
);
assert!(
!result.contains("StreamError(serde_json::Value)"),
"StreamError should not contain serde_json::Value"
);
assert!(
result.contains("ApiError"),
"Should contain ApiError variant"
);
assert!(
result.contains("ValidationError"),
"Should contain ValidationError variant"
);
}
// Note: Additional test cases for comprehensive serde_json::Value reduction
// will be implemented as we enhance the generator with proper discriminated
// union support, reference resolution, and inline type generation
#[test]
fn test_count_serde_json_value_occurrences() {
// Meta-test to track reduction in serde_json::Value usage over time
use std::fs;
use std::path::Path;
let generated_path = Path::new(env!("CARGO_MANIFEST_DIR"))
.parent()
.unwrap()
.join("anthropic/src/generated/types.rs");
if generated_path.exists() {
let content = fs::read_to_string(&generated_path).unwrap();
let count = content.matches("serde_json::Value").count();
// Current baseline: 50 occurrences
// Goal: Reduce to ~18-20 (only truly dynamic fields)
println!("Current serde_json::Value occurrences: {count}");
// This will fail once we implement the fixes, reminding us to update the baseline
assert!(
count <= 50,
"serde_json::Value usage has increased! Current: {count}, Expected: <= 50"
);
// Future goal after implementing discriminated unions
// assert!(count <= 20, "Still too many serde_json::Value occurrences: {}. Target: <= 20", count);
}
}