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
33 changes: 12 additions & 21 deletions xsd-parser/src/pipeline/renderer/steps/quick_xml/serialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,15 +751,6 @@ impl ComplexBase<'_> {
Some(quote!(helper.write_xmlns(&mut bytes, None, &#ns_const);))
});

let need_xsi_namespace =
nillable_type_support && !collector.provides_xsi_namespace();
let xsi = need_xsi_namespace.then(|| {
let namespace = resolve_quick_xml_ident!(ctx, "::xsd_parser_types::misc::Namespace");
let namespace_prefix = resolve_quick_xml_ident!(ctx, "::xsd_parser_types::misc::NamespacePrefix");

quote!(helper.write_xmlns(&mut bytes, Some(&#namespace_prefix::XSI), &#namespace::XSI);)
});

let global_xmlns = collector
.get_namespaces(ctx.types, ctx.ident, default_namespace.as_ref())
.values()
Expand All @@ -777,8 +768,8 @@ impl ComplexBase<'_> {
quote! {
helper.write_xmlns(&mut bytes, #prefix_const, &#ns_const);
}
});
let global_xmlns = xsi.into_iter().chain(global_xmlns).collect::<Vec<_>>();
})
.collect::<Vec<_>>();
let global_xmlns = global_xmlns.is_empty().not().then(|| {
quote! {
if self.is_root {
Expand Down Expand Up @@ -1412,10 +1403,6 @@ impl NamespaceCollector {
}))
}

fn provides_xsi_namespace(&self) -> bool {
self.xsi_namespace.is_some()
}

fn get_namespaces(
&mut self,
types: &MetaTypes,
Expand Down Expand Up @@ -1445,12 +1432,6 @@ impl NamespaceCollector {
let ty = types.items.get(ident).unwrap();
let mut state = GetNamespaceState::Empty;

if self.nillable_type_support {
if let Some(id) = &self.xsi_namespace {
Self::add_ns(&mut state, types, NamespaceKey::Normal(*id));
}
}

match &ty.variant {
MetaTypeVariant::Union(x) => {
for ty in &*x.types {
Expand All @@ -1459,6 +1440,11 @@ impl NamespaceCollector {
}
MetaTypeVariant::Reference(x) => {
self.merge(&mut state, types, &x.type_, default_ns);
if self.nillable_type_support && x.nillable {
if let Some(id) = self.xsi_namespace {
Self::add_ns(&mut state, types, NamespaceKey::Normal(id));
}
}
}
MetaTypeVariant::Enumeration(x) => {
for var in &*x.variants {
Expand All @@ -1479,6 +1465,11 @@ impl NamespaceCollector {
if let ElementMetaVariant::Type { type_, .. } = &el.variant {
self.merge(&mut state, types, type_, default_ns);
}
if self.nillable_type_support && el.nillable {
if let Some(id) = self.xsi_namespace {
Self::add_ns(&mut state, types, NamespaceKey::Normal(id));
}
}
}
}
MetaTypeVariant::ComplexType(x) => {
Expand Down
27 changes: 0 additions & 27 deletions xsd-parser/tests/feature/defaultable_content/expected/quick_xml.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2186,14 +2186,6 @@ pub mod quick_xml_serialize {
WithSerializer::serializer(&self.value.content, None, false)?,
);
let mut bytes = BytesStart::new(self.name);
helper.begin_ns_scope();
if self.is_root {
helper.write_xmlns(
&mut bytes,
Some(&super::PREFIX_XSI),
&super::NS_XSI,
);
}
helper.write_attrib_opt(&mut bytes, "attrib-a", &self.value.attrib_a)?;
helper.write_attrib_opt(&mut bytes, "attrib-b", &self.value.attrib_b)?;
return Ok(Some(Event::Start(bytes)));
Expand All @@ -2204,7 +2196,6 @@ pub mod quick_xml_serialize {
},
SimpleTypeSerializerState::End__ => {
*self.state = SimpleTypeSerializerState::Done__;
helper.end_ns_scope();
return Ok(Some(Event::End(BytesEnd::new(self.name))));
}
SimpleTypeSerializerState::Done__ => return Ok(None),
Expand Down Expand Up @@ -2252,14 +2243,6 @@ pub mod quick_xml_serialize {
WithSerializer::serializer(&self.value.content, None, false)?,
);
let mut bytes = BytesStart::new(self.name);
helper.begin_ns_scope();
if self.is_root {
helper.write_xmlns(
&mut bytes,
Some(&super::PREFIX_XSI),
&super::NS_XSI,
);
}
helper.write_attrib_opt(&mut bytes, "attrib-a", &self.value.attrib_a)?;
helper.write_attrib_opt(&mut bytes, "attrib-b", &self.value.attrib_b)?;
return Ok(Some(Event::Start(bytes)));
Expand All @@ -2272,7 +2255,6 @@ pub mod quick_xml_serialize {
}
SequenceTypeSerializerState::End__ => {
*self.state = SequenceTypeSerializerState::Done__;
helper.end_ns_scope();
return Ok(Some(Event::End(BytesEnd::new(self.name))));
}
SequenceTypeSerializerState::Done__ => return Ok(None),
Expand Down Expand Up @@ -2386,14 +2368,6 @@ pub mod quick_xml_serialize {
WithSerializer::serializer(&self.value.content, None, false)?,
);
let mut bytes = BytesStart::new(self.name);
helper.begin_ns_scope();
if self.is_root {
helper.write_xmlns(
&mut bytes,
Some(&super::PREFIX_XSI),
&super::NS_XSI,
);
}
helper.write_attrib_opt(&mut bytes, "attrib-a", &self.value.attrib_a)?;
helper.write_attrib_opt(&mut bytes, "attrib-b", &self.value.attrib_b)?;
return Ok(Some(Event::Start(bytes)));
Expand All @@ -2406,7 +2380,6 @@ pub mod quick_xml_serialize {
}
NestedSeqTypeSerializerState::End__ => {
*self.state = NestedSeqTypeSerializerState::Done__;
helper.end_ns_scope();
return Ok(Some(Event::End(BytesEnd::new(self.name))));
}
NestedSeqTypeSerializerState::Done__ => return Ok(None),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -835,11 +835,6 @@ pub mod tns {
let mut bytes = BytesStart::new(self.name);
helper.begin_ns_scope();
if self.is_root {
helper.write_xmlns(
&mut bytes,
Some(&super::super::PREFIX_XSI),
&super::super::NS_XSI,
);
helper.write_xmlns(
&mut bytes,
Some(&super::super::PREFIX_TNS),
Expand Down Expand Up @@ -912,11 +907,6 @@ pub mod tns {
let mut bytes = BytesStart::new(self.name);
helper.begin_ns_scope();
if self.is_root {
helper.write_xmlns(
&mut bytes,
Some(&super::super::PREFIX_XSI),
&super::super::NS_XSI,
);
helper.write_xmlns(
&mut bytes,
Some(&super::super::PREFIX_TNS),
Expand Down Expand Up @@ -1041,11 +1031,6 @@ pub mod tns {
let mut bytes = BytesStart::new(self.name);
helper.begin_ns_scope();
if self.is_root {
helper.write_xmlns(
&mut bytes,
Some(&super::super::PREFIX_XSI),
&super::super::NS_XSI,
);
helper.write_xmlns(
&mut bytes,
Some(&super::super::PREFIX_TNS),
Expand Down
4 changes: 4 additions & 0 deletions xsd-parser/tests/feature/nillable/example/bar.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<Bar>
<X>1</X>
<Y>2</Y>
</Bar>
6 changes: 6 additions & 0 deletions xsd-parser/tests/feature/nillable/expected/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,9 @@ pub struct FooType {
pub d: Option<Nillable<i32>>,
}
pub type NillableFoo = Nillable<FooType>;
pub type Bar = BarType;
#[derive(Debug)]
pub struct BarType {
pub x: i32,
pub y: i32,
}
Loading
Loading