Skip to content

Latest commit

 

History

History
146 lines (114 loc) · 2.84 KB

projected-names.md

File metadata and controls

146 lines (114 loc) · 2.84 KB
id title
projected-names
Projected names

Projected Names

:::warning Projected names is a legacy feature and will be phased out. Please use Encoded Names instead for changing the name over the wire. :::

There is some cases where the name you have in TypeSpec might differ from the name over the wire or for a certain language.

Known targets

List of known targets.

  • Wire:
    • json: Configure JSON representation of data
    • xml: Configure XML representation of data
  • Language:
    • csharp: Configure C# generated code
    • java: Configure Java generated code
    • python: Configure python generated code
    • javascript: Configure javascript generated code
    • swift : Configure swift generated code
    • c : Configure C generated code
  • Type:
    • client: Configure output for the client
    • server: Configure output for the server

Update name for a given target

With decorator

To update the name of a TypeSpec entity you can use the @projectedName decorator. This decorator takes 2 parameters:

  • string target name. See known targets
  • string projected name. Whatever the name should be in the given target.

Example:

model Foo {
  // Specify that when serializing to JSON `expireAt` property should be named `exp`
  @projectedName("json", "exp")
  expireAt: string;
}

With projection

The decorator is just a syntax sugar for the target projection behind the scenes. In more complex cases you might want to just implement the projection manually.

model Foo {
  expireAt: string;
}

projection Foo#target {
  to(targetName) {
    if targetName == "json" {
      self::rename("exp");
    };
  }
}

Example

model CertificateAttributes {
  @projectedName("json", "nbf")
  @projectedName("csharp", "ValidAfter")
  notBefore: int32;

  @projectedName("json", "exp")
  expires: int32;

  @projectedName("client", "createdAt")
  created: int32;

  updated: int32;
}
Json Typescript CSharp
When serialized to Json property use the json projected name Typescript didn't provide any projected name so it keep the model as it is. Model uses the `csharp` projected names and keeps the reference to the JSON name in JsonProperty
{
  "nbf": 1430344421,
  "exp": 2208988799,
  "created": 1493938289,
  "updated": 1493938291
}
interface Attributes {
  notBefore: number;
  expires: number;
  createdAt: number;
  updated: number;
}
class CertificateAttributes
{
  [JsonProperty("nbf")]
  public int ValidAfter {get; set;}

  [JsonProperty("exp")]
  public int Expires {get; set;}

  [JsonProperty("created")]
  public int CreatedAt {get; set;}

  public int Updated {get; set;}
}