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
#[cfg(test)]
mod tests {
use openapi_to_rust::test_helpers::*;
use serde_json::json;
#[test]
fn test_create_response_input_field_properly_typed() {
// Test that CreateResponse.input field generates a proper union type, not serde_json::Value
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"CreateResponse": {
"allOf": [
{
"$ref": "#/components/schemas/BaseProperties"
},
{
"type": "object",
"properties": {
"input": {
"description": "Text or array inputs to the model",
"oneOf": [
{
"type": "string",
"description": "A text input",
"title": "Text input"
},
{
"type": "array",
"description": "A list of input items",
"title": "Input item list",
"items": {
"$ref": "#/components/schemas/InputItem"
}
}
]
}
},
"required": ["input"]
}
]
},
"BaseProperties": {
"type": "object",
"properties": {
"model": {
"type": "string"
}
},
"required": ["model"]
},
"InputItem": {
"type": "object",
"properties": {
"type": {
"type": "string",
"enum": ["text", "image"]
},
"content": {
"type": "string"
}
},
"required": ["type", "content"]
}
}
}
});
let result =
test_generation("create_response_input_typing", spec).expect("Generation failed");
// Check that CreateResponse was generated
assert!(
result.contains("pub struct CreateResponse"),
"Should generate CreateResponse struct"
);
// Check that input field is NOT serde_json::Value
assert!(
!result.contains("pub input: serde_json::Value"),
"Input field should NOT be serde_json::Value"
);
// Check that input field has a proper union type
assert!(
result.contains("pub input: CreateResponseInput"),
"Input field should use a proper union type CreateResponseInput"
);
// Check that the union enum was generated
assert!(
result.contains("pub enum CreateResponseInput"),
"Should generate CreateResponseInput enum"
);
// Check the union has the expected variants
assert!(
result.contains("#[serde(untagged)]"),
"Union should be untagged"
);
assert!(
result.contains("String(String)") || result.contains("TextInput(String)"),
"Should have string variant"
);
assert!(
result.contains("InputItemList(Vec<InputItem>)")
|| result.contains("Array(Vec<InputItem>)"),
"Should have array variant"
);
}
#[test]
fn test_nested_allof_with_oneof_property() {
// Test the exact pattern from OpenAI fixture
let spec = json!({
"openapi": "3.1.0",
"info": {"title": "Test", "version": "1.0"},
"components": {
"schemas": {
"CreateResponse": {
"allOf": [
{
"$ref": "#/components/schemas/CreateModelResponseProperties"
},
{
"$ref": "#/components/schemas/ResponseProperties"
},
{
"properties": {
"input": {
"description": "Text, image, or file inputs to the model",
"oneOf": [
{
"description": "A text input",
"title": "Text input",
"type": "string"
},
{
"description": "A list of input items",
"items": {
"$ref": "#/components/schemas/InputItem"
},
"title": "Input item list",
"type": "array"
}
]
}
},
"required": ["model", "input"],
"type": "object"
}
]
},
"CreateModelResponseProperties": {
"type": "object",
"properties": {
"model": {
"type": "string"
}
},
"required": ["model"]
},
"ResponseProperties": {
"type": "object",
"properties": {
"temperature": {
"type": "number",
"nullable": true
}
}
},
"InputItem": {
"type": "object",
"properties": {
"type": {
"type": "string"
},
"content": {
"type": "string"
}
},
"required": ["type", "content"]
}
}
}
});
let result = test_generation("nested_allof_oneof", spec).expect("Generation failed");
// The same assertions as above
assert!(result.contains("pub struct CreateResponse"));
assert!(
!result.contains("pub input: serde_json::Value"),
"Input field should NOT be serde_json::Value - it should be properly typed"
);
assert!(
result.contains("pub input: CreateResponseInput"),
"Input field should use CreateResponseInput union type"
);
assert!(result.contains("pub enum CreateResponseInput"));
}
}