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 #1230

Closed
wants to merge 29 commits into from
Closed
Show file tree
Hide file tree
Changes from 27 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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets rename to @useSystemTextJsonConverter to make it more explicit what this is used for.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renamed

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll say it feels quite weird having this at the root of Tcgc namespace with this name that is very csharp specific

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is indeed csharp specific only decorator.
@timotheeguerin Are you implying we should keep it within csharp sub-namespace if we ensure it will not be used for other languages?

Copy link
Member

@timotheeguerin timotheeguerin Aug 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah just feels like this is not super compatible with the new approach of using a shared name and scope.

But if you feel like it's best then I'm not blocking or anything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have decided that language-specific decorators will go in the main namespace because of grow-up, but they will have to specify the scope, and we have an issue to have linter warnings if a decorator is applied to emitters that won't do anything with it.

My question with this PR is more: I thought we had decided that there was a use case for Java here too, and that there was going to be a redesign of this decorator (most likely just a rename), that would make it compatible with Java's scenario as well

Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,33 @@ model Foo {
model Bar {}
```

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

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

```typespec
@Azure.ClientGenerator.Core.hasJsonConverter(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
@hasJsonConverter
model MyModel {
prop: string;
}
```

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

:::warning
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ npm install --save-peer @azure-tools/typespec-client-generator-core
- [`@convenientAPI`](./decorators.md#@Azure.ClientGenerator.Core.convenientAPI)
- [`@exclude`](./decorators.md#@Azure.ClientGenerator.Core.exclude)
- [`@flattenProperty`](./decorators.md#@Azure.ClientGenerator.Core.flattenProperty)
- [`@hasJsonConverter`](./decorators.md#@Azure.ClientGenerator.Core.hasJsonConverter)
- [`@include`](./decorators.md#@Azure.ClientGenerator.Core.include)
- [`@internal`](./decorators.md#@Azure.ClientGenerator.Core.internal)
- [`@operationGroup`](./decorators.md#@Azure.ClientGenerator.Core.operationGroup)
Expand Down
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 @@ -19,6 +19,7 @@ npm install @azure-tools/typespec-client-generator-core
- [`@convenientAPI`](#@convenientapi)
- [`@exclude`](#@exclude)
- [`@flattenProperty`](#@flattenproperty)
- [`@hasJsonConverter`](#@hasjsonconverter)
- [`@include`](#@include)
- [`@internal`](#@internal)
- [`@operationGroup`](#@operationgroup)
Expand Down Expand Up @@ -371,6 +372,33 @@ model Foo {
model Bar {}
```

#### `@hasJsonConverter`

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

```typespec
@Azure.ClientGenerator.Core.hasJsonConverter(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
@hasJsonConverter
model MyModel {
prop: string;
}
```

#### `@include`

_Deprecated: @include decorator is deprecated. Use `@usage` and `@access` decorator instead._
Expand Down
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
* @hasJsonConverter
* model MyModel {
* prop: string;
* }
* ```
*/
export type HasJsonConverterDecorator = (
context: DecoratorContext,
target: Model,
scope?: string
) => void;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
$convenientAPI,
$exclude,
$flattenProperty,
$hasJsonConverter,
$include,
$internal,
$operationGroup,
Expand All @@ -22,6 +23,7 @@ import type {
ConvenientAPIDecorator,
ExcludeDecorator,
FlattenPropertyDecorator,
HasJsonConverterDecorator,
IncludeDecorator,
InternalDecorator,
OperationGroupDecorator,
Expand All @@ -44,6 +46,7 @@ type Decorators = {
$access: AccessDecorator;
$flattenProperty: FlattenPropertyDecorator;
$override: OverrideDecorator;
$hasJsonConverter: HasJsonConverterDecorator;
};

/** 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,
$hasJsonConverter,
};
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
* @hasJsonConverter
* model MyModel {
* prop: string;
* }
* ```
*/
extern dec hasJsonConverter(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 $hasJsonConverter: DecoratorFunction = (
live1206 marked this conversation as resolved.
Show resolved Hide resolved
context: DecoratorContext,
entity: Model,
scope?: LanguageScopes
live1206 marked this conversation as resolved.
Show resolved Hide resolved
) => {};
Original file line number Diff line number Diff line change
Expand Up @@ -327,4 +327,29 @@ describe("typespec-client-generator-core: general decorators list", () => {
]);
});
});

describe("csharp only decorator", () => {
live1206 marked this conversation as resolved.
Show resolved Hide resolved
live1206 marked this conversation as resolved.
Show resolved Hide resolved
it("@hasJsonConverter", async function () {
runner = await createSdkTestRunner(
{},
{ additionalDecorators: ["Azure\\.ClientGenerator\\.Core\\.@hasJsonConverter"] }
);

await runner.compileWithBuiltInService(`
@hasJsonConverter("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.@hasJsonConverter", arguments: { scope: "csharp" } },
]);
expectDiagnostics(runner.context.diagnostics, []);
});
});
});
Loading