Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

We should consider removing #[non_exhaustive] from models used in requests #1856

Open
heaths opened this issue Oct 15, 2024 · 0 comments
Open
Assignees
Labels
Azure.Core The azure_core crate Client This issue points to a problem in the data-plane of the library. design-discussion An area of design currently under discussion and open to team and community feedback.
Milestone

Comments

@heaths
Copy link
Member

heaths commented Oct 15, 2024

The #[non_exhaustive] attribute enforces API consumers to have version-tolerant code with .. for pattern matches and catch-all match arms to help prevent breaking changes in the future if we were to add enum variants or model fields. This is why it's called out in the guidelines.

However, for models that must be constructed to be sent in requests, this prevents them from being constructed without a factory method outside the crate:

azure_security_keyvault_secrets/src/lib.rs

#[derive(Clone, Default)]
#[non_exhaustive]
pub struct Secret {
    pub name: Option<String>,
    pub version: Option<String>,
    pub value: Option<String>,
}

my_app/src/main.rs

// works
let secret = client.get_secret().await?;
let (name, version, value, ..) = secret;

// fails
let secret = Secret {
    name: Some("name".to_string()),
    value: Some("secret".to_string()),
    ...Default::default()
};

So instead we should consider only putting #[non_exhaustive] on response-only models or, perhaps, not at all. We should still put #[non_exhaustive] on both fixed and extensible enums, however.

See https://github.com/Azure/typespec-rust/issues/77 for an internal discussion with more context.

@heaths heaths self-assigned this Oct 15, 2024
@heaths heaths added Client This issue points to a problem in the data-plane of the library. Azure.Core The azure_core crate design-discussion An area of design currently under discussion and open to team and community feedback. labels Oct 15, 2024
@heaths heaths added this to the 2024-12 milestone Oct 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Azure.Core The azure_core crate Client This issue points to a problem in the data-plane of the library. design-discussion An area of design currently under discussion and open to team and community feedback.
Projects
None yet
Development

No branches or pull requests

1 participant