Skip to content
Closed
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
22 changes: 18 additions & 4 deletions xsd-parser/src/pipeline/interpreter/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::models::{
meta::{MetaType, MetaTypeVariant},
schema::{
xs::{
AttributeGroupType, AttributeType, ComplexBaseType, ElementType, GroupType, Schema,
SchemaContent, SimpleBaseType,
AttributeGroupType, AttributeType, ComplexBaseType, ElementType, GroupType,
RedefineContent, Schema, SchemaContent, SimpleBaseType,
},
NamespaceId, QName, SchemaId, Schemas,
},
Expand Down Expand Up @@ -76,8 +76,22 @@ impl<'schema, 'state> SchemaInterpreter<'schema, 'state> {
| SchemaContent::Import(_)
| SchemaContent::Include(_)
| SchemaContent::Notation(_)
| SchemaContent::Override(_)
| SchemaContent::Redefine(_) => (),
| SchemaContent::Override(_) => (),
SchemaContent::Redefine(x) => {
for c in &x.content {
match c {
RedefineContent::Annotation(_)
| RedefineContent::AttributeGroup(_)
| RedefineContent::Group(_) => (),
RedefineContent::SimpleType(x) => {
this.create_simple_type(target_namespace, None, x)?;
}
RedefineContent::ComplexType(x) => {
this.create_complex_type(target_namespace, None, x)?;
}
}
}
}
SchemaContent::Element(x) => {
this.create_element(target_namespace, None, x)?;
}
Expand Down
1 change: 1 addition & 0 deletions xsd-parser/tests/feature/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ mod nillable;
mod nillable_dynamic_types;
mod num_big_int;
mod numeric_fields;
mod redefine;
mod ref_to_attribute;
mod schema_display_name;
mod schema_no_prefix;
Expand Down
3 changes: 3 additions & 0 deletions xsd-parser/tests/feature/redefine/example/default.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<tns:Foo xmlns:tns="http://example.com">
1
</tns:Foo>
7 changes: 7 additions & 0 deletions xsd-parser/tests/feature/redefine/expected/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub type Foo = EnumType;
#[derive(Debug)]
pub enum EnumType {
_1,
_2,
_3,
}
42 changes: 42 additions & 0 deletions xsd-parser/tests/feature/redefine/expected/quick_xml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::borrow::Cow;
use xsd_parser_types::{
misc::{Namespace, NamespacePrefix},
quick_xml::{
DeserializeBytes, DeserializeHelper, Error, ErrorKind, RawByteStr, SerializeBytes,
SerializeHelper,
},
};
pub const NS_XS: Namespace = Namespace::new_const(b"http://www.w3.org/2001/XMLSchema");
pub const NS_XML: Namespace = Namespace::new_const(b"http://www.w3.org/XML/1998/namespace");
pub const NS_TNS: Namespace = Namespace::new_const(b"http://example.com");
pub const PREFIX_XS: NamespacePrefix = NamespacePrefix::new_const(b"xs");
pub const PREFIX_XML: NamespacePrefix = NamespacePrefix::new_const(b"xml");
pub const PREFIX_TNS: NamespacePrefix = NamespacePrefix::new_const(b"tns");
pub type Foo = EnumType;
#[derive(Debug)]
pub enum EnumType {
_1,
_2,
_3,
}
impl SerializeBytes for EnumType {
fn serialize_bytes(&self, helper: &mut SerializeHelper) -> Result<Option<Cow<'_, str>>, Error> {
match self {
Self::_1 => Ok(Some(Cow::Borrowed("1"))),
Self::_2 => Ok(Some(Cow::Borrowed("2"))),
Self::_3 => Ok(Some(Cow::Borrowed("3"))),
}
}
}
impl DeserializeBytes for EnumType {
fn deserialize_bytes(helper: &mut DeserializeHelper, bytes: &[u8]) -> Result<Self, Error> {
match bytes {
b"1" => Ok(Self::_1),
b"2" => Ok(Self::_2),
b"3" => Ok(Self::_3),
x => Err(Error::from(ErrorKind::UnknownOrInvalidValue(
RawByteStr::from_slice(x),
))),
}
}
}
11 changes: 11 additions & 0 deletions xsd-parser/tests/feature/redefine/expected/serde_quick_xml.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
use serde::{Deserialize, Serialize};
pub type Foo = EnumType;
#[derive(Debug, Deserialize, Serialize)]
pub enum EnumType {
#[serde(rename = "1")]
_1,
#[serde(rename = "2")]
_2,
#[serde(rename = "3")]
_3,
}
38 changes: 38 additions & 0 deletions xsd-parser/tests/feature/redefine/expected/serde_xml_rs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use core::ops::{Deref, DerefMut};
use serde::{Deserialize, Serialize};
pub type Foo = EnumType;
#[derive(Debug, Deserialize, Serialize)]
pub struct EnumType {
#[serde(rename = "#text")]
pub value: EnumTypeValue,
}
impl From<EnumTypeValue> for EnumType {
fn from(value: EnumTypeValue) -> Self {
Self { value }
}
}
impl From<EnumType> for EnumTypeValue {
fn from(value: EnumType) -> Self {
value.value
}
}
impl Deref for EnumType {
type Target = EnumTypeValue;
fn deref(&self) -> &Self::Target {
&self.value
}
}
impl DerefMut for EnumType {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.value
}
}
#[derive(Debug, Deserialize, Serialize)]
pub enum EnumTypeValue {
#[serde(rename = "1")]
_1,
#[serde(rename = "2")]
_2,
#[serde(rename = "3")]
_3,
}
136 changes: 136 additions & 0 deletions xsd-parser/tests/feature/redefine/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
use xsd_parser::{
config::{Schema, SerdeXmlRsVersion},
Config, IdentType,
};

use crate::utils::{generate_test, ConfigEx};

fn config() -> Config {
let mut config = Config::test_default().with_generate([(IdentType::Element, "tns:Foo")]);
config
.parser
.schemas
.push(Schema::File("tests/feature/redefine/reference.xsd".into()));
config
}

/* default */

#[test]
fn generate_default() {
generate_test(
"tests/feature/redefine/schema.xsd",
"tests/feature/redefine/expected/default.rs",
config(),
);
}

#[cfg(not(feature = "update-expectations"))]
mod default {
#![allow(unused_imports)]

include!("expected/default.rs");
}

/* quick_xml */

#[test]
fn generate_quick_xml() {
generate_test(
"tests/feature/redefine/schema.xsd",
"tests/feature/redefine/expected/quick_xml.rs",
config().with_quick_xml(),
);
}

#[test]
#[cfg(not(feature = "update-expectations"))]
fn read_quick_xml() {
use quick_xml::{EnumType, Foo};

let obj =
crate::utils::quick_xml_read_test::<Foo, _>("tests/feature/redefine/example/default.xml");

assert!(matches!(obj, EnumType::_1));
}

#[test]
#[cfg(not(feature = "update-expectations"))]
fn write_quick_xml() {
use quick_xml::Foo;

let obj = Foo::_1;

crate::utils::quick_xml_write_test(
&obj,
"tns:Foo",
"tests/feature/redefine/example/default.xml",
);
}

#[cfg(not(feature = "update-expectations"))]
mod quick_xml {
#![allow(unused_imports)]

include!("expected/quick_xml.rs");
}

/* serde_xml_rs */

#[test]
fn generate_serde_xml_rs() {
generate_test(
"tests/feature/redefine/schema.xsd",
"tests/feature/redefine/expected/serde_xml_rs.rs",
config().with_serde_xml_rs(SerdeXmlRsVersion::Version08AndAbove),
);
}

#[test]
#[cfg(not(feature = "update-expectations"))]
fn read_serde_xml_rs() {
use serde_xml_rs::{EnumTypeValue, Foo};

let obj = crate::utils::serde_xml_rs_read_test::<Foo, _>(
"tests/feature/redefine/example/default.xml",
);

assert!(matches!(obj.value, EnumTypeValue::_1));
}

#[cfg(not(feature = "update-expectations"))]
mod serde_xml_rs {
#![allow(unused_imports)]

include!("expected/serde_xml_rs.rs");
}

/* serde_quick_xml */

#[test]
fn generate_serde_quick_xml() {
generate_test(
"tests/feature/redefine/schema.xsd",
"tests/feature/redefine/expected/serde_quick_xml.rs",
config().with_serde_quick_xml(),
);
}

#[test]
#[cfg(not(feature = "update-expectations"))]
fn read_serde_quick_xml() {
use serde_quick_xml::{EnumType, Foo};

let obj = crate::utils::serde_quick_xml_read_test::<Foo, _>(
"tests/feature/redefine/example/default.xml",
);

assert!(matches!(obj, EnumType::_1));
}

#[cfg(not(feature = "update-expectations"))]
mod serde_quick_xml {
#![allow(unused_imports)]

include!("expected/serde_quick_xml.rs");
}
12 changes: 12 additions & 0 deletions xsd-parser/tests/feature/redefine/reference.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:simpleType name="EnumType">
<xs:restriction base="xs:string">
<xs:enumeration value="1" />
<xs:enumeration value="2" />
<xs:enumeration value="3" />
</xs:restriction>
</xs:simpleType>

</xs:schema>
18 changes: 18 additions & 0 deletions xsd-parser/tests/feature/redefine/schema.xsd
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://example.com"
targetNamespace="http://example.com"
elementFormDefault="qualified"
>

<xs:redefine schemaLocation="reference.xsd">
<xs:simpleType name="EnumType">
<xs:restriction base="EnumType">
<xs:enumeration value="1" />
</xs:restriction>
</xs:simpleType>
</xs:redefine>

<xs:element name="Foo" type="EnumType" />
</xs:schema>