openapi-to-rust 0.4.0

Generate strongly-typed Rust structs, HTTP clients, and SSE streaming clients from OpenAPI 3.1 specifications
Documentation
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
#[cfg(test)]
mod tests {
    use openapi_to_rust::test_helpers::*;
    use serde_json::json;

    #[test]
    fn test_x_stainless_const_bug_nested() {
        // Test that reproduces the exact nesting issue from OpenAI spec
        let spec = minimal_spec(json!({
            "ReasoningPart": {
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the part. Always `summary_text`.",
                        "enum": ["summary_text"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "text": {
                        "type": "string"
                    }
                },
                "required": ["type", "text"]
            },
            "ResponseEvent": {
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the event. Always `response.event.done`.",
                        "enum": ["response.event.done"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "part": {
                        "$ref": "#/components/schemas/ReasoningPart"
                    }
                },
                "required": ["type", "part"]
            }
        }));

        let result = test_generation("x_stainless_nested", spec).expect("Generation failed");

        println!("Generated output:\n{}", result);

        // Test that ReasoningPart type field has correct enum value
        assert!(
            result.contains("pub enum ReasoningPartType"),
            "ReasoningPartType enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"summary_text\")]"),
            "ReasoningPartType should have summary_text variant with correct serde rename"
        );

        // CRITICAL BUG TEST: ResponseEvent type field should NOT be summary_text
        assert!(
            result.contains("pub enum ResponseEventType"),
            "ResponseEventType enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"response.event.done\")]"),
            "ResponseEventType should have response.event.done variant with correct serde rename - NOT summary_text"
        );

        // Make sure the wrong value isn't there for ResponseEvent
        if result.contains("ResponseEventType") {
            assert!(
                !result.contains("ResponseEventType")
                    || !result.contains("summary_text")
                    || result.contains("response.event.done"),
                "ResponseEventType should NOT have summary_text enum value - this would indicate the bug"
            );
        }
    }

    #[test]
    fn test_x_stainless_const_bug_discriminated_union() {
        // Test that reproduces the exact discriminated union pattern from OpenAI spec
        let spec = minimal_spec(json!({
            "ReasoningPart": {
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the part. Always `summary_text`.",
                        "enum": ["summary_text"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "text": {
                        "type": "string"
                    }
                },
                "required": ["type", "text"]
            },
            "ResponseReasoningSummaryPartAddedEvent": {
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the event. Always `response.reasoning_summary_part.added`.",
                        "enum": ["response.reasoning_summary_part.added"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "part": {
                        "$ref": "#/components/schemas/ReasoningPart"
                    }
                },
                "required": ["type", "part"]
            },
            "ResponseReasoningSummaryPartDoneEvent": {
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the event. Always `response.reasoning_summary_part.done`.",
                        "enum": ["response.reasoning_summary_part.done"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "part": {
                        "$ref": "#/components/schemas/ReasoningPart"
                    }
                },
                "required": ["type", "part"]
            },
            "EventUnion": {
                "oneOf": [
                    { "$ref": "#/components/schemas/ResponseReasoningSummaryPartAddedEvent" },
                    { "$ref": "#/components/schemas/ResponseReasoningSummaryPartDoneEvent" }
                ],
                "discriminator": {
                    "propertyName": "type"
                }
            }
        }));

        let result = test_generation("x_stainless_discriminated", spec).expect("Generation failed");

        println!("Generated output:\n{}", result);

        // Test that ReasoningPart type field has correct enum value
        assert!(
            result.contains("pub enum ReasoningPartType"),
            "ReasoningPartType enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"summary_text\")]"),
            "ReasoningPartType should have summary_text variant with correct serde rename"
        );

        // CRITICAL BUG TEST: Event type enums should have correct values
        assert!(
            result.contains("pub enum ResponseReasoningSummaryPartAddedEventType"),
            "ResponseReasoningSummaryPartAddedEventType enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"response.reasoning_summary_part.added\")]"),
            "ResponseReasoningSummaryPartAddedEventType should have response.reasoning_summary_part.added variant"
        );

        assert!(
            result.contains("pub enum ResponseReasoningSummaryPartDoneEventType"),
            "ResponseReasoningSummaryPartDoneEventType enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"response.reasoning_summary_part.done\")]"),
            "ResponseReasoningSummaryPartDoneEventType should have response.reasoning_summary_part.done variant"
        );

        // Test discriminated union generation
        assert!(
            result.contains("pub enum EventUnion"),
            "EventUnion should be generated as discriminated union"
        );
        assert!(
            result.contains("#[serde(tag = \"type\")]"),
            "EventUnion should use type as discriminator tag"
        );
    }

    #[test]
    #[ignore = "Test designed to fail until generator enum collision bug is fixed - see investigation results"]
    fn test_actual_reasoning_item_missing_type_field() {
        // Test the ACTUAL ReasoningItem structure from OpenAI spec
        // This reproduces the missing main type field issue
        let spec = minimal_spec(json!({
            "ReasoningItem": {
                "description": "A description of the chain of thought used by a reasoning model while generating a response.",
                "type": "object",
                "properties": {
                    "encrypted_content": {
                        "description": "The encrypted content of the reasoning item - populated when a response is generated with `reasoning.encrypted_content` in the `include` parameter.",
                        "nullable": true,
                        "type": "string"
                    },
                    "id": {
                        "description": "The unique identifier of the reasoning content.",
                        "type": "string"
                    },
                    "status": {
                        "description": "The status of the item. One of `in_progress`, `completed`, or `incomplete`. Populated when items are returned via API.",
                        "enum": [
                            "in_progress",
                            "completed",
                            "incomplete"
                        ],
                        "type": "string"
                    },
                    "summary": {
                        "description": "Reasoning text contents.",
                        "items": {
                            "properties": {
                                "text": {
                                    "description": "A short summary of the reasoning used by the model when generating the response.",
                                    "type": "string"
                                },
                                "type": {
                                    "description": "The type of the object. Always `summary_text`.",
                                    "enum": [
                                        "summary_text"
                                    ],
                                    "type": "string",
                                    "x-stainless-const": true
                                }
                            },
                            "required": [
                                "type",
                                "text"
                            ],
                            "type": "object"
                        },
                        "type": "array"
                    },
                    "type": {
                        "description": "The type of the object. Always `reasoning`.",
                        "enum": [
                            "reasoning"
                        ],
                        "type": "string",
                        "x-stainless-const": true
                    }
                },
                "required": [
                    "id",
                    "summary",
                    "type"
                ],
                "title": "Reasoning",
                "type": "object"
            }
        }));

        let result = test_generation("actual_reasoning_item_missing_type_field", spec)
            .expect("Generation failed");

        println!("Generated output:\n{}", result);

        // CRITICAL BUG TEST: ReasoningItem should have a main type field with value "reasoning"
        assert!(
            result.contains("pub struct ReasoningItem"),
            "ReasoningItem struct should be generated"
        );
        assert!(
            result.contains("pub r#type: ReasoningItemMainType"),
            "ReasoningItem should have main type field"
        );
        assert!(
            result.contains("pub enum ReasoningItemMainType")
                || result.contains("pub enum ReasoningItemType"),
            "ReasoningItem main type enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"reasoning\")]"),
            "ReasoningItem main type should have 'reasoning' variant"
        );

        // Test that summary array item type is correct
        assert!(
            result.contains("ReasoningItemSummaryItemType")
                || result.contains("ReasoningItemSummaryItem"),
            "ReasoningItem summary item type should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"summary_text\")]"),
            "ReasoningItem summary item should have 'summary_text' variant"
        );
    }

    #[test]
    fn test_x_stainless_const_bug_reproduction() {
        let spec = minimal_spec(json!({
            "ReasoningItem": {
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the object. Always `summary_text`.",
                        "enum": ["summary_text"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "text": {
                        "type": "string"
                    }
                },
                "required": ["type", "text"]
            },
            "ResponseReasoningSummaryPartDoneEvent": {
                "description": "Emitted when a reasoning summary part is completed.",
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the event. Always `response.reasoning_summary_part.done`.",
                        "enum": ["response.reasoning_summary_part.done"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "item_id": {
                        "type": "string"
                    },
                    "part": {
                        "$ref": "#/components/schemas/ReasoningItem"
                    }
                },
                "required": ["type", "item_id", "part"]
            },
            "ResponseReasoningSummaryPartAddedEvent": {
                "description": "Emitted when a new reasoning summary part is added.",
                "type": "object",
                "properties": {
                    "type": {
                        "description": "The type of the event. Always `response.reasoning_summary_part.added`.",
                        "enum": ["response.reasoning_summary_part.added"],
                        "type": "string",
                        "x-stainless-const": true
                    },
                    "item_id": {
                        "type": "string"
                    },
                    "part": {
                        "$ref": "#/components/schemas/ReasoningItem"
                    }
                },
                "required": ["type", "item_id", "part"]
            },
            "EventUnion": {
                "oneOf": [
                    { "$ref": "#/components/schemas/ResponseReasoningSummaryPartDoneEvent" },
                    { "$ref": "#/components/schemas/ResponseReasoningSummaryPartAddedEvent" }
                ],
                "discriminator": {
                    "propertyName": "type"
                }
            }
        }));

        let result = test_generation("x_stainless_const_bug", spec).expect("Generation failed");

        println!("Generated output:\n{}", result);

        // Test that ReasoningItem type field has correct enum value
        assert!(
            result.contains("pub enum ReasoningItemType"),
            "ReasoningItemType enum should be generated"
        );
        assert!(
            result.contains("#[serde(rename = \"summary_text\")]"),
            "ReasoningItemType should have summary_text variant with correct serde rename"
        );

        // Test that event type fields have correct enum values (NOT summary_text)
        assert!(
            result.contains("pub enum ResponseReasoningSummaryPartDoneEventType"),
            "ResponseReasoningSummaryPartDoneEventType enum should be generated"
        );
        assert!(
            result.contains("pub enum ResponseReasoningSummaryPartAddedEventType"),
            "ResponseReasoningSummaryPartAddedEventType enum should be generated"
        );

        // CRITICAL BUG TEST: These should NOT be "summary_text"
        assert!(
            !result.contains("ResponseReasoningSummaryPartDoneEventType")
                || !result.contains("#[serde(rename = \"summary_text\")]")
                || result.contains("#[serde(rename = \"response.reasoning_summary_part.done\")]"),
            "ResponseReasoningSummaryPartDoneEventType should NOT have summary_text - should have response.reasoning_summary_part.done"
        );
        assert!(
            !result.contains("ResponseReasoningSummaryPartAddedEventType")
                || !result.contains("#[serde(rename = \"summary_text\")]")
                || result.contains("#[serde(rename = \"response.reasoning_summary_part.added\")]"),
            "ResponseReasoningSummaryPartAddedEventType should NOT have summary_text - should have response.reasoning_summary_part.added"
        );

        // Test discriminated union generation
        assert!(
            result.contains("pub enum EventUnion"),
            "EventUnion should be generated as discriminated union"
        );
        assert!(
            result.contains("#[serde(tag = \"type\")]"),
            "EventUnion should use type as discriminator tag"
        );
    }
}