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
#[cfg(test)]
mod tests {
use openapi_to_rust::test_helpers::*;
use serde_json::json;
#[test]
fn test_const_enum_in_object_property() {
// This test reproduces the exact issue with the role field in Message
let spec = minimal_spec(json!({
"Message": {
"type": "object",
"title": "Message",
"properties": {
"content": {
"description": "The content of the message.",
"title": "Content",
"oneOf": [
{
"type": "string",
"title": "Text content",
"description": "Text content of the message."
},
{
"type": "array",
"title": "Array of content blocks",
"items": {
"type": "object"
}
}
]
},
"id": {
"type": "string",
"description": "Unique object identifier."
},
"role": {
"const": "assistant",
"default": "assistant",
"description": "Conversational role of the generated message.\n\nThis will always be `\"assistant\"`.",
"enum": ["assistant"],
"title": "Role",
"type": "string"
},
"model": {
"type": "string",
"description": "The model that was used to generate the message."
}
},
"required": ["content", "id", "role", "model"]
}
}));
let result = test_generation("const_enum_property", spec).expect("Generation failed");
// Assert that MessageRole enum was generated for the role property
assert!(
result.contains("pub enum MessageRole"),
"MessageRole enum should be generated for role property with const+enum"
);
// Assert that the role field uses the enum type, not String
assert!(
!result.contains("pub role: String"),
"role property should NOT be String when it has const+enum values"
);
assert!(
result.contains("pub role: MessageRole"),
"role property should use MessageRole enum type"
);
// Verify the enum has the correct variant
assert!(
result.contains("#[serde(rename = \"assistant\")]"),
"MessageRole enum should have assistant variant"
);
assert!(
result.contains("Assistant"),
"MessageRole enum should have Assistant variant"
);
// Verify that it's marked as the default variant since it has a default value
assert!(
result.contains("#[default]"),
"MessageRole enum should have a default variant since the property has default: 'assistant'"
);
// Verify the struct has the proper serde attribute for the default
assert!(
result.contains("#[serde(default)]"),
"role field should have serde(default) attribute"
);
// Verify other fields are generated correctly
assert!(
result.contains("pub id: String"),
"id field should be String"
);
assert!(
result.contains("pub model: String"),
"model field should be String"
);
assert!(
result.contains("pub content: "),
"content field should exist"
);
}
}