Unverified Commit 6a042ab1 authored by Michael Rossberg's avatar Michael Rossberg Committed by GitHub
Browse files

Fix s3UnwrappedXmlOutput of LocationConstraint (#127)



* Fix s3UnwrappedXmlOutput of LocationConstraint

* Remove field in struct type

* add canary assertion

* add xml test

---------

Co-authored-by: default avatarNugine <nugine@foxmail.com>
parent 400f0752
Loading
Loading
Loading
Loading
+10 −0
Original line number Diff line number Diff line
@@ -24,6 +24,8 @@ pub struct Operation {
    pub smithy_input: String,
    pub smithy_output: String,

    pub s3_unwrapped_xml_output: bool,

    pub doc: Option<String>,

    pub http_method: String,
@@ -77,6 +79,12 @@ pub fn collect_operations(model: &smithy::Model) -> Operations {
            smithy_http_code
        };

        // https://smithy.io/2.0/aws/customizations/s3-customizations.html
        // https://github.com/Nugine/s3s/pull/127
        if sh.traits.s3_unwrapped_xml_output() {
            assert_eq!(op_name, "GetBucketLocation");
        }

        let op = Operation {
            name: op_name.clone(),

@@ -86,6 +94,8 @@ pub fn collect_operations(model: &smithy::Model) -> Operations {
            smithy_input,
            smithy_output,

            s3_unwrapped_xml_output: sh.traits.s3_unwrapped_xml_output(),

            doc: sh.traits.doc().map(o),

            http_method: sh.traits.http_method().unwrap().to_owned(),
+4 −0
Original line number Diff line number Diff line
@@ -209,6 +209,10 @@ impl Traits {
        self.get("smithy.api#xmlFlattened").is_some()
    }

    pub fn s3_unwrapped_xml_output(&self) -> bool {
        self.get("aws.customizations#s3UnwrappedXmlOutput").is_some()
    }

    pub fn http_label(&self) -> Option<&Value> {
        self.get("smithy.api#httpLabel")
    }
+8 −1
Original line number Diff line number Diff line
@@ -154,8 +154,15 @@ fn codegen_xml_ser(ops: &Operations, rust_types: &RustTypes) {
                            g!("s.timestamp(\"{}\", &self.{}, TimestampFormat::{})?;", xml_name, field.name, fmt);
                        }
                    } else if field.option_type {
                        let s3_unwrapped_xml_output =
                            ops.iter().any(|(_, op)| op.s3_unwrapped_xml_output && op.output == ty.name);

                        g!("if let Some(ref val) = self.{} {{", field.name);
                        if s3_unwrapped_xml_output {
                            g!("val.serialize_content(s)?;");
                        } else {
                            g!("s.content(\"{xml_name}\", val)?;");
                        }
                        g!("}}");
                    } else {
                        let default_is_zero = match field.default_value.as_ref() {
+1 −1
Original line number Diff line number Diff line
@@ -505,7 +505,7 @@ impl SerializeContent for GetBucketLifecycleConfigurationOutput {
impl SerializeContent for GetBucketLocationOutput {
    fn serialize_content<W: Write>(&self, s: &mut Serializer<W>) -> SerResult {
        if let Some(ref val) = self.location_constraint {
            s.content("LocationConstraint", val)?;
            val.serialize_content(s)?;
        }
        Ok(())
    }
+34 −0
Original line number Diff line number Diff line
@@ -19,6 +19,15 @@ fn serialize_content<T: xml::SerializeContent>(val: &T) -> xml::SerResult<String
    Ok(String::from_utf8(buf).unwrap())
}

fn serialize<T: xml::Serialize>(val: &T) -> xml::SerResult<String> {
    let mut buf = Vec::with_capacity(256);
    {
        let mut ser = xml::Serializer::new(&mut buf);
        val.serialize(&mut ser)?;
    }
    Ok(String::from_utf8(buf).unwrap())
}

/// See <https://github.com/Nugine/s3s/issues/2>
#[test]
fn d001() {
@@ -146,3 +155,28 @@ fn s001() {

    assert_eq!(ans, expected);
}

#[test]
fn s002() {
    {
        let us_west_2 = crate::dto::BucketLocationConstraint::from_static(crate::dto::BucketLocationConstraint::US_WEST_2);
        let val = crate::dto::GetBucketLocationOutput {
            location_constraint: Some(us_west_2),
        };

        let ans = serialize(&val).unwrap();
        let expected = "<LocationConstraint>us-west-2</LocationConstraint>";

        assert_eq!(ans, expected);
    }
    {
        let val = crate::dto::GetBucketLocationOutput {
            location_constraint: None,
        };

        let ans = serialize(&val).unwrap();
        let expected = "<LocationConstraint></LocationConstraint>";

        assert_eq!(ans, expected);
    }
}