Azure / typespec-azure

About TypeSpec Azure Libraries
https://azure.github.io/typespec-azure/
MIT License
8 stars 28 forks source link

Client-side suggested defaults #910

Open timotheeguerin opened 1 month ago

timotheeguerin commented 1 month ago

Copied from https://github.com/Azure/typespec-azure-pr/issues/800

Per azure/typespec-azure-pr#728 We have a mechanism for providing service-side defaults, but need a mechanism that allows simplifying the client-side SDK by providing a suggested default value for required properties. This allows client SDKs to simplify an API, or prevent an SDK breaking change when there is a REST API breaking change making a formerly optional property required.

One proposal is to use a decorator @suggestedValue in cadl core that provides the value hint:

model SomeModel {
  @suggestedValue("Value that client SDKs may supply by default if they make this property optional") requiredThing: string;
}

Usages

Simple scalars

model Widget {
  @suggestedValue(1)
  count: int
}

Suggested client SKU

enum AccountType { Free, Basic, Advanced}
model WidgetAccount {
  @suggestedValue(AccountType.Basic) // AcountType.Basic::value??
  type: AccountType;
}

Default object or collection

model FirewallRule
{
   action: string;
   port: string;
   ipRange?: string;
}

model DefaultRule extends FirewallRule {
  action: "Deny";
  port: "*"
}

model AllowHttps extends FirewallRule {
  action: "Allow";
  port: 443;
}

model MyHttpsConnectedWidget {
  @suggestedValue([DefaultRule, AllowHttps])
  firewallRule: FirewallRule[];
  ...
}

As this applies to model properties, it would analogously apply to parameters

Questions / Considerations

iscai-msft commented 1 week ago

What would be the difference with a client-side default? @johanste is against them because their roundtripability isn't good