Skip to content

Commit

Permalink
Add @useSystemTextJsonConverter decorator for csharp (#1336)
Browse files Browse the repository at this point in the history
Resolves #1229
This change has already been approved in
#1230
  • Loading branch information
live1206 committed Aug 9, 2024
1 parent 01ba634 commit 5b46eef
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .chronus/changes/json-converter-2024-6-24-16-48-41.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
changeKind: feature
packages:
- "@azure-tools/typespec-client-generator-core"
---

Add `@hasJsonConverter` for csharp only to indicate if JSON converter is needed
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,30 @@ model Origin {
@get
op getModel(): Fish;
```

### `@useSystemTextJsonConverter` {#@Azure.ClientGenerator.Core.useSystemTextJsonConverter}

Whether a model needs the custom JSON converter, this is only used for backward compatibility for csharp.

```typespec
@Azure.ClientGenerator.Core.useSystemTextJsonConverter(scope?: valueof string)
```

#### Target

`Model`

#### Parameters

| Name | Type | Description |
| ----- | ---------------- | ------------------------------------------------------------------------------------------------------------- |
| scope | `valueof string` | The language scope you want this decorator to apply to. If not specified, will apply to all language emitters |

#### Examples

```typespec
@useSystemTextJsonConverter
model MyModel {
prop: string;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ npm install --save-peer @azure-tools/typespec-client-generator-core
- [`@override`](./decorators.md#@Azure.ClientGenerator.Core.override)
- [`@protocolAPI`](./decorators.md#@Azure.ClientGenerator.Core.protocolAPI)
- [`@usage`](./decorators.md#@Azure.ClientGenerator.Core.usage)
- [`@useSystemTextJsonConverter`](./decorators.md#@Azure.ClientGenerator.Core.useSystemTextJsonConverter)
28 changes: 28 additions & 0 deletions packages/typespec-client-generator-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ npm install @azure-tools/typespec-client-generator-core
- [`@override`](#@override)
- [`@protocolAPI`](#@protocolapi)
- [`@usage`](#@usage)
- [`@useSystemTextJsonConverter`](#@usesystemtextjsonconverter)

#### `@access`

Expand Down Expand Up @@ -625,3 +626,30 @@ model Origin {
@get
op getModel(): Fish;
```

#### `@useSystemTextJsonConverter`

Whether a model needs the custom JSON converter, this is only used for backward compatibility for csharp.

```typespec
@Azure.ClientGenerator.Core.useSystemTextJsonConverter(scope?: valueof string)
```

##### Target

`Model`

##### Parameters

| Name | Type | Description |
| ----- | ---------------- | ------------------------------------------------------------------------------------------------------------- |
| scope | `valueof string` | The language scope you want this decorator to apply to. If not specified, will apply to all language emitters |

##### Examples

```typespec
@useSystemTextJsonConverter
model MyModel {
prop: string;
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -483,3 +483,21 @@ export type OverrideDecorator = (
override: Operation,
scope?: string
) => void;

/**
* Whether a model needs the custom JSON converter, this is only used for backward compatibility for csharp.
*
* @param scope The language scope you want this decorator to apply to. If not specified, will apply to all language emitters
* @example
* ```typespec
* @useSystemTextJsonConverter
* model MyModel {
* prop: string;
* }
* ```
*/
export type UseSystemTextJsonConverterDecorator = (
context: DecoratorContext,
target: Model,
scope?: string
) => void;
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
$override,
$protocolAPI,
$usage,
$useSystemTextJsonConverter,
} from "@azure-tools/typespec-client-generator-core";
import type {
AccessDecorator,
Expand All @@ -28,6 +29,7 @@ import type {
OverrideDecorator,
ProtocolAPIDecorator,
UsageDecorator,
UseSystemTextJsonConverterDecorator,
} from "./Azure.ClientGenerator.Core.js";

type Decorators = {
Expand All @@ -44,6 +46,7 @@ type Decorators = {
$access: AccessDecorator;
$flattenProperty: FlattenPropertyDecorator;
$override: OverrideDecorator;
$useSystemTextJsonConverter: UseSystemTextJsonConverterDecorator;
};

/** An error here would mean that the exported decorator is not using the same signature. Make sure to have export const $decName: DecNameDecorator = (...) => ... */
Expand All @@ -61,4 +64,5 @@ const _: Decorators = {
$access,
$flattenProperty,
$override,
$useSystemTextJsonConverter,
};
14 changes: 14 additions & 0 deletions packages/typespec-client-generator-core/lib/decorators.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -467,3 +467,17 @@ extern dec flattenProperty(target: ModelProperty, scope?: valueof string);
* ```
*/
extern dec override(original: Operation, override: Operation, scope?: valueof string);

/**
* Whether a model needs the custom JSON converter, this is only used for backward compatibility for csharp.
* @param scope The language scope you want this decorator to apply to. If not specified, will apply to all language emitters
*
* @example
* ```typespec
* @useSystemTextJsonConverter
* model MyModel {
* prop: string;
* }
* ```
*/
extern dec useSystemTextJsonConverter(target: Model, scope?: valueof string);
6 changes: 6 additions & 0 deletions packages/typespec-client-generator-core/src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1135,3 +1135,9 @@ export function getOverriddenClientMethod(
): Operation | undefined {
return getScopedDecoratorData(context, overrideKey, entity);
}

export const $useSystemTextJsonConverter: DecoratorFunction = (
context: DecoratorContext,
entity: Model,
scope?: LanguageScopes
) => {};
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,32 @@ describe("typespec-client-generator-core: general decorators list", () => {
]);
});
});

describe("csharp only decorator", () => {
it("@useSystemTextJsonConverter", async function () {
runner = await createSdkTestRunner(
{},
{ additionalDecorators: ["Azure\\.ClientGenerator\\.Core\\.@useSystemTextJsonConverter"] }
);

await runner.compileWithBuiltInService(`
@useSystemTextJsonConverter("csharp")
model A {
id: string;
}
op test(): A;
`);

const models = runner.context.sdkPackage.models;
strictEqual(models.length, 1);
deepStrictEqual(models[0].decorators, [
{
name: "Azure.ClientGenerator.Core.@useSystemTextJsonConverter",
arguments: { scope: "csharp" },
},
]);
expectDiagnostics(runner.context.diagnostics, []);
});
});
});

0 comments on commit 5b46eef

Please sign in to comment.