Skip to main content
Version: Latest (Core: 0.60.x, Azure: 0.46.x)

Client renaming

This page documents how to customize names for client generation. For an overview of the setup, please visit the setup page.

note

The TypeSpec compiler provides an @encodedName decorator that allows changing the name of the property for a given serialization format. However in Azure we recommend that you define the property name as the value sent on the wire and use the @clientName decorator to change the name of the generated property.

Default behaviors​

By default, any language code generator will assume the TYPESPEC name is the client. For clarity, generators do not attempt to do any auto-magic rename.

NOTE: While names are not transformed, they will be adapted to what is idiomatic of the language (Python snake_case, etc.).

namespace PetStoreNamespace;

@doc("This is the input I need")
@resource("input")
model InputModel {
@key
@doc("Id of this object")
@visibility("read")
name: string;
}

@doc("Read my resource")
op GetModel is ResourceRead<InputModel>;

Customizations​

Model names​

Renames in the context of models can be done on the model name and the attribute name. Renames have a target:

  • client means that all client will use that name
  • csharp, javascript, python, java means you target this specific language

Language target takes priority over client target.

NOTE: As model name do not get serialized as JSON, sometimes the best choice is to rename the main TYPESPEC for clarity. Talk to your emitter contact is you're unsure if you should rename the model in the main TYPESPEC or customize it.

import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;

@@clientName(InputModel, "InputOptions"); // Use InputOptions as a base name in clients
@@clientName(InputModel, "ParameterOptions", "csharp"); // Prefer a different name for C# only

@@clientName(InputModel.name, "input_name", "python"); // Python may need a different to be idiomatic

Rename operations and parameters​

import "./main.tsp";
import "@azure-tools/typespec-client-generator-core";

using Azure.ClientGenerator.Core;

@@clientName(GetModel, "ReadModel"); // Use InputOptions as a base name in clients
@@clientName(GetModel, "GetComputedModel", "python"); // Note that Python will still snake_case it

Implementation​

Order of Operations​

For consistency when generating code, the order in which overrides are applied is important. Code emitters should apply overrides in the following order.

Over-the-Wire JSON Names​

For determining the final name of a TypeSpec entity when sent over-the-wire in JSON:

  1. If the @encodedName decorator exists, use this value
  2. Use the original name in the spec

Client SDK Names​

For determining the final name of a TypeSpec entity when used in a client SDK (e.g. Python):

  1. Check if there's a scoped @clientName decorator for your emitter
  2. Check if there's a @clientName decorator at all
  3. Check the friendly name
  4. Use the original name in the spec

Note: If the object name is from @clientName decorator, do not apply your language's casing rules on it. If it's not, apply your language's heuristics (i.e. for Python, apply snake casing)