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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
* Fixes `TryFrom<&str>` for a string-constrained newtype over a string-like native type
* A string-constrained newtype whose inner type is neither `String` nor
`FromStr` is rejected at `finalize` rather than in the consumer's build
* Schemas for tuple structs with a direct standard `Vec` remainder now describe
each trailing element instead of the vector container. This changes schema
validation without changing the generated Rust representation or Serde behavior.

## [0.1.0] - 2026-11-07

Expand Down
28 changes: 23 additions & 5 deletions typespace/src/build/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ use crate::build::{JsonValue, Type, TypeCommon, TypeCommonBuilt, integer_width,
use crate::error::{Error, NameAxis};
use crate::output::Outputspace;
use crate::serde_attrs::SerdeDerives;
use crate::settings::{GeneratedCrate, Settings};
use crate::settings::{GeneratedCrate, Settings, path_text};
use crate::{
DefaultConstructor, RenderedStructProperty, TypespaceBuilder, TypespaceRenderer,
TypespaceTrait, has_trait,
DefaultConstructor, RenderedStructProperty, TraitProvision, TypespaceBuilder,
TypespaceRenderer, TypespaceTrait, has_trait,
};

/// A struct with named fields.
Expand Down Expand Up @@ -1200,10 +1200,28 @@ impl<Id: Clone + Ord + std::fmt::Debug + std::fmt::Display> TupleStruct<Id> {
let json_schema_impl = traits.remove(TypespaceTrait::JsonSchema).then(|| {
let schemars = out.crate_path(GeneratedCrate::Schemars);
let (additional_items, min_items, max_items) = if let Some(rest_id) = rest.as_ref() {
assert!(rest_ident.is_some());
// For a direct standard Vec remainder, each trailing item uses
// the element's schema. IfParameters establishes trait availability;
// it does not establish how a custom container represents its items.
// Restrict this correction to the known standard Vec case. Custom
// containers (including transparent Vec wrappers or aliases) and
// other remainder shapes retain their existing schema behavior,
// which may still describe an extra array level.
let vec_type = &typespace.settings.vec_type;
let additional_item_id = match typespace.types.get(rest_id) {
Some(Type::Vec(element_id))
if path_text(vec_type.path()) == "::std::vec::Vec"
&& vec_type.provision(TypespaceTrait::JsonSchema)
== TraitProvision::IfParameters =>
{
element_id
}
_ => rest_id,
};
let additional_item_ident = typespace.render_ident(additional_item_id, out);
let additional_items = quote! {
additional_items: Some(::std::boxed::Box::new(
g.subschema_for::<#rest_ident>()
g.subschema_for::<#additional_item_ident>()
)),
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
pub struct Custom(pub u32, pub super::OpaqueVec<super::NoSchema>);
impl ::schemars::JsonSchema for Custom {
fn schema_name() -> ::std::string::String {
"Custom".to_string()
}
fn json_schema(
g: &mut ::schemars::r#gen::SchemaGenerator,
) -> ::schemars::schema::Schema {
let fields = [g.subschema_for::<u32>()].into_iter().collect();
::schemars::schema::SchemaObject {
metadata: Some(
::std::boxed::Box::new(::schemars::schema::Metadata {
title: Some("Custom".to_string()),
..::std::default::Default::default()
}),
),
instance_type: Some(
::schemars::schema::SingleOrVec::Single(
::std::boxed::Box::new(::schemars::schema::InstanceType::Array),
),
),
array: Some(
::std::boxed::Box::new(::schemars::schema::ArrayValidation {
items: Some(::schemars::schema::SingleOrVec::Vec(fields)),
additional_items: Some(
::std::boxed::Box::new(
g.subschema_for::<super::OpaqueVec<super::NoSchema>>(),
),
),
min_items: Some(1u32),
..::std::default::Default::default()
}),
),
..::std::default::Default::default()
}
.into()
}
}
268 changes: 268 additions & 0 deletions typespace/tests/output/standard_vec_remainder_schemas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,268 @@
#[derive(::serde::Deserialize, ::serde::Serialize, schemars::JsonSchema)]
pub struct Element {
pub text: ::std::string::String,
}
pub struct Nested(pub u32, pub ::std::vec::Vec<::std::vec::Vec<::std::string::String>>);
impl ::serde::Serialize for Nested {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
use ::serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(None)?;
seq.serialize_element(&self.0)?;
self.1.serialize(::json_serde::FlattenedSequenceSerializer::new(&mut seq))?;
seq.end()
}
}
impl<'de> ::serde::Deserialize<'de> for Nested {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> ::serde::de::Visitor<'de> for Visitor {
type Value = Nested;
fn expecting(
&self,
formatter: &mut ::std::fmt::Formatter,
) -> ::std::fmt::Result {
formatter.write_str("a sequence")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: ::serde::de::SeqAccess<'de>,
{
let field_0 = seq
.next_element()?
.ok_or_else(|| ::serde::de::Error::invalid_length(
0usize,
&"a tuple of size 1 or more",
))?;
let rest = ::serde::Deserialize::deserialize(
::json_serde::FlattenedSequenceDeserializer::new(&mut seq),
)?;
Ok(Nested(field_0, rest))
}
}
deserializer.deserialize_seq(Visitor)
}
}
impl ::schemars::JsonSchema for Nested {
fn schema_name() -> ::std::string::String {
"Nested".to_string()
}
fn json_schema(
g: &mut ::schemars::r#gen::SchemaGenerator,
) -> ::schemars::schema::Schema {
let fields = [g.subschema_for::<u32>()].into_iter().collect();
::schemars::schema::SchemaObject {
metadata: Some(
::std::boxed::Box::new(::schemars::schema::Metadata {
title: Some("Nested".to_string()),
..::std::default::Default::default()
}),
),
instance_type: Some(
::schemars::schema::SingleOrVec::Single(
::std::boxed::Box::new(::schemars::schema::InstanceType::Array),
),
),
array: Some(
::std::boxed::Box::new(::schemars::schema::ArrayValidation {
items: Some(::schemars::schema::SingleOrVec::Vec(fields)),
additional_items: Some(
::std::boxed::Box::new(
g.subschema_for::<::std::vec::Vec<::std::string::String>>(),
),
),
min_items: Some(1u32),
..::std::default::Default::default()
}),
),
..::std::default::Default::default()
}
.into()
}
}
#[derive(::serde::Deserialize, ::serde::Serialize, schemars::JsonSchema)]
#[serde(transparent)]
pub struct Ordinary(pub ::std::vec::Vec<::std::string::String>);
impl ::std::ops::Deref for Ordinary {
type Target = ::std::vec::Vec<::std::string::String>;
fn deref(&self) -> &::std::vec::Vec<::std::string::String> {
&self.0
}
}
impl ::std::convert::From<Ordinary> for ::std::vec::Vec<::std::string::String> {
fn from(value: Ordinary) -> Self {
value.0
}
}
impl ::std::convert::From<::std::vec::Vec<::std::string::String>> for Ordinary {
fn from(value: ::std::vec::Vec<::std::string::String>) -> Self {
Self(value)
}
}
pub struct Referenced(pub u32, pub ::std::vec::Vec<Element>);
impl ::serde::Serialize for Referenced {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
use ::serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(None)?;
seq.serialize_element(&self.0)?;
self.1.serialize(::json_serde::FlattenedSequenceSerializer::new(&mut seq))?;
seq.end()
}
}
impl<'de> ::serde::Deserialize<'de> for Referenced {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> ::serde::de::Visitor<'de> for Visitor {
type Value = Referenced;
fn expecting(
&self,
formatter: &mut ::std::fmt::Formatter,
) -> ::std::fmt::Result {
formatter.write_str("a sequence")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: ::serde::de::SeqAccess<'de>,
{
let field_0 = seq
.next_element()?
.ok_or_else(|| ::serde::de::Error::invalid_length(
0usize,
&"a tuple of size 1 or more",
))?;
let rest = ::serde::Deserialize::deserialize(
::json_serde::FlattenedSequenceDeserializer::new(&mut seq),
)?;
Ok(Referenced(field_0, rest))
}
}
deserializer.deserialize_seq(Visitor)
}
}
impl ::schemars::JsonSchema for Referenced {
fn schema_name() -> ::std::string::String {
"Referenced".to_string()
}
fn json_schema(
g: &mut ::schemars::r#gen::SchemaGenerator,
) -> ::schemars::schema::Schema {
let fields = [g.subschema_for::<u32>()].into_iter().collect();
::schemars::schema::SchemaObject {
metadata: Some(
::std::boxed::Box::new(::schemars::schema::Metadata {
title: Some("Referenced".to_string()),
..::std::default::Default::default()
}),
),
instance_type: Some(
::schemars::schema::SingleOrVec::Single(
::std::boxed::Box::new(::schemars::schema::InstanceType::Array),
),
),
array: Some(
::std::boxed::Box::new(::schemars::schema::ArrayValidation {
items: Some(::schemars::schema::SingleOrVec::Vec(fields)),
additional_items: Some(
::std::boxed::Box::new(g.subschema_for::<Element>()),
),
min_items: Some(1u32),
..::std::default::Default::default()
}),
),
..::std::default::Default::default()
}
.into()
}
}
pub struct Unconstrained(pub u32, pub ::std::vec::Vec<serde_json::Value>);
impl ::serde::Serialize for Unconstrained {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: ::serde::Serializer,
{
use ::serde::ser::SerializeSeq;
let mut seq = serializer.serialize_seq(None)?;
seq.serialize_element(&self.0)?;
self.1.serialize(::json_serde::FlattenedSequenceSerializer::new(&mut seq))?;
seq.end()
}
}
impl<'de> ::serde::Deserialize<'de> for Unconstrained {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: ::serde::Deserializer<'de>,
{
struct Visitor;
impl<'de> ::serde::de::Visitor<'de> for Visitor {
type Value = Unconstrained;
fn expecting(
&self,
formatter: &mut ::std::fmt::Formatter,
) -> ::std::fmt::Result {
formatter.write_str("a sequence")
}
fn visit_seq<A>(self, mut seq: A) -> Result<Self::Value, A::Error>
where
A: ::serde::de::SeqAccess<'de>,
{
let field_0 = seq
.next_element()?
.ok_or_else(|| ::serde::de::Error::invalid_length(
0usize,
&"a tuple of size 1 or more",
))?;
let rest = ::serde::Deserialize::deserialize(
::json_serde::FlattenedSequenceDeserializer::new(&mut seq),
)?;
Ok(Unconstrained(field_0, rest))
}
}
deserializer.deserialize_seq(Visitor)
}
}
impl ::schemars::JsonSchema for Unconstrained {
fn schema_name() -> ::std::string::String {
"Unconstrained".to_string()
}
fn json_schema(
g: &mut ::schemars::r#gen::SchemaGenerator,
) -> ::schemars::schema::Schema {
let fields = [g.subschema_for::<u32>()].into_iter().collect();
::schemars::schema::SchemaObject {
metadata: Some(
::std::boxed::Box::new(::schemars::schema::Metadata {
title: Some("Unconstrained".to_string()),
..::std::default::Default::default()
}),
),
instance_type: Some(
::schemars::schema::SingleOrVec::Single(
::std::boxed::Box::new(::schemars::schema::InstanceType::Array),
),
),
array: Some(
::std::boxed::Box::new(::schemars::schema::ArrayValidation {
items: Some(::schemars::schema::SingleOrVec::Vec(fields)),
additional_items: Some(
::std::boxed::Box::new(g.subschema_for::<serde_json::Value>()),
),
min_items: Some(1u32),
..::std::default::Default::default()
}),
),
..::std::default::Default::default()
}
.into()
}
}
2 changes: 1 addition & 1 deletion typespace/tests/output/test_tuple_struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl ::schemars::JsonSchema for MyTupleStruct {
::std::boxed::Box::new(::schemars::schema::ArrayValidation {
items: Some(::schemars::schema::SingleOrVec::Vec(fields)),
additional_items: Some(
::std::boxed::Box::new(g.subschema_for::<Vec<String>>()),
::std::boxed::Box::new(g.subschema_for::<String>()),
),
min_items: Some(2u32),
..::std::default::Default::default()
Expand Down
Loading
Loading