Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add @useSystemTextJsonConverter decorator for csharp #1336

Merged
merged 29 commits into from
Aug 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0e4606a
wip
live1206 Jul 24, 2024
ddcdc82
wip
live1206 Jul 24, 2024
a4ba0b3
add changelog and fix format
live1206 Jul 24, 2024
5668ad7
set sub-namespace for decorator
live1206 Jul 30, 2024
8f35f17
clean up
live1206 Jul 30, 2024
79effb3
Merge branch 'main' into json-converter
live1206 Jul 30, 2024
8cb7069
fix format
live1206 Jul 30, 2024
c532e9c
Merge branch 'json-converter' of https://github.com/live1206/typespec…
live1206 Jul 30, 2024
6d37315
fix format
live1206 Jul 30, 2024
1d26dfb
update doc
live1206 Jul 30, 2024
843aab5
Merge branch 'main' into json-converter
live1206 Jul 30, 2024
4e35b0b
regen docs
live1206 Jul 30, 2024
b51bfc8
Merge branch 'json-converter' of https://github.com/live1206/typespec…
live1206 Jul 30, 2024
2a8904c
update doc
live1206 Jul 30, 2024
bdec73d
rename to hasJsonConverter
live1206 Jul 31, 2024
796dda8
Merge branch 'main' into json-converter
live1206 Jul 31, 2024
e278c4f
update namespace
live1206 Jul 31, 2024
f9ffd8b
remove scope and implementation
live1206 Jul 31, 2024
4e37acd
regen
live1206 Jul 31, 2024
c385ce0
Merge branch 'main' into json-converter
live1206 Aug 6, 2024
1e9758d
Merge branch 'main' into json-converter
live1206 Aug 7, 2024
c558a6e
move decorator to root namespace
live1206 Aug 8, 2024
f5d5474
Merge branch 'main' into json-converter
live1206 Aug 8, 2024
a4d3e0e
fix test
live1206 Aug 8, 2024
bf00958
fix format
live1206 Aug 8, 2024
e796bd4
regen docs
live1206 Aug 8, 2024
1c499e7
add scope to implementation
live1206 Aug 8, 2024
4e0c38b
rename to useSystemTextJsonConverter
live1206 Aug 9, 2024
1fd1583
fix format
live1206 Aug 9, 2024
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
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, []);
});
});
});
Loading