Skip to content

[FEATURE REQ] please make it easier to instantiate objects #42913

Open
@pwmcintyre

Description

@pwmcintyre

Is your feature request related to a problem? Please describe.

I writing unit tests for some code which uses the SecretClient.

Imagine something like:

SecretClient client = mock(SecretClient.class);
when(client.getSecret("foo")).thenReturn(new KeyVaultSecret("foo", "secret"));

But the KeyVaultSecret has many properties my application uses, which I need to set, but there are no setters!

I have had to scrap together all sorts of workaround to get to this — here's how it looks:

// 1. create a bundle, some properties have no setter, so you need to serialize from JSON
SecretBundle bundle = SecretBundle.fromJson(createReader(
        """
        {
            "attributes": {
                "created": %d,
                "recoveryLevel": "%s",
                "enabled": true
            }
        }
        """.formatted(
                Instant.now().getLong(MILLI_OF_SECOND),
                RECOVERABLE
        )));

// 2. set the ID this way also sets the vault, name, and version
bundle.setId("%s/secrets/%s/%s".formatted(vault.replaceAll("/$",""), name, version));
bundle.setValue(new String(Hex.encodeHex(value)));

// 3. use this helper to make the secret from the bundle
KeyVaultSecret secret = createKeyVaultSecret(bundle);

// 4. finally, set some other stuff which are required
secret.getProperties().setExpiresOn(Instant.now().plusSeconds(600).atOffset(ZoneOffset.UTC));
return secret;

This is just one example, even harder is generic iterator classes like PagedIterable 🥵

Describe the solution you'd like

Builder patter would be ideal — eg:

KeyVaultSecret secret = new KeyVaultSecret.Builder()
    .vault("https://example.vault.azure.net/")
    .name("example")
    .version("aaaaa")
    .value(v)
    .build()

Additionally, maybe default createdOn, enabled, etc

Describe alternatives you've considered

more setters?

Additional context

NA

Information Checklist
Kindly make sure that you have added all the following information above and checkoff the required fields otherwise we will treat the issuer as an incomplete report

  • Description Added
  • Expected solution specified

Metadata

Metadata

Assignees

Labels

ClientThis issue points to a problem in the data-plane of the library.KeyVaultcustomer-reportedIssues that are reported by GitHub users external to the Azure organization.needs-team-attentionWorkflow: This issue needs attention from Azure service team or SDK teamquestionThe issue doesn't require a change to the product in order to be resolved. Most issues start as that

Type

No type

Projects

Status

Untriaged

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions