Unverified Commit 613da815 authored by Harry Barber's avatar Harry Barber Committed by GitHub
Browse files

Use . delimiter in OperationExtension (#1651)

* Change OperationExtension to require "."

* Switch codegeneration to use "."
parent 9bfd1b2c
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -115,7 +115,7 @@ open class ServerOperationHandlerGenerator(
                        $callImpl
                        let output_wrapper: $outputWrapperName = output_inner.into();
                        let mut response = output_wrapper.into_response();
                        let operation_ext = #{SmithyHttpServer}::extension::OperationExtension::new("${operation.id.namespace}##$operationName").expect("malformed absolute shape ID");
                        let operation_ext = #{SmithyHttpServer}::extension::OperationExtension::new("${operation.id.namespace}.$operationName").expect("malformed absolute shape ID");
                        response.extensions_mut().insert(operation_ext);
                        response.map(#{SmithyHttpServer}::body::boxed)
                    }
+1 −1
Original line number Diff line number Diff line
@@ -533,7 +533,7 @@ class ServerProtocolTestGenerator(
        )
        rustWriter.writeWithNoFormatting(
            """
            assert_eq!(operation_extension.operation(), format!("{}#{}", "${operationShape.id.namespace}", "${operationSymbol.name}"));
            assert_eq!(operation_extension.absolute(), format!("{}.{}", "${operationShape.id.namespace}", "${operationSymbol.name}"));
            """.trimIndent(),
        )
    }
+30 −4
Original line number Diff line number Diff line
@@ -58,6 +58,8 @@ use crate::request::RequestParts;
/// This extension type is set when it has been correctly determined that the request should be
/// routed to a particular operation. The operation handler might not even get invoked because the
/// request fails to deserialize into the modeled operation input.
///
/// The format given must be the absolute shape ID with `#` replaced with a `.`.
#[derive(Debug, Clone)]
pub struct OperationExtension {
    absolute: &'static str,
@@ -67,18 +69,18 @@ pub struct OperationExtension {
}

/// An error occurred when parsing an absolute operation shape ID.
#[derive(Debug, Clone, Error)]
#[derive(Debug, Clone, Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum ParseError {
    #[error("# was not found - missing namespace")]
    #[error(". was not found - missing namespace")]
    MissingNamespace,
}

impl OperationExtension {
    /// Creates a new [`OperationExtension`] from the absolute shape ID of the operation.
    /// Creates a new [`OperationExtension`] from the absolute shape ID of the operation with `#` symbol replaced with a `.`.
    pub fn new(absolute_operation_id: &'static str) -> Result<Self, ParseError> {
        let (namespace, name) = absolute_operation_id
            .split_once('#')
            .rsplit_once('.')
            .ok_or(ParseError::MissingNamespace)?;
        Ok(Self {
            absolute: absolute_operation_id,
@@ -188,3 +190,27 @@ where

    Ok(Extension(value))
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn ext_accept() {
        let value = "com.amazonaws.ebs.CompleteSnapshot";
        let ext = OperationExtension::new(value).unwrap();

        assert_eq!(ext.absolute(), value);
        assert_eq!(ext.namespace(), "com.amazonaws.ebs");
        assert_eq!(ext.name(), "CompleteSnapshot");
    }

    #[test]
    fn ext_reject() {
        let value = "CompleteSnapshot";
        assert_eq!(
            OperationExtension::new(value).unwrap_err(),
            ParseError::MissingNamespace
        )
    }
}