Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 40 additions & 10 deletions packages/versioning/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ op newName(): void;

#### `@returnTypeChangedFrom`

Identifies when the target type changed.
Declares that the return type of an operation has changed starting at a given version,
while keeping earlier versions consistent with the previous return type.

This decorator is used to track return type changes across API versions. When applied,
the operation will return `oldType` in versions before the specified `version`,
and the current return type definition in the specified version and later.

```typespec
@TypeSpec.Versioning.returnTypeChangedFrom(version: EnumMember, oldType: unknown)
Expand All @@ -226,14 +231,28 @@ Identifies when the target type changed.

##### Parameters

| Name | Type | Description |
| ------- | ------------ | -------------------------------------------- |
| version | `EnumMember` | The version that the target type changed in. |
| oldType | `unknown` | The previous type of the target. |
| Name | Type | Description |
| ------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| version | `EnumMember` | The version when the return type change takes effect. The new return type applies<br />from this version onwards, while the old return type applies to earlier versions. |
| oldType | `unknown` | The previous return type used before the specified version. |

##### Examples

```tsp
// In v1: returns a string
// In v2+: returns an int32
@returnTypeChangedFrom(Versions.v2, string)
op getUserId(): int32;
```

#### `@typeChangedFrom`

Identifies when the target type changed.
Declares that the type of a model property has changed starting at a given version,
while keeping earlier versions consistent with the previous type.

This decorator is used to track type changes across API versions. When applied,
the property will use `oldType` in versions before the specified `version`,
and the current type definition in the specified version and later.

```typespec
@TypeSpec.Versioning.typeChangedFrom(version: EnumMember, oldType: unknown)
Expand All @@ -245,10 +264,21 @@ Identifies when the target type changed.

##### Parameters

| Name | Type | Description |
| ------- | ------------ | -------------------------------------------- |
| version | `EnumMember` | The version that the target type changed in. |
| oldType | `unknown` | The previous type of the target. |
| Name | Type | Description |
| ------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| version | `EnumMember` | The version when the type change takes effect. The new type applies<br />from this version onwards, while the old type applies to earlier versions. |
| oldType | `unknown` | The previous type used before the specified version. |

##### Examples

```tsp
model Foo {
// In v1: id is a string
// In v2+: id is an int32
@typeChangedFrom(Versions.v2, string)
id: int32;
}
```

#### `@useDependency`

Expand Down
40 changes: 34 additions & 6 deletions packages/versioning/generated-defs/TypeSpec.Versioning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,10 +202,25 @@ export type MadeRequiredDecorator = (
) => DecoratorValidatorCallbacks | void;

/**
* Identifies when the target type changed.
* Declares that the type of a model property has changed starting at a given version,
* while keeping earlier versions consistent with the previous type.
*
* @param version The version that the target type changed in.
* @param oldType The previous type of the target.
* This decorator is used to track type changes across API versions. When applied,
* the property will use `oldType` in versions before the specified `version`,
* and the current type definition in the specified version and later.
*
* @param version The version when the type change takes effect. The new type applies
* from this version onwards, while the old type applies to earlier versions.
* @param oldType The previous type used before the specified version.
* @example
* ```tsp
* model Foo {
* // In v1: id is a string
* // In v2+: id is an int32
* @typeChangedFrom(Versions.v2, string)
* id: int32;
* }
* ```
*/
export type TypeChangedFromDecorator = (
context: DecoratorContext,
Expand All @@ -215,10 +230,23 @@ export type TypeChangedFromDecorator = (
) => DecoratorValidatorCallbacks | void;

/**
* Identifies when the target type changed.
* Declares that the return type of an operation has changed starting at a given version,
* while keeping earlier versions consistent with the previous return type.
*
* This decorator is used to track return type changes across API versions. When applied,
* the operation will return `oldType` in versions before the specified `version`,
* and the current return type definition in the specified version and later.
*
* @param version The version that the target type changed in.
* @param oldType The previous type of the target.
* @param version The version when the return type change takes effect. The new return type applies
* from this version onwards, while the old return type applies to earlier versions.
* @param oldType The previous return type used before the specified version.
* @example
* ```tsp
* // In v1: returns a string
* // In v2+: returns an int32
* @returnTypeChangedFrom(Versions.v2, string)
* op getUserId(): int32;
* ```
*/
export type ReturnTypeChangedFromDecorator = (
context: DecoratorContext,
Expand Down
46 changes: 40 additions & 6 deletions packages/versioning/lib/decorators.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,49 @@ extern dec madeOptional(target: ModelProperty, version: EnumMember);
extern dec madeRequired(target: ModelProperty, version: EnumMember);

/**
* Identifies when the target type changed.
* @param version The version that the target type changed in.
* @param oldType The previous type of the target.
* Declares that the type of a model property has changed starting at a given version,
* while keeping earlier versions consistent with the previous type.
*
* This decorator is used to track type changes across API versions. When applied,
* the property will use `oldType` in versions before the specified `version`,
* and the current type definition in the specified version and later.
*
* @param version The version when the type change takes effect. The new type applies
* from this version onwards, while the old type applies to earlier versions.
* @param oldType The previous type used before the specified version.
*
* @example
*
* ```tsp
* model Foo {
* // In v1: id is a string
* // In v2+: id is an int32
* @typeChangedFrom(Versions.v2, string)
* id: int32;
* }
* ```
*/
extern dec typeChangedFrom(target: ModelProperty, version: EnumMember, oldType: unknown);

/**
* Identifies when the target type changed.
* @param version The version that the target type changed in.
* @param oldType The previous type of the target.
* Declares that the return type of an operation has changed starting at a given version,
* while keeping earlier versions consistent with the previous return type.
*
* This decorator is used to track return type changes across API versions. When applied,
* the operation will return `oldType` in versions before the specified `version`,
* and the current return type definition in the specified version and later.
*
* @param version The version when the return type change takes effect. The new return type applies
* from this version onwards, while the old return type applies to earlier versions.
* @param oldType The previous return type used before the specified version.
*
* @example
*
* ```tsp
* // In v1: returns a string
* // In v2+: returns an int32
* @returnTypeChangedFrom(Versions.v2, string)
* op getUserId(): int32;
* ```
*/
extern dec returnTypeChangedFrom(target: Operation, version: EnumMember, oldType: unknown);
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,12 @@ op newName(): void;

### `@returnTypeChangedFrom` {#@TypeSpec.Versioning.returnTypeChangedFrom}

Identifies when the target type changed.
Declares that the return type of an operation has changed starting at a given version,
while keeping earlier versions consistent with the previous return type.

This decorator is used to track return type changes across API versions. When applied,
the operation will return `oldType` in versions before the specified `version`,
and the current return type definition in the specified version and later.

```typespec
@TypeSpec.Versioning.returnTypeChangedFrom(version: EnumMember, oldType: unknown)
Expand All @@ -176,14 +181,28 @@ Identifies when the target type changed.

#### Parameters

| Name | Type | Description |
| ------- | ------------ | -------------------------------------------- |
| version | `EnumMember` | The version that the target type changed in. |
| oldType | `unknown` | The previous type of the target. |
| Name | Type | Description |
| ------- | ------------ | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| version | `EnumMember` | The version when the return type change takes effect. The new return type applies<br />from this version onwards, while the old return type applies to earlier versions. |
| oldType | `unknown` | The previous return type used before the specified version. |

#### Examples

```tsp
// In v1: returns a string
// In v2+: returns an int32
@returnTypeChangedFrom(Versions.v2, string)
op getUserId(): int32;
```

### `@typeChangedFrom` {#@TypeSpec.Versioning.typeChangedFrom}

Identifies when the target type changed.
Declares that the type of a model property has changed starting at a given version,
while keeping earlier versions consistent with the previous type.

This decorator is used to track type changes across API versions. When applied,
the property will use `oldType` in versions before the specified `version`,
and the current type definition in the specified version and later.

```typespec
@TypeSpec.Versioning.typeChangedFrom(version: EnumMember, oldType: unknown)
Expand All @@ -195,10 +214,21 @@ Identifies when the target type changed.

#### Parameters

| Name | Type | Description |
| ------- | ------------ | -------------------------------------------- |
| version | `EnumMember` | The version that the target type changed in. |
| oldType | `unknown` | The previous type of the target. |
| Name | Type | Description |
| ------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------- |
| version | `EnumMember` | The version when the type change takes effect. The new type applies<br />from this version onwards, while the old type applies to earlier versions. |
| oldType | `unknown` | The previous type used before the specified version. |

#### Examples

```tsp
model Foo {
// In v1: id is a string
// In v2+: id is an int32
@typeChangedFrom(Versions.v2, string)
id: int32;
}
```

### `@useDependency` {#@TypeSpec.Versioning.useDependency}

Expand Down