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
#[cfg(test)]
mod tests {
use serde_json::json;
#[test]
fn test_openai_input_generates_ergonomic_types() {
// Test the exact OpenAI API pattern
let spec_json = json!({
"openapi": "3.1.0",
"info": {
"title": "OpenAI API",
"version": "2.3.0"
},
"components": {
"schemas": {
"CreateChatCompletionRequest": {
"type": "object",
"properties": {
"messages": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ChatCompletionRequestMessage"
}
},
"model": {
"type": "string"
},
"store": {
"type": "boolean",
"nullable": true
}
},
"required": ["messages", "model"]
},
"ChatCompletionRequestMessage": {
"type": "object",
"properties": {
"role": {
"type": "string",
"enum": ["system", "user", "assistant"]
},
"content": {
"type": "string"
}
},
"required": ["role", "content"]
},
"CreateCompletionRequest": {
"type": "object",
"properties": {
"model": {
"type": "string"
},
"prompt": {
"description": "The prompt(s) to generate completions for",
"oneOf": [
{
"type": "string",
"description": "A single prompt string"
},
{
"type": "array",
"description": "Multiple prompts",
"items": {
"type": "string"
}
}
]
},
"max_tokens": {
"type": "integer",
"nullable": true
}
},
"required": ["model", "prompt"]
},
"CreateEmbeddingRequest": {
"type": "object",
"properties": {
"input": {
"description": "Input text to embed",
"oneOf": [
{
"type": "string",
"description": "A single input string"
},
{
"type": "array",
"description": "Multiple input strings",
"items": {
"type": "string"
}
},
{
"type": "array",
"description": "Token arrays",
"items": {
"type": "integer"
}
},
{
"type": "array",
"description": "Arrays of token arrays",
"items": {
"type": "array",
"items": {
"type": "integer"
}
}
}
]
},
"model": {
"type": "string"
}
},
"required": ["input", "model"]
}
}
}
});
let mut analyzer = openapi_to_rust::SchemaAnalyzer::new(spec_json.clone()).unwrap();
let mut analysis = analyzer.analyze().unwrap();
let generator = openapi_to_rust::CodeGenerator::new(Default::default());
let types_content = generator.generate(&mut analysis).unwrap();
println!("Generated OpenAI types:\n{types_content}");
// Check for ergonomic type names
// 1. CreateCompletionRequest should have a nice Prompt union type
assert!(
types_content.contains("pub struct CreateCompletionRequest"),
"Should generate CreateCompletionRequest struct"
);
assert!(
types_content.contains("pub prompt: CreateCompletionRequestPrompt"),
"Prompt field should use a proper union type, not serde_json::Value"
);
assert!(
types_content.contains("pub enum CreateCompletionRequestPrompt"),
"Should generate union enum for prompt"
);
assert!(
types_content.contains("#[serde(untagged)]"),
"Prompt union should be untagged"
);
// 2. CreateEmbeddingRequest should have a nice Input union type
assert!(
types_content.contains("pub struct CreateEmbeddingRequest"),
"Should generate CreateEmbeddingRequest struct"
);
assert!(
types_content.contains("pub input: CreateEmbeddingRequestInput"),
"Input field should use a proper union type"
);
assert!(
types_content.contains("pub enum CreateEmbeddingRequestInput"),
"Should generate union enum for input"
);
// Check the variants have good names
assert!(
types_content.contains("String(String)"),
"Should have String variant"
);
assert!(
types_content.contains("StringArray(Vec<String>)")
|| types_content.contains("Vec<String>"),
"Should have string array variant"
);
assert!(
types_content.contains("IntegerArray(Vec<i64>)") || types_content.contains("Vec<i64>"),
"Should have integer array variant"
);
assert!(
types_content.contains("IntegerArrayArray(Vec<Vec<i64>>)")
|| types_content.contains("Vec<Vec<i64>>"),
"Should have array of integer arrays variant"
);
// 3. No serde_json::Value for oneOf fields
assert!(
!types_content.contains("pub prompt: serde_json::Value"),
"Prompt should NOT be serde_json::Value"
);
assert!(
!types_content.contains("pub input: serde_json::Value"),
"Input should NOT be serde_json::Value"
);
// 4. Nullable fields should be Option<T>
assert!(
types_content.contains("pub store: Option<bool>"),
"Nullable boolean should be Option<bool>"
);
assert!(
types_content.contains("pub max_tokens: Option<i32>")
|| types_content.contains("pub max_tokens: Option<i64>"),
"Nullable integer should be Option<i32/i64>"
);
}
#[test]
fn test_openai_streaming_response_types() {
// Test streaming response types specifically
let spec_json = json!({
"openapi": "3.1.0",
"info": {
"title": "OpenAI Streaming API",
"version": "1.0.0"
},
"components": {
"schemas": {
"CreateChatCompletionStreamResponse": {
"type": "object",
"properties": {
"id": {
"type": "string"
},
"object": {
"type": "string",
"enum": ["chat.completion.chunk"]
},
"created": {
"type": "integer"
},
"model": {
"type": "string"
},
"choices": {
"type": "array",
"items": {
"$ref": "#/components/schemas/ChatCompletionStreamResponseChoice"
}
}
},
"required": ["id", "object", "created", "model", "choices"]
},
"ChatCompletionStreamResponseChoice": {
"type": "object",
"properties": {
"index": {
"type": "integer"
},
"delta": {
"$ref": "#/components/schemas/ChatCompletionStreamResponseDelta"
},
"finish_reason": {
"type": "string",
"nullable": true,
"enum": ["stop", "length", "tool_calls", "content_filter", "function_call"]
}
},
"required": ["index", "delta"]
},
"ChatCompletionStreamResponseDelta": {
"type": "object",
"properties": {
"content": {
"type": "string",
"nullable": true
},
"role": {
"type": "string",
"enum": ["system", "user", "assistant", "tool"]
}
}
}
}
}
});
let mut analyzer = openapi_to_rust::SchemaAnalyzer::new(spec_json.clone()).unwrap();
let mut analysis = analyzer.analyze().unwrap();
let generator = openapi_to_rust::CodeGenerator::new(Default::default());
let types_content = generator.generate(&mut analysis).unwrap();
// Check streaming types have nice names
assert!(
types_content.contains("pub struct CreateChatCompletionStreamResponse"),
"Should generate streaming response struct"
);
assert!(
types_content.contains("pub struct ChatCompletionStreamResponseChoice"),
"Should generate choice struct"
);
assert!(
types_content.contains("pub struct ChatCompletionStreamResponseDelta"),
"Should generate delta struct"
);
// Enums should be properly typed
assert!(
types_content.contains("pub object: CreateChatCompletionStreamResponseObject")
|| types_content.contains("pub object: String"),
"Object field should be typed"
);
// Nullable fields
assert!(
types_content.contains("pub content: Option<String>"),
"Nullable content should be Option<String>"
);
assert!(
types_content.contains("pub finish_reason: Option<"),
"Nullable finish_reason should be Option<T>"
);
}
}