From 8d0d889a9dee623e61ff731a4f461676927f9e0e Mon Sep 17 00:00:00 2001 From: Jeong Min Oh Date: Thu, 19 Nov 2026 23:54:17 +0900 Subject: [PATCH] Fix parser round-30 findings (alias args, error headers, flatten, generics) Return-type aliases: - A const argument written as a bare identifier (Page for type Page) pairs with its const parameter and no longer counts as a type argument, so the alias still expands. Responses: - An error response documents the HeaderMap headers the error sets (bare or in the tuple) on its status, as the success side does. Snapshots for the error-header cases now show "headers": {}. Flatten: - The module doc states the limit: with an opaque member that keeps the object open, the flattened map's value schema is not emitted (JSON Schema additionalProperties cannot see the member's keys). Nothing is rejected. - Test names and comments in serde_attrs (tag/content malformed values, default substring) describe what they test. Generic instances and inlining: - A const argument of a nested type is part of its component name (Tree> and Tree> no longer share Tree_Buf_u8). - A const-only definition (Matrix<3>) inlines with its length. - A const parameter named in value_type / into / from / try_from is pinned by the instantiation like one named in a field type. - value_type wins over #[schema(any)] on the same field, as the wire_override module doc says; SchemaConstraints::any_decides is the one check every any-suppression site uses. Tests: every behaviour change has a test that fails without it (alias const pairing rstest cases, error header snapshots, nested const injectivity and recursive component tests, const-only inline, value_type attribute length, value_type over any). The vacuous contains('B') in aliased_non_schema_type_import_is_flagged now asserts `Query`. Co-Authored-By: Claude Opus 5.5 --- .../changepack_log_parser_round30.json | 1 + .../src/parser/extractor_validation.rs | 2 +- .../src/parser/parameters/shared.rs | 2 +- crates/vespera_macro/src/parser/response.rs | 14 +++- ...s__response_parts_err_header_map_json.snap | 3 +- ..._parts__response_parts_err_no_content.snap | 4 +- ..._response_parts_err_status_header_map.snap | 5 +- ...s__response_parts_result_nested_tuple.snap | 4 +- .../src/parser/result_alias/expansion.rs | 37 +++++++-- .../src/parser/result_alias/tests.rs | 27 +++++++ .../src/parser/schema/schema_attrs.rs | 13 ++- .../parser/schema/serde_attrs/enum_repr.rs | 7 +- .../src/parser/schema/serde_attrs/fallback.rs | 15 ++-- .../src/parser/schema/struct_schema.rs | 4 +- .../parser/schema/struct_schema/flatten.rs | 23 ++++-- .../struct_schema/tests_serde_fidelity.rs | 3 + .../parser/schema/type_schema/conversion.rs | 8 +- .../type_schema/conversion/schema_def.rs | 55 +++++++------ .../type_schema/conversion/tests_const.rs | 80 +++++++++++++++++++ .../schema/type_schema/generic_instances.rs | 19 +++++ .../src/parser/schema/wire_override.rs | 31 ++++++- 21 files changed, 285 insertions(+), 72 deletions(-) create mode 100644 .changepacks/changepack_log_parser_round30.json diff --git a/.changepacks/changepack_log_parser_round30.json b/.changepacks/changepack_log_parser_round30.json new file mode 100644 index 000000000..1a51da241 --- /dev/null +++ b/.changepacks/changepack_log_parser_round30.json @@ -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":"Parser round 30: a const generic argument of a return-type alias pairs with its const parameter (type Page); error responses document their HeaderMap headers like the success side; a const argument of a nested generic type is part of its component name (Tree> vs Tree>); a const-only generic definition inlines with its length; a const parameter named in value_type / into attributes is pinned by the instantiation; value_type wins over schema(any) on the same field; flatten module docs state the flattened map limit with an opaque member; test name and assertion fixes.","date":"2026-11-19T00:00:00.000Z"} diff --git a/crates/vespera_macro/src/parser/extractor_validation.rs b/crates/vespera_macro/src/parser/extractor_validation.rs index ee40ef9e0..705e55843 100644 --- a/crates/vespera_macro/src/parser/extractor_validation.rs +++ b/crates/vespera_macro/src/parser/extractor_validation.rs @@ -784,7 +784,7 @@ mod tests { &[], ) .expect_err("should flag aliased non-Schema import"); - assert!(err.to_string().contains('B'), "{err}"); + assert!(err.to_string().contains("`Query`"), "{err}"); } #[test] diff --git a/crates/vespera_macro/src/parser/parameters/shared.rs b/crates/vespera_macro/src/parser/parameters/shared.rs index 205e6120b..871de4790 100644 --- a/crates/vespera_macro/src/parser/parameters/shared.rs +++ b/crates/vespera_macro/src/parser/parameters/shared.rs @@ -185,7 +185,7 @@ pub(super) fn newtype_source(struct_item: &syn::ItemStruct) -> Option { let constraints = extract_schema_constraints(&field.attrs); if flags.skip || flags.skip_deserializing - || constraints.is_any() + || constraints.any_decides() || extract_field_codec(&field.attrs).custom_for(SerdeSide::Deserialize) { return None; diff --git a/crates/vespera_macro/src/parser/response.rs b/crates/vespera_macro/src/parser/response.rs index 82ef03bcf..0da9b01cb 100644 --- a/crates/vespera_macro/src/parser/response.rs +++ b/crates/vespera_macro/src/parser/response.rs @@ -495,8 +495,10 @@ pub fn parse_return_type( /// /// The error body is found like the success one: `(StatusCode, Json)`, /// `(HeaderMap, Json)` and header arrays document `E`; a metadata-only -/// error (`StatusCode`, `(StatusCode, HeaderMap)`) has no content. The -/// default error status is `400`; an error that is a `Redirect` / +/// error (`StatusCode`, `(StatusCode, HeaderMap)`) has no content. A +/// `HeaderMap` the error sets (bare or in the tuple) documents `headers` on +/// its status, as the success side does. The default error status is `400`; +/// an error that is a `Redirect` / /// `NoContent` is keyed like the success side (`3XX` / `204`), never /// overwriting the success entry. An error documented without a body (an /// outer `StatusCode`) takes the body of a later (inner) one, and several @@ -515,6 +517,10 @@ fn insert_error_response( "200" => "400", key => key, }; + let shadows = Shadows { + known_schemas, + struct_definitions, + }; let response = responses .entry(key.to_string()) .or_insert_with(|| Response { @@ -530,6 +536,10 @@ fn insert_error_response( content: None, ..Default::default() }); + response.headers = response + .headers + .take() + .or_else(|| has_header_map(err_ty, shadows).then(BTreeMap::new)); let Some(content) = response_content(err_ty, known_schemas, struct_definitions) else { return; }; diff --git a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_header_map_json.snap b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_header_map_json.snap index aa9108f7f..0ea67f537 100644 --- a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_header_map_json.snap +++ b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_header_map_json.snap @@ -1,6 +1,6 @@ --- source: crates/vespera_macro/src/parser/response/tests_parts.rs -assertion_line: 63 +assertion_line: 88 expression: responses_json(return_type) --- { @@ -16,6 +16,7 @@ expression: responses_json(return_type) }, "400": { "description": "Error response", + "headers": {}, "content": { "application/json": { "schema": { diff --git a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_no_content.snap b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_no_content.snap index 67e815df3..3022c6243 100644 --- a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_no_content.snap +++ b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_no_content.snap @@ -1,5 +1,6 @@ --- source: crates/vespera_macro/src/parser/response/tests_parts.rs +assertion_line: 88 expression: responses_json(return_type) --- { @@ -14,6 +15,7 @@ expression: responses_json(return_type) } }, "204": { - "description": "Successful response" + "description": "Successful response", + "headers": {} } } diff --git a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_status_header_map.snap b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_status_header_map.snap index 4777d183d..370183659 100644 --- a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_status_header_map.snap +++ b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_err_status_header_map.snap @@ -1,6 +1,6 @@ --- source: crates/vespera_macro/src/parser/response/tests_parts.rs -assertion_line: 63 +assertion_line: 88 expression: responses_json(return_type) --- { @@ -15,6 +15,7 @@ expression: responses_json(return_type) } }, "400": { - "description": "Error response" + "description": "Error response", + "headers": {} } } diff --git a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_result_nested_tuple.snap b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_result_nested_tuple.snap index 760ec5207..575f2be7c 100644 --- a/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_result_nested_tuple.snap +++ b/crates/vespera_macro/src/parser/response/snapshots/vespera_macro__parser__response__tests_parts__response_parts_result_nested_tuple.snap @@ -1,5 +1,6 @@ --- source: crates/vespera_macro/src/parser/response/tests_parts.rs +assertion_line: 88 expression: responses_json(return_type) --- { @@ -14,6 +15,7 @@ expression: responses_json(return_type) } }, "400": { - "description": "Error response" + "description": "Error response", + "headers": {} } } diff --git a/crates/vespera_macro/src/parser/result_alias/expansion.rs b/crates/vespera_macro/src/parser/result_alias/expansion.rs index 0114ad9d7..acb247d95 100644 --- a/crates/vespera_macro/src/parser/result_alias/expansion.rs +++ b/crates/vespera_macro/src/parser/result_alias/expansion.rs @@ -130,14 +130,7 @@ fn expand_alias( } let segment = last_path_segment(ty)?; let mut args: Vec = match &segment.arguments { - syn::PathArguments::AngleBracketed(args) => args - .args - .iter() - .filter_map(|arg| match arg { - syn::GenericArgument::Type(ty) => Some(ty.clone()), - _ => None, - }) - .collect(), + syn::PathArguments::AngleBracketed(args) => type_arguments(&alias.generics, args), // `None` (and the `Fn(..)` sugar, which never reaches a return type). _ => Vec::new(), }; @@ -195,3 +188,31 @@ fn expand_alias( let concrete: Vec<&Type> = values.iter().collect(); Some(substitute_type(&body, &names, &concrete)) } + +/// The type arguments of a use site that fill the type parameters of an +/// alias with `generics` (`Page` for `type Page`: `[User]`). Lifetimes are skipped on both sides and the rest +/// are paired by position, as rustc pairs them. An argument paired with a +/// const parameter is a value even when it parses as a type (a bare +/// identifier such as `LIMIT`), so it is not a type argument. This mirrors +/// `type_argument_types` in `schema/type_schema/conversion/schema_def.rs`. +fn type_arguments( + generics: &syn::Generics, + args: &syn::AngleBracketedGenericArguments, +) -> Vec { + let mut params = generics + .params + .iter() + .filter(|param| !matches!(param, syn::GenericParam::Lifetime(_))); + args.args + .iter() + .filter(|arg| !matches!(arg, syn::GenericArgument::Lifetime(_))) + .filter_map(|arg| { + let param = params.next(); + let syn::GenericArgument::Type(ty) = arg else { + return None; + }; + (!matches!(param, Some(syn::GenericParam::Const(_)))).then(|| ty.clone()) + }) + .collect() +} diff --git a/crates/vespera_macro/src/parser/result_alias/tests.rs b/crates/vespera_macro/src/parser/result_alias/tests.rs index cb1f96e68..8aab7f973 100644 --- a/crates/vespera_macro/src/parser/result_alias/tests.rs +++ b/crates/vespera_macro/src/parser/result_alias/tests.rs @@ -83,6 +83,33 @@ fn expand(file_src: &str, ret: &str) -> String { #[case::wrapper_untouched("type Json = Bad;", "-> Json", "-> Json < u8 >")] #[case::too_many_args("type R = Result;", "-> R", "-> R < A , B >")] #[case::missing_arg_without_default("type R = Result;", "-> R", "-> R < A >")] +// A const parameter takes its argument's slot, even when the argument is a +// bare identifier that parses as a type: `LIMIT` is a value, `User` the `T`. +#[case::bare_const_argument_takes_const_slot( + "type Page = Json>;", + "-> Page", + "-> Json < Vec < User > >" +)] +#[case::literal_const_argument_takes_const_slot( + "type Page = Json>;", + "-> Page<3, User>", + "-> Json < Vec < User > >" +)] +#[case::lifetime_before_const_argument( + "type Page<'a, const MAX: usize, T> = Json>;", + "-> Page<'static, LIMIT, User>", + "-> Json < Vec < User > >" +)] +#[case::const_parameter_after_type_parameter( + "type Page = Json>;", + "-> Page", + "-> Json < Vec < User > >" +)] +#[case::extra_type_argument_after_const_slot_still_counts( + "type Page = Json>;", + "-> Page", + "-> Page < LIMIT , A , B >" +)] #[case::unknown_alias("", "-> ApiResult", "-> ApiResult < User >")] #[case::self_recursive_stops("type Result = Result;", "-> Result", "-> Result < u8 >")] #[case::cycle_back_to_start("type A = B; type B = A;", "-> A", "-> A")] diff --git a/crates/vespera_macro/src/parser/schema/schema_attrs.rs b/crates/vespera_macro/src/parser/schema/schema_attrs.rs index e10dd3b76..0c8b4218d 100644 --- a/crates/vespera_macro/src/parser/schema/schema_attrs.rs +++ b/crates/vespera_macro/src/parser/schema/schema_attrs.rs @@ -91,7 +91,8 @@ pub struct SchemaConstraints { /// "every-custom-type-must-derive-Schema" checking when the field /// is intentionally arbitrary / opaque JSON. Bare form /// `#[schema(any)]` sets it to `true`; an explicit - /// `#[schema(any = false)]` is also accepted. + /// `#[schema(any = false)]` is also accepted. A `value_type` on the same + /// field wins over it (see [`SchemaConstraints::any_decides`]). pub any: Option, /// `#[schema(value_type = T)]`: document the field (or, on a container, @@ -161,6 +162,16 @@ impl SchemaConstraints { self.any == Some(true) } + /// `true` when `#[schema(any)]` decides the field's schema: it is set and + /// no `value_type` overrides it. The `value_type` wins over `any` (the + /// module doc of `wire_override`), so every check that `any` suppresses + /// (the emitted schema, the garde rules, the leaf assertion, the + /// parameter, the flattened member) asks this instead of [`Self::is_any`]. + #[must_use] + pub fn any_decides(&self) -> bool { + self.is_any() && self.value_type.is_none() + } + /// `true` when at least one constraint produces a `garde` runtime rule /// (excludes pure-OpenAPI annotations such as `example` / `read_only` / /// `write_only` / `unique_items` / `exclusive_minimum` / diff --git a/crates/vespera_macro/src/parser/schema/serde_attrs/enum_repr.rs b/crates/vespera_macro/src/parser/schema/serde_attrs/enum_repr.rs index f0f869fff..9a273bac8 100644 --- a/crates/vespera_macro/src/parser/schema/serde_attrs/enum_repr.rs +++ b/crates/vespera_macro/src/parser/schema/serde_attrs/enum_repr.rs @@ -265,7 +265,8 @@ mod tests { } // `extract_tag` / `extract_content` have no raw-token fallback: the - // structured walk matches a qualified key by its last segment. + // structured walk matches a qualified key by its last segment, and the + // malformed-value tests below pin what that walk returns. /// A qualified `tag` key is matched by its last segment. #[test] @@ -466,7 +467,7 @@ mod tests { #[case(r#"qualified::tag = "type""#, Some("type"))] #[case("qualified::tag = value", None)] #[case("qualified::tag", None)] - fn malformed_tag_fallback_is_exact(#[case] tokens: &str, #[case] expected: Option<&str>) { + fn malformed_tag_is_exact(#[case] tokens: &str, #[case] expected: Option<&str>) { let attr = create_enum_attr_with_raw_tokens(tokens.parse().unwrap()); assert_eq!(extract_tag(&[attr]).as_deref(), expected); } @@ -475,7 +476,7 @@ mod tests { #[case(r#"qualified::content = "data""#, Some("data"))] #[case("qualified::content = value", None)] #[case("qualified::content", None)] - fn malformed_content_fallback_is_exact(#[case] tokens: &str, #[case] expected: Option<&str>) { + fn malformed_content_is_exact(#[case] tokens: &str, #[case] expected: Option<&str>) { let attr = create_enum_attr_with_raw_tokens(tokens.parse().unwrap()); assert_eq!(extract_content(&[attr]).as_deref(), expected); } diff --git a/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs b/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs index 1ba333a04..054a35a43 100644 --- a/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs +++ b/crates/vespera_macro/src/parser/schema/serde_attrs/fallback.rs @@ -194,16 +194,15 @@ mod tests { ); } - /// Test that the fallback's "default appears as a substring inside - /// another identifier" branch returns None (no false-positive - /// match). Exercises the trailing `None` arm of - /// `scan_default_from_raw_tokens` (substring found, but neither - /// `=` follows nor delimiter chars surround it). + /// 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 + /// nothing and returns `None`. #[test] fn test_extract_default_substring_in_identifier_is_not_a_match() { - // `field_default` contains "default" but as a suffix of an - // identifier — `before_char` is `_`, not one of the valid - // delimiters, so the standalone check fails. + // `field_default` contains "default" only as a suffix of an + // identifier: the character before it is `_`, which is not a key + // boundary, so it is not a key candidate. let tokens: TokenStream = "@bogus, field_default" .parse() .expect("token stream parses"); diff --git a/crates/vespera_macro/src/parser/schema/struct_schema.rs b/crates/vespera_macro/src/parser/schema/struct_schema.rs index 06da84001..39d39aa23 100644 --- a/crates/vespera_macro/src/parser/schema/struct_schema.rs +++ b/crates/vespera_macro/src/parser/schema/struct_schema.rs @@ -644,7 +644,7 @@ fn positional_schema_with( ) -> SchemaRef { let mut schema_ref = field_schema_ref(field, constraints, side, known_schemas, struct_definitions); - if !constraints.is_any() { + if !constraints.any_decides() { apply_garde_rules( &mut schema_ref, &field.ty, @@ -672,7 +672,7 @@ fn positional_schema_with( /// constraints `c` (explicit — they win). Shared by object properties and /// query / path struct parameters. pub fn apply_field_rules(schema_ref: &mut SchemaRef, field: &syn::Field, c: &SchemaConstraints) { - if !c.is_any() { + if !c.any_decides() { apply_garde_rules(schema_ref, &field.ty, &extract_garde_rules(&field.attrs), c); } if !c.is_empty() { diff --git a/crates/vespera_macro/src/parser/schema/struct_schema/flatten.rs b/crates/vespera_macro/src/parser/schema/struct_schema/flatten.rs index e3d63dcbd..8f8d7f19b 100644 --- a/crates/vespera_macro/src/parser/schema/struct_schema/flatten.rs +++ b/crates/vespera_macro/src/parser/schema/struct_schema/flatten.rs @@ -19,9 +19,16 @@ //! (`FlatMapAccess`) WITHOUT taking it: it types the outer object's //! unlisted keys AND every key of a flattened member declared after it //! (that member takes its keys only later), so in the request view those -//! members' properties must satisfy the map's value schema too; +//! members' properties must satisfy the map's value schema too. The one +//! exception is an opaque member that keeps the object open (see the +//! `allOf` paragraph below): the map's value schema is then not emitted at +//! all, because JSON Schema `additionalProperties` cannot see the keys that +//! member evaluates. The object stays open (nothing is rejected), but the +//! value type of the unlisted keys is not documented; //! - a flattened `Option` is `None` whenever `T` fails, so it never makes -//! a key required, and in the request view its keys reject nothing; +//! a key required; in the request view its keys reject nothing, except +//! when the outer `deny_unknown_fields` is set and `T` leaves its keys +//! (its properties are then `{not: {}}`, see [`request_member`]); //! - a flattened unit struct reads nothing; //! - a flattened externally tagged enum takes the FIRST buffered key naming //! a variant (`FlatMapDeserializer::deserialize_enum`) and leaves any @@ -41,9 +48,13 @@ //! which stays a `$ref` `allOf` member so its component is reused (see //! `assemble`). Only members whose keys cannot be known (a flattened enum, an //! unresolvable type) stay `allOf` members — and they keep the merged object -//! open. Such a member's resolved schema is inlined with every object inside -//! it reopened (see `embedded`: closed branches, map variants, `$ref` -//! branches): it never sees the outer keys, so it must not reject them. A +//! open, except in the request view of a `deny_unknown_fields` object when +//! the member leaves its keys (serde rejects them, so they do not open it; +//! see the bullets above). Keeping the object open also drops the flattened +//! map's value schema (see the map bullet). Such a member's resolved schema +//! is inlined with every object inside it reopened (see `embedded`: closed +//! branches, map variants, `$ref` branches): it never sees the outer keys, so +//! it must not reject them. A //! newtype / transparent wrapper (`allOf: [$ref]`) and a recursive generic //! instance (its materialised component) are merged like the struct or //! flattened like the enum they name. @@ -123,7 +134,7 @@ pub(super) fn flatten_part( let optional = optional || omittable; // `#[schema(value_type = T)]` documents a custom codec's wire shape, // exactly as on a field that is not flattened. - let custom = constraints.is_any() + let custom = constraints.any_decides() || (constraints.value_type.is_none() && codec_applies(&field.attrs, side)); let leaves = !custom && leaves_keys(ty, struct_definitions, WRAPPER_DEPTH); let schema = if custom { diff --git a/crates/vespera_macro/src/parser/schema/struct_schema/tests_serde_fidelity.rs b/crates/vespera_macro/src/parser/schema/struct_schema/tests_serde_fidelity.rs index 59d485675..9e865a26c 100644 --- a/crates/vespera_macro/src/parser/schema/struct_schema/tests_serde_fidelity.rs +++ b/crates/vespera_macro/src/parser/schema/struct_schema/tests_serde_fidelity.rs @@ -135,6 +135,9 @@ fn both_views(src: &str) -> String { "flatten_two_maps", "struct Req { #[serde(flatten)] a: HashMap, #[serde(flatten)] b: HashMap }" )] +// The opaque enum keeps the object open, so the map's value type is not +// documented in either view (`additionalProperties` cannot see the enum's +// keys; see the flatten module doc). Nothing is rejected. #[case::flatten_map_and_enum( "flatten_map_and_enum", "struct Req { id: i32, #[serde(flatten)] kind: Kind, #[serde(flatten)] extra: HashMap }" diff --git a/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs b/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs index 14fcc2034..e400dfa08 100644 --- a/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs +++ b/crates/vespera_macro/src/parser/schema/type_schema/conversion.rs @@ -669,9 +669,11 @@ fn parse_type_impl( let const_exprs = const_argument_exprs(parsed.generics_mut(), args); // Substitute generic parameters with concrete types in - // all fields. A lifetime-only definition (`Borrowed<'a>`) - // has no type to substitute: it is its own component. - if !generic_params.is_empty() + // all fields. A definition with neither a type nor a + // const argument (`Borrowed<'a>`) has nothing to + // substitute: it is its own component. A const-only + // definition (`Matrix<3>`) is inlined with its lengths. + if (!generic_params.is_empty() || !const_exprs.is_empty()) && generic_params.len() == concrete_types.len() { // (named AND tuple fields — tuple structs now map diff --git a/crates/vespera_macro/src/parser/schema/type_schema/conversion/schema_def.rs b/crates/vespera_macro/src/parser/schema/type_schema/conversion/schema_def.rs index 53b777b24..21079104d 100644 --- a/crates/vespera_macro/src/parser/schema/type_schema/conversion/schema_def.rs +++ b/crates/vespera_macro/src/parser/schema/type_schema/conversion/schema_def.rs @@ -229,6 +229,13 @@ impl SchemaDef { /// Vec)]`, `#[serde(into = "Wire")]`) — container, variants and /// fields (all variants of an enum). pub(super) fn substitute(&mut self, params: &[String], concrete: &[&Type]) { + self.rewrite_types(&|ty| substitute_type(ty, params, concrete)); + } + + /// Apply `rewrite` to every field type and to every type the container, + /// variant and field attributes name (`value_type`, `into`, `from`, + /// `try_from`), in the order they are written. + fn rewrite_types(&mut self, rewrite: &dyn Fn(&Type) -> Type) { let mut attrs: Vec<&mut Vec> = Vec::new(); let mut fields: Vec<&mut syn::Field> = Vec::new(); match self { @@ -245,42 +252,39 @@ impl SchemaDef { } } for field in fields { - field.ty = substitute_type(&field.ty, params, concrete); + field.ty = rewrite(&field.ty); attrs.push(&mut field.attrs); } for attr in attrs.into_iter().flatten() { if let syn::Meta::List(list) = &mut attr.meta && (list.path.is_ident("schema") || list.path.is_ident("serde")) { - list.tokens = substitute_tokens(list.tokens.clone(), params, concrete); + list.tokens = substitute_tokens(list.tokens.clone(), rewrite); } } } /// Replace the const parameters `consts` (see [`const_argument_exprs`]) by - /// their argument expressions in every field type, so an array length + /// their argument expressions in every field type and in the types the + /// attributes name (`#[schema(value_type = [T; N])]`), so an array length /// `[T; N]` becomes the literal the instantiation gives (`[T; 4]`, which /// pins `minItems` / `maxItems`). pub(super) fn substitute_consts(&mut self, consts: &[(String, syn::Expr)]) { - let fields: Vec<&mut syn::Field> = match self { - Self::Struct(item) => item.fields.iter_mut().collect(), - Self::Enum(item) => item - .variants - .iter_mut() - .flat_map(|variant| variant.fields.iter_mut()) - .collect(), - }; - let mut visitor = ConstArgs { consts }; - for field in fields { - syn::visit_mut::VisitMut::visit_type_mut(&mut visitor, &mut field.ty); + if consts.is_empty() { + return; } + self.rewrite_types(&|ty| { + let mut ty = ty.clone(); + syn::visit_mut::VisitMut::visit_type_mut(&mut ConstArgs { consts }, &mut ty); + ty + }); } } -/// `tokens` of an attribute list with the type after `value_type =` and the -/// type string after `into =` / `from =` / `try_from =` substituted (other -/// keys, `rename = "T"` included, are left alone). -fn substitute_tokens(tokens: TokenStream, params: &[String], concrete: &[&Type]) -> TokenStream { +/// `tokens` of an attribute list with `rewrite` applied to the type after +/// `value_type =` and to the type string after `into =` / `from =` / +/// `try_from =` (other keys, `rename = "T"` included, are left alone). +fn substitute_tokens(tokens: TokenStream, rewrite: &dyn Fn(&Type) -> Type) -> TokenStream { let trees: Vec = tokens.into_iter().collect(); let mut out = TokenStream::new(); let mut index = 0; @@ -290,7 +294,7 @@ fn substitute_tokens(tokens: TokenStream, params: &[String], concrete: &[&Type]) TokenTree::Group(group) => { let mut inner = proc_macro2::Group::new( group.delimiter(), - substitute_tokens(group.stream(), params, concrete), + substitute_tokens(group.stream(), rewrite), ); inner.set_span(group.span()); out.extend([TokenTree::Group(inner)]); @@ -310,7 +314,7 @@ fn substitute_tokens(tokens: TokenStream, params: &[String], concrete: &[&Type]) index += 1; let end = value_end(&trees, index); let value: TokenStream = trees[index..end].iter().cloned().collect(); - out.extend(substitute_value(&key, value, params, concrete)); + out.extend(substitute_value(&key, value, rewrite)); index = end; } out @@ -341,21 +345,16 @@ fn value_end(trees: &[TokenTree], start: usize) -> usize { /// The substituted `value` of `key` (unchanged for other keys or a value /// that is not a type / type string). -fn substitute_value( - key: &str, - value: TokenStream, - params: &[String], - concrete: &[&Type], -) -> TokenStream { +fn substitute_value(key: &str, value: TokenStream, rewrite: &dyn Fn(&Type) -> Type) -> TokenStream { let substituted = match key { "value_type" => syn::parse2::(value.clone()) .ok() - .map(|ty| quote::ToTokens::into_token_stream(substitute_type(&ty, params, concrete))), + .map(|ty| quote::ToTokens::into_token_stream(rewrite(&ty))), "into" | "from" | "try_from" => syn::parse2::(value.clone()) .ok() .and_then(|lit| Some((lit.parse::().ok()?, lit.span()))) .map(|(ty, span)| { - let ty = substitute_type(&ty, params, concrete); + let ty = rewrite(&ty); let text = quote::ToTokens::into_token_stream(ty).to_string(); quote::ToTokens::into_token_stream(syn::LitStr::new(&text, span)) }), diff --git a/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_const.rs b/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_const.rs index 84a23925a..7332b3dab 100644 --- a/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_const.rs +++ b/crates/vespera_macro/src/parser/schema/type_schema/conversion/tests_const.rs @@ -66,6 +66,86 @@ fn recursive_const_instances_get_distinct_components() { assert!(keys.iter().all(|key| !key.contains("_Const.N")), "{keys:?}"); } +/// Round 30: a const argument of a NESTED type argument is part of the name +/// too. `Tree>` and `Tree>` are distinct recursive +/// components, and each one's `value` keeps its own array length (before the +/// fix both were named `Tree_Buf_u8`, so the second reused the first). +#[test] +fn nested_const_arguments_get_distinct_recursive_components() { + take_materialized(); + let known: HashSet = ["Tree", "Buf"].into_iter().map(String::from).collect(); + let defs = HashMap::from([ + ( + "Buf".to_string(), + "struct Buf { data: [T; N] }".to_string(), + ), + ( + "Tree".to_string(), + "struct Tree { value: T, children: Vec> }".to_string(), + ), + ]); + let reference_of = |src: &str| { + let ty: Type = syn::parse_str(src).unwrap(); + reference(parse_type_to_schema_ref(&ty, &known, &defs)) + }; + let four = reference_of("Tree>"); + let eight = reference_of("Tree>"); + assert_ne!(four, eight); + let materialized = take_materialized(); + for (path, len) in [(four, 4), (eight, 8)] { + let name = path.trim_start_matches("#/components/schemas/"); + let component = &materialized[name]; + let properties = component.properties.as_ref().expect("the tree's fields"); + let SchemaRef::Inline(value) = &properties["value"] else { + panic!("the buffer is inline"); + }; + let value_properties = value.properties.as_ref().expect("the buffer's fields"); + let SchemaRef::Inline(data) = &value_properties["data"] else { + panic!("the array is inline"); + }; + assert_eq!(data.min_items, Some(len), "{name}"); + assert_eq!(data.max_items, Some(len), "{name}"); + } +} + +/// Round 30: a definition with only const parameters (`Matrix`) has +/// no type to substitute, but its `Matrix<3>` argument is concrete: it inlines +/// with the length pinned, as a definition with a type parameter does. +#[test] +fn const_only_definition_inlines_with_its_length() { + let known: HashSet = ["Matrix"].into_iter().map(String::from).collect(); + let defs = HashMap::from([( + "Matrix".to_string(), + "struct Matrix { data: [f32; N] }".to_string(), + )]); + let schema = inline_schema("Matrix<3>", &known, &defs); + let properties = schema.properties.expect("the struct's fields"); + let SchemaRef::Inline(data) = &properties["data"] else { + panic!("the array is inline"); + }; + assert_eq!(data.min_items, Some(3)); + assert_eq!(data.max_items, Some(3)); +} + +/// Round 30: a const parameter named in an attribute type (`value_type = +/// [T; N]`) is pinned by the instantiation like one named in a field type, so +/// the documented array has the argument's fixed length. +#[test] +fn const_arguments_pin_lengths_in_attribute_types() { + let known: HashSet = ["Buf"].into_iter().map(String::from).collect(); + let defs = HashMap::from([( + "Buf".to_string(), + "struct Buf { #[schema(value_type = [T; N])] data: Vec }".to_string(), + )]); + let schema = inline_schema("Buf", &known, &defs); + let properties = schema.properties.expect("the struct's fields"); + let SchemaRef::Inline(data) = &properties["data"] else { + panic!("the array is inline"); + }; + assert_eq!(data.min_items, Some(4)); + assert_eq!(data.max_items, Some(4)); +} + /// Round 29: a const argument written as a bare identifier (`Buf`, /// parsed as a type) pairs with its const parameter, so the type parameter /// still takes `u8` and the vector's items are integers, as with `Buf`. diff --git a/crates/vespera_macro/src/parser/schema/type_schema/generic_instances.rs b/crates/vespera_macro/src/parser/schema/type_schema/generic_instances.rs index 0b5ac15cf..e7d9e98fa 100644 --- a/crates/vespera_macro/src/parser/schema/type_schema/generic_instances.rs +++ b/crates/vespera_macro/src/parser/schema/type_schema/generic_instances.rs @@ -129,6 +129,16 @@ fn push_type_segments( for arg in &args.args { if let syn::GenericArgument::Type(inner) = arg { push_type_segments(name, inner, component); + } else if let syn::GenericArgument::Const(expr) = arg { + // A nested const argument (`Tree>`) is + // spelled as a top-level one is + // ([`push_const_segments`]), so `Buf` and + // `Buf` never share a name. A bare + // identifier (`Buf`) parses as a type and + // spells as one; a const slot never holds a type, + // so that cannot collide with a type argument. + name.push_str("_Const."); + push_array_len(name, expr); } } } @@ -385,6 +395,10 @@ mod tests { #[case::other("Page", &["impl Trait"], "Page_.Value")] #[case::qualified_self("Page", &["::Out"], "Page_.Qself_T_Tr.pOut")] #[case::underscore_ident("Pair", &["A_B", "C"], "Pair_A-B_C")] + // A const argument of a nested type is spelled like a top-level one; a + // bare identifier in that slot spells as the type it parses as. + #[case::nested_const_argument("Tree", &["Buf"], "Tree_Buf_u8_Const.4")] + #[case::nested_bare_const_argument("Tree", &["Buf"], "Tree_Buf_u8_LEN")] // Round 11: the base is escaped like an argument. #[case::underscore_base("My_Tree", &["i32"], "My-Tree_i32")] #[case::dash_dot_base("My-Tree.v1", &["i32"], "My.hTree.dv1_i32")] @@ -411,6 +425,11 @@ mod tests { #[case::qualified_self_vs_bare(&["::Out"], &["Out"])] #[case::const_len_vs_expr(&["[u8; N]"], &["[u8; 2 * 2]"])] #[case::const_len_path_vs_bare(&["[u8; a::N]"], &["[u8; N]"])] + // Round 30: nested const arguments are part of the name, so two + // instances of a nested const generic never share a component. + #[case::nested_const_len(&["Tree>"], &["Tree>"])] + #[case::nested_const_vs_bare_const(&["Tree>"], &["Tree>"])] + #[case::nested_const_vs_type(&["Tree>"], &["Tree>"])] fn instance_names_are_injective(#[case] left: &[&str], #[case] right: &[&str]) { let parse = |args: &[&str]| -> Vec { args.iter() diff --git a/crates/vespera_macro/src/parser/schema/wire_override.rs b/crates/vespera_macro/src/parser/schema/wire_override.rs index d63dc4323..bf152baa8 100644 --- a/crates/vespera_macro/src/parser/schema/wire_override.rs +++ b/crates/vespera_macro/src/parser/schema/wire_override.rs @@ -114,13 +114,14 @@ pub fn field_schema_ref( known_schemas: &HashSet + Eq + Hash>, struct_definitions: &HashMap + Eq + Hash, impl AsRef>, ) -> SchemaRef { - // `#[schema(any)]` is the "any JSON value" (`{}`) escape hatch. - if constraints.is_any() { - return SchemaRef::Inline(Box::default()); - } + // `value_type` always wins; then `#[schema(any)]` is the "any JSON value" + // (`{}`) escape hatch. if let Some(value_type) = &constraints.value_type { return parse_type_to_schema_ref(&value_type.0, known_schemas, struct_definitions); } + if constraints.is_any() { + return SchemaRef::Inline(Box::default()); + } match codec_wire(field, side) { CodecWire::RustType => { parse_type_to_schema_ref(&field.ty, known_schemas, struct_definitions) @@ -353,6 +354,28 @@ mod tests { item.fields.into_iter().next().unwrap() } + /// Round 30: `value_type` wins over `#[schema(any)]` on the same field + /// (the module doc): the field documents the value type, not `{}`. + #[test] + fn value_type_wins_over_any() { + let field = field("#[schema(any, value_type = String)] x: Vec"); + let constraints = extract_schema_constraints(&field.attrs); + let schema = field_schema_ref( + &field, + &constraints, + SerdeSide::Serialize, + &HashSet::::new(), + &HashMap::::new(), + ); + let SchemaRef::Inline(schema) = schema else { + panic!("expected an inline schema"); + }; + assert_eq!( + schema.schema_type, + Some(vespera_core::schema::SchemaType::String) + ); + } + #[rstest] #[case::plain("plain", "x: DateTime", SerdeSide::Serialize)] #[case::any(