Skip to content

Commit b5e56ac

Browse files
committed
fix(openapi): Try to resolve the reference and serialize the resolved value
1 parent 0390faa commit b5e56ac

File tree

1 file changed

+24
-7
lines changed

1 file changed

+24
-7
lines changed

xdk-openapi/src/reference.rs

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,20 +63,37 @@ impl<T: Clone + Any> RefOrValue<T> {
6363
}
6464
}
6565
// Custom Serialize for RefOrValue
66-
// For references, we serialize both the $ref and resolved value so templates have both
66+
// For references, we serialize both the $ref path AND try to include resolved properties
67+
// so templates can access either the reference name or the actual schema fields
6768
impl<T: Clone + Serialize + Any> Serialize for RefOrValue<T> {
6869
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
6970
where
7071
S: Serializer,
7172
{
7273
match self {
7374
RefOrValue::Reference { path, .. } => {
74-
// Preserve the reference path so templates can extract type names
75-
// Templates will use the schema name from the $ref path
76-
use serde::ser::SerializeMap;
77-
let mut map = serializer.serialize_map(Some(1))?;
78-
map.serialize_entry("$ref", path)?;
79-
map.end()
75+
// Try to resolve the reference and serialize the resolved value
76+
// This allows templates to access the actual schema properties
77+
if let Ok(resolved) = self.try_resolve() {
78+
// Serialize the resolved value with $ref path included
79+
// First serialize the resolved value to a serde_json::Value
80+
let resolved_json = serde_json::to_value(resolved.as_ref())
81+
.map_err(serde::ser::Error::custom)?;
82+
83+
if let serde_json::Value::Object(mut obj) = resolved_json {
84+
// Add the $ref path to the object so templates can still extract type names
85+
obj.insert("$ref".to_string(), serde_json::Value::String(path.clone()));
86+
return obj.serialize(serializer);
87+
}
88+
89+
// If not an object, just serialize the resolved value
90+
resolved.serialize(serializer)
91+
} else {
92+
// Fallback: just serialize the $ref path as an object
93+
let mut obj = serde_json::Map::new();
94+
obj.insert("$ref".to_string(), serde_json::Value::String(path.clone()));
95+
serde_json::Value::Object(obj).serialize(serializer)
96+
}
8097
}
8198
RefOrValue::Value(value) => value.serialize(serializer),
8299
}

0 commit comments

Comments
 (0)