Skip to content

apply us-east-1 as default region if not set by user in with_test_defaults_v2() #4312

Merged
aajtodd merged 9 commits intomainfrom
aajtodd/smithy-mocks-misc
Oct 14, 2025
Merged

apply us-east-1 as default region if not set by user in with_test_defaults_v2() #4312
aajtodd merged 9 commits intomainfrom
aajtodd/smithy-mocks-misc

Conversation

@aajtodd
Copy link
Contributor

@aajtodd aajtodd commented Sep 22, 2025

Motivation and Context

Description

  • Adds a new with_test_defaults_v2() method to all clients supporting region configuration. This new method applies us-east-1 as default region if not set by user. This allows aws-smithy-mocks to work for non AWS SDK generated clients. This is only set as the default if a user has not already supplied one.
    • NOTE: We are choosing to add a new method here for backwards compatibility concerns (our own generated endpoint tests break because of precedence problems between endpoint rules and with_test_defaults).
  • Clarify test-util feature requirement when using aws-smithy-mocks.

Checklist

  • For changes to the smithy-rs codegen or runtime crates, I have created a changelog entry Markdown file in the .changelog directory, specifying "client," "server," or both in the applies_to key.
  • For changes to the AWS SDK, generated SDK code, or SDK runtime crates, I have created a changelog entry Markdown file in the .changelog directory, specifying "aws-sdk-rust" in the applies_to key.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@aajtodd aajtodd requested review from a team as code owners September 22, 2025 16:00
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

Copy link
Contributor

@landonxjames landonxjames left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some questions on the README, no blockers

## Prerequisites

<div class="warning">
You must enable the `test-util` feature of the service client crate in order to use the `mock_client` macro.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know its kind of implicit in the example below that test-util should only be enabled for [dev-dependencies], but should we make that explicit and warn users only to use those features for testing?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This happens so frequently that I wonder if we should design a really nice error where the mock_client invokes a macro we define in the crates and if test-util isn't enabled it uses compile_fail! To give a precise error.

Can't do it here obviously since we'd need to add that macro first in the generated crates

@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

rustTemplate(
"""
if ${section.configBuilderRef}.config.load::<#{Region}>().is_none() {
self.set_region(#{Some}(#{Region}::new("us-east-1")));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it worth a BMV for this or other opt-out? I feel like there is a very high probability that someones unit test will be broken by this and although the fix won't be hard they might appreciate a way to hold back to keep their tests working

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Our own unit tests are broken by this and I'm not sure whether we want to ship this right now as we have introduced a precedence problem for defaults between with_test_defaults and endpoint builtins.


Generated test code:

// Code generated by software.amazon.smithy.rust.codegen.smithy-rs. DO NOT EDIT.
#![cfg(feature = "test-util")]
#[::tokio::test]
async fn operation_input_test_test_operation_1() {
    /* documentation: region should fallback to the default */
    /* builtIns: {} */
    /* clientParams: {} */
    let (http_client, rcvr) = ::aws_smithy_http_client::test_util::capture_request(None);
    let conf = {
        #[allow(unused_mut)]
        let mut builder = endpoint_test_service::Config::builder().with_test_defaults().http_client(http_client);
        builder.build()
    };
    let client = endpoint_test_service::Client::from_conf(conf);
    let _result = dbg!(
        client
            .test_operation()
            .set_bar(::std::option::Option::Some(
                endpoint_test_service::types::Bar::builder()
                    .set_f(::std::option::Option::Some("blah".to_owned()))
                    .build()
            ))
            .send()
            .await
    );
    let req = rcvr.expect_request();
    let uri = req.uri().to_string();
    assert!(
        uri.starts_with("https://prod.us-east-2.api.myservice.aws.dev/"),
        "expected URI to start with `https://prod.us-east-2.api.myservice.aws.dev/` but it was `{}`",
        uri
    );
}

failure:

running 1 test
test operation_input_test_test_operation_1 ... FAILED

failures:

---- operation_input_test_test_operation_1 stdout ----
[endpoint-test-service/rust-client-codegen/tests/endpoint_tests.rs:15:19] client.test_operation().set_bar(::std::option::Option::Some(endpoint_test_service::types::Bar::builder().set_f(::std::option::Option::Some("blah".to_owned())).build())).send().await = Ok(
    TestOperationOutput {
        _request_id: None,
    },
)

thread 'operation_input_test_test_operation_1' panicked at endpoint-test-service/rust-client-codegen/tests/endpoint_tests.rs:28:5:
expected URI to start with `https://prod.us-east-2.api.myservice.aws.dev/` but it was `https://prod.us-east-1.api.myservice.aws.dev/foo`
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace


failures:
    operation_input_test_test_operation_1

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s

this test is generated by: https://github.com/smithy-lang/smithy-rs/blob/2016a67af3beeb4c976da9fb3d867f15822e[…]/amazon/smithy/rustsdk/endpoints/OperationInputTestGenerator.kt
Interestingly it shows nothing for builtins

but effectively it's relying on the default builtin value of us-east-2which it would not be now because with_test_defaults sets a different default when unset

@aajtodd aajtodd force-pushed the aajtodd/smithy-mocks-misc branch from 3fed2bf to 9a975ff Compare October 13, 2025 14:11
@aajtodd aajtodd changed the title apply us-east-1 as default region if not set by user in with_test_defaults() apply us-east-1 as default region if not set by user in with_test_defaults_v2() Oct 13, 2025
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@aajtodd aajtodd force-pushed the aajtodd/smithy-mocks-misc branch from 8a0a45d to 109027c Compare October 13, 2025 17:15
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@aajtodd aajtodd force-pushed the aajtodd/smithy-mocks-misc branch from 109027c to c85c7fa Compare October 13, 2025 18:13
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@aajtodd aajtodd force-pushed the aajtodd/smithy-mocks-misc branch from c85c7fa to 476685f Compare October 14, 2025 12:54
@github-actions
Copy link

A new generated diff is ready to view.

A new doc preview is ready to view.

@aajtodd aajtodd merged commit f36458a into main Oct 14, 2025
50 checks passed
@aajtodd aajtodd deleted the aajtodd/smithy-mocks-misc branch October 14, 2025 13:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants