Skip to content
Open
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_parser_round41.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"crates/vespera/Cargo.toml":"Patch","crates/vespera_core/Cargo.toml":"Patch","crates/vespera_macro/Cargo.toml":"Patch","crates/vespera_inprocess/Cargo.toml":"Patch","crates/vespera_jni/Cargo.toml":"Patch","libs/vespera-bridge/build.gradle.kts":"Patch","libs/vespera-bridge-gradle-plugin/build.gradle.kts":"Patch"},"note":"schema parser fixes: a raw string literal (r\"...\" / r#\"...\"#) as a serde default or rename value is read on the raw-token fallback path too; crate::Schema, self::Schema and super::Schema name the bare Schema component instead of a CrateSchema / SelfSchema / SuperSchema component.","date":"2026-11-24T00:00:00.000Z"}
70 changes: 59 additions & 11 deletions crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,49 @@ pub(super) fn quoted_value_after_key(tokens: &str, key: &str) -> Option<String>
None
}

/// The value of the `"..."` literal `value_part` starts with (after
/// whitespace); a value of any other kind yields `None`. The closing quote is
/// the first one not escaped by a backslash, and the literal's escapes
/// (`\"`, `\\`, `\u{..}`) are decoded by `syn`, as rustc decodes them.
/// The value of the string literal `value_part` starts with (after
/// whitespace), plain (`"..."`) or raw (`r"..."`, `r#"..."#`); a value of any
/// other kind yields `None`. A plain literal's closing quote is the first one
/// not escaped by a backslash, a raw literal's is the quote followed by the
/// same number of `#`. The literal is decoded by `syn`, as rustc decodes it.
fn leading_string_literal(value_part: &str) -> Option<String> {
let body = value_part.trim_start().strip_prefix('"')?;
let text = value_part.trim_start();
let end = if text.starts_with('"') {
quoted_literal_end(text)?
} else {
raw_literal_end(text)?
};
syn::parse_str::<syn::LitStr>(&text[..end])
.ok()
.map(|literal| literal.value())
}

/// Byte index just past the plain literal `text` (which starts with `"`).
fn quoted_literal_end(text: &str) -> Option<usize> {
let mut escaped = false;
let end = body.char_indices().find_map(|(index, ch)| {
text.char_indices().skip(1).find_map(|(index, ch)| {
if escaped {
escaped = false;
None
} else if ch == '\\' {
escaped = true;
None
} else {
(ch == '"').then_some(index)
(ch == '"').then_some(index + 1)
}
})?;
syn::parse_str::<syn::LitStr>(&format!("\"{}\"", &body[..end]))
.ok()
.map(|literal| literal.value())
})
}

/// Byte index just past the raw literal `text` (`r` + `#`s + `"` ... `"` +
/// the same `#`s); `None` when `text` opens no raw literal.
fn raw_literal_end(text: &str) -> Option<usize> {
let after_r = text.strip_prefix('r')?;
let hashes = after_r.len() - after_r.trim_start_matches('#').len();
let body = after_r[hashes..].strip_prefix('"')?;
let closing = format!("\"{}", "#".repeat(hashes));
let close = body.find(&closing)?;
// `r` + hashes + `"` + body + closing
Some(1 + hashes + 1 + close + closing.len())
}

pub(super) fn contains_standalone_word(tokens: &str, word: &str) -> bool {
Expand Down Expand Up @@ -194,6 +216,32 @@ mod tests {
);
}

/// The raw-token fallback reads a raw string literal value (`r"..."`,
/// `r#"..."#`) the same as a plain one, for `default` and `rename`.
#[rstest]
#[case::plain("\"f\"", "f")]
#[case::raw("r\"f\"", "f")]
#[case::raw_hashed("r#\"f\"#", "f")]
#[case::raw_keeps_backslash("r#\"a\\b\"#", "a\\b")]
fn test_fallback_reads_raw_and_plain_literal_values(
#[case] literal: &str,
#[case] value: &str,
) {
let tokens: TokenStream = format!("@bogus, default = {literal}, rename = {literal}")
.parse()
.expect("token stream parses");
let attr = create_attr_with_raw_tokens(tokens);
assert_eq!(
extract_default(std::slice::from_ref(&attr)),
Some(Some(value.to_string()))
);
assert_eq!(
extract_field_rename(&[attr]).as_deref(),
Some(value),
"rename through the fallback"
);
}

/// Test that a "default" occurring as part of a longer identifier is not
/// a serde default: `scan_default_from_raw_tokens` skips it (the
/// `key_positions` candidates must be whole keys), so the scan finds
Expand Down
17 changes: 15 additions & 2 deletions crates/vespera_macro/src/parser/schema/type_schema/conversion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,10 +597,21 @@ fn parse_type_impl(
// `module::Schema` names its module-scoped key when one is registered:
// a renamed `Schema` may share its name with an unrelated component.
// Only without a registered key is the convention tried, a schema
// named `ModuleSchema` (or `moduleSchema`).
// named `ModuleSchema` (or `moduleSchema`). A path keyword parent
// (`crate::Schema`, `self::Schema`, `super::Schema`) is no module
// name, so it never selects a `CrateSchema`-style component.
let resolved_name = match scoped_key() {
Some(key) => key,
None if type_name == "Schema" && path.segments.len() > 1 => {
None if type_name == "Schema"
&& path.segments.len() > 1
&& !matches!(
path.segments[path.segments.len() - 2]
.ident
.to_string()
.as_str(),
"crate" | "self" | "super"
) =>
{
// The parent module name (e.g., "user" from "crate::models::user::Schema")
let parent_name = path.segments[path.segments.len() - 2]
.ident
Expand Down Expand Up @@ -929,3 +940,5 @@ mod tests_arms;
mod tests_const;
#[cfg(test)]
mod tests_instances;
#[cfg(test)]
mod tests_path_keyword;
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//! `crate::Schema` / `self::Schema` / `super::Schema` (sidecar of
//! `conversion`; `use super::*` semantics preserved): a path keyword is no
//! module name, so the `<module>Schema` convention never picks a component
//! named after the keyword.

use rstest::rstest;

use super::*;

/// The bare `Schema` and a component named `CrateSchema` (`SelfSchema` /
/// `SuperSchema`) are both known; a keyword-qualified `Schema` names the bare
/// one, never the convention's `<Keyword>Schema` component.
#[rstest]
#[case::crate_root("crate::Schema", "CrateSchema")]
#[case::self_module("self::Schema", "SelfSchema")]
#[case::super_module("super::Schema", "SuperSchema")]
fn path_keyword_parent_names_the_bare_schema(#[case] ty_src: &str, #[case] other: &str) {
let known = HashSet::from(["Schema", other]);
let defs: HashMap<String, String> = HashMap::new();
let ty: Type = syn::parse_str(ty_src).unwrap();
let schema_ref = parse_type_to_schema_ref(&ty, &known, &defs);
let SchemaRef::Ref(reference) = schema_ref else {
panic!("a $ref for {ty_src}: {schema_ref:?}");
};
assert_eq!(reference.ref_path, "#/components/schemas/Schema");
}
Loading