Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_cgh9Sk7aKVb-x_Vv9J4Pv.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespera_macro/Cargo.toml":"Patch","crates/vespera_core/Cargo.toml":"Patch","crates/vespera/Cargo.toml":"Patch"},"note":"Fix struct in option issue","date":"2025-12-12T07:26:22.869740900Z"}
6 changes: 3 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 35 additions & 3 deletions crates/vespera_macro/src/parser/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,20 @@ pub(super) fn parse_type_to_schema_ref_with_schemas(
return SchemaRef::Inline(Box::new(Schema::array(inner_schema)));
} else {
// Option<T> -> nullable schema
if let SchemaRef::Inline(mut schema) = inner_schema {
schema.nullable = Some(true);
return SchemaRef::Inline(schema);
match inner_schema {
SchemaRef::Inline(mut schema) => {
schema.nullable = Some(true);
return SchemaRef::Inline(schema);
}
SchemaRef::Ref(reference) => {
// Wrap reference in an inline schema to attach nullable flag
return SchemaRef::Inline(Box::new(Schema {
ref_path: Some(reference.ref_path),
schema_type: None,
nullable: Some(true),
..Schema::new(SchemaType::Object)
}));
}
}
}
}
Expand Down Expand Up @@ -919,6 +930,27 @@ mod tests {
}
}

#[test]
fn test_parse_type_to_schema_ref_option_ref_nullable() {
let mut known = HashMap::new();
known.insert("User".to_string(), "struct User;".to_string());

let ty: syn::Type = syn::parse_str("Option<User>").unwrap();
let schema_ref = parse_type_to_schema_ref(&ty, &known, &HashMap::new());

match schema_ref {
SchemaRef::Inline(schema) => {
assert_eq!(
schema.ref_path,
Some("#/components/schemas/User".to_string())
);
assert_eq!(schema.nullable, Some(true));
assert_eq!(schema.schema_type, None);
}
_ => panic!("Expected inline schema for Option<User>"),
}
}

#[rstest]
#[case(
r#"
Expand Down
53 changes: 52 additions & 1 deletion examples/axum-example/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,17 @@
"value2"
]
},
"InSkipResponse": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
},
"MapQuery": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1563,6 +1574,44 @@
"type": "string",
"default": "default42"
},
"in_skip": {
"$ref": "#/components/schemas/InSkipResponse"
},
"in_skip2": {
"$ref": "#/components/schemas/InSkipResponse",
"nullable": true
},
"in_skip3": {
"type": "array",
"items": {
"$ref": "#/components/schemas/InSkipResponse"
}
},
"in_skip4": {
"type": "array",
"items": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"in_skip5": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"in_skip6": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"name": {
"type": "string"
},
Expand All @@ -1572,7 +1621,9 @@
}
},
"required": [
"name"
"name",
"in_skip",
"in_skip3"
]
},
"StructBody": {
Expand Down
32 changes: 32 additions & 0 deletions examples/axum-example/src/routes/users.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::{BTreeMap, HashMap};

use serde::{Deserialize, Serialize};
use vespera::{
Schema,
Expand Down Expand Up @@ -84,6 +86,18 @@ pub struct SkipResponse {

#[serde(rename = "num", default)]
pub num: i32,

pub in_skip: InSkipResponse,
pub in_skip2: Option<InSkipResponse>,
pub in_skip3: Vec<InSkipResponse>,
pub in_skip4: Option<Vec<InSkipResponse>>,
pub in_skip5: Option<HashMap<String, InSkipResponse>>,
pub in_skip6: Option<BTreeMap<String, InSkipResponse>>,
}

#[derive(Serialize, Deserialize, Schema)]
pub struct InSkipResponse {
pub name: String,
}

fn default_value() -> String {
Expand All @@ -102,5 +116,23 @@ pub async fn skip_response() -> Json<SkipResponse> {
email6: "[email protected]".to_string(),
email7: "[email protected]".to_string(),
num: 0,
in_skip: InSkipResponse {
name: "John Doe".to_string(),
},
in_skip2: Some(InSkipResponse {
name: "John Doe".to_string(),
}),
in_skip3: vec![InSkipResponse {
name: "John Doe".to_string(),
}],
in_skip4: Some(vec![InSkipResponse {
name: "John Doe".to_string(),
}]),
in_skip5: Some(HashMap::from([("John Doe".to_string(), InSkipResponse {
name: "John Doe".to_string(),
})]),),
in_skip6: Some(BTreeMap::from([("John Doe".to_string(), InSkipResponse {
name: "John Doe".to_string(),
})]),),
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -1471,6 +1471,17 @@ expression: "std::fs::read_to_string(\"openapi.json\").unwrap()"
"value2"
]
},
"InSkipResponse": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
},
"MapQuery": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1567,6 +1578,44 @@ expression: "std::fs::read_to_string(\"openapi.json\").unwrap()"
"type": "string",
"default": "default42"
},
"in_skip": {
"$ref": "#/components/schemas/InSkipResponse"
},
"in_skip2": {
"$ref": "#/components/schemas/InSkipResponse",
"nullable": true
},
"in_skip3": {
"type": "array",
"items": {
"$ref": "#/components/schemas/InSkipResponse"
}
},
"in_skip4": {
"type": "array",
"items": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"in_skip5": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"in_skip6": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"name": {
"type": "string"
},
Expand All @@ -1576,7 +1625,9 @@ expression: "std::fs::read_to_string(\"openapi.json\").unwrap()"
}
},
"required": [
"name"
"name",
"in_skip",
"in_skip3"
]
},
"StructBody": {
Expand Down
53 changes: 52 additions & 1 deletion openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,17 @@
"value2"
]
},
"InSkipResponse": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": [
"name"
]
},
"MapQuery": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -1563,6 +1574,44 @@
"type": "string",
"default": "default42"
},
"in_skip": {
"$ref": "#/components/schemas/InSkipResponse"
},
"in_skip2": {
"$ref": "#/components/schemas/InSkipResponse",
"nullable": true
},
"in_skip3": {
"type": "array",
"items": {
"$ref": "#/components/schemas/InSkipResponse"
}
},
"in_skip4": {
"type": "array",
"items": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"in_skip5": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"in_skip6": {
"type": "object",
"properties": {},
"required": [],
"additionalProperties": {
"$ref": "#/components/schemas/InSkipResponse"
},
"nullable": true
},
"name": {
"type": "string"
},
Expand All @@ -1572,7 +1621,9 @@
}
},
"required": [
"name"
"name",
"in_skip",
"in_skip3"
]
},
"StructBody": {
Expand Down