Skip to content

Conversation

@chunyu3
Copy link
Contributor

@chunyu3 chunyu3 commented Jul 19, 2024

Fix #4031

When we specify the allowed-emit-decorators in emitter, TCGC will help to emit out decoration information for each item (client, property, operation), e.g final-state-var, our .NET input-Model need to contain this information.

This PR will

  • emit decorators to all kind of models, operations and operations
  • add reference resolve strategy in TypeSpecInputDecoratorInfoConverter to support parse by reference

@chunyu3 chunyu3 marked this pull request as draft July 19, 2024 07:13
@microsoft-github-policy-service microsoft-github-policy-service bot added the emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp label Jul 19, 2024
@chunyu3 chunyu3 changed the title Emit decorator list [Do-not-merge] Emit decorator list Jul 19, 2024
@azure-sdk
Copy link
Collaborator

azure-sdk commented Jul 19, 2024

API change check

APIView has identified API level changes in this PR and created following API reviews.

@typespec/http-client-csharp
Microsoft.Generator.CSharp.Input

@chunyu3 chunyu3 changed the title [Do-not-merge] Emit decorator list Emit decorator list Jul 26, 2024
@chunyu3 chunyu3 changed the title Emit decorator list Emit decorator list on models Jul 26, 2024
@chunyu3
Copy link
Contributor Author

chunyu3 commented Jul 26, 2024

autorest.csharp regen preview PR: Azure/autorest.csharp#4940

@chunyu3 chunyu3 marked this pull request as ready for review July 26, 2024 11:23
@chunyu3 chunyu3 requested a review from ArcturusZhang August 9, 2024 08:44
@chunyu3 chunyu3 force-pushed the emitDecorator branch 2 times, most recently from 8b1ee70 to 5b30017 Compare August 14, 2024 02:29
Copy link
Member

@archerzz archerzz left a comment

Choose a reason for hiding this comment

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

Do we have a test case where Decorators is not empty array?

@archerzz
Copy link
Member

Previously I thought it's quite natural to pass decorators to generator. But after seeing the codes, I hesitate, mainly because:

  1. In some cases, we have to process the decorators before passing input json to generator. Do we still need to pass the original data?
  2. Swagger input will be converted to input model first. If a metadata happens to exist in both typespec decorator and swagger x-ms-xxx directive, we definitely don't want to covert swagger directive into typespec decorator. We should go approach 1 above.
  3. It looks quite painful to parse the DecoratorInfo in csharp.

@m-nash @ArcturusZhang your thoughts?

@chunyu3
Copy link
Contributor Author

chunyu3 commented Aug 15, 2024

3. It looks quite painful to parse the DecoratorInfo in csharp.

For #1, we only need to pass some special set(which is small) of decorators which may not common, or be special decorators defined in customized typespec library/emitter. Common decorators we will parse the information to our input model and will not pass to generator

#2, I think the swagger derective (all its corresponds typespec decorator) does not belong to the decorators to emit.
#3 We only need to parse known decorator's information (we know the definition/structure of the decorator to parse). Not generic parse for all decorators.

@ArcturusZhang
Copy link
Member

Previously I thought it's quite natural to pass decorators to generator. But after seeing the codes, I hesitate, mainly because:

  1. In some cases, we have to process the decorators before passing input json to generator. Do we still need to pass the original data?
  2. Swagger input will be converted to input model first. If a metadata happens to exist in both typespec decorator and swagger x-ms-xxx directive, we definitely don't want to covert swagger directive into typespec decorator. We should go approach 1 above.
  3. It looks quite painful to parse the DecoratorInfo in csharp.

@m-nash @ArcturusZhang your thoughts?

So we could categorize the decorators into two types:
The first type of decorators really changes something on the typespec, such as this. These kind of decorators have done their work in tsp compilation because they changes the type graph, and TCGC does not even feel the existence of them
The second type of the decorators are "flags". They do nothing on the typespec itself, such as the @useSystemTextJsonConverter decorator. For this kind of decorators, it makes sense that we pass through their data to the generator.

I think this should resolve your first concern.

For 2, some extensions will change the spec, for instance some x-ms-format extension changes the type, for this kind of extensions, our code model converter should do their work correctly by convert it to its actual types, and it should never be a decorator in the decorator list. Some other extensions are just flags, these needs to be put into the decorator list.

For 3, you might need to specify. It is always painful when we want to convert an unknown type into C#, it is inevitable.

@archerzz
Copy link
Member

archerzz commented Aug 15, 2024

Previously I thought it's quite natural to pass decorators to generator. But after seeing the codes, I hesitate, mainly because:

  1. In some cases, we have to process the decorators before passing input json to generator. Do we still need to pass the original data?
  2. Swagger input will be converted to input model first. If a metadata happens to exist in both typespec decorator and swagger x-ms-xxx directive, we definitely don't want to covert swagger directive into typespec decorator. We should go approach 1 above.
  3. It looks quite painful to parse the DecoratorInfo in csharp.

@m-nash @ArcturusZhang your thoughts?

So we could categorize the decorators into two types: The first type of decorators really changes something on the typespec, such as this. These kind of decorators have done their work in tsp compilation because they changes the type graph, and TCGC does not even feel the existence of them The second type of the decorators are "flags". They do nothing on the typespec itself, such as the @useSystemTextJsonConverter decorator. For this kind of decorators, it makes sense that we pass through their data to the generator.

I think this should resolve your first concern.

For 2, some extensions will change the spec, for instance some x-ms-format extension changes the type, for this kind of extensions, our code model converter should do their work correctly by convert it to its actual types, and it should never be a decorator in the decorator list. Some other extensions are just flags, these needs to be put into the decorator list.

For 3, you might need to specify. It is always painful when we want to convert an unknown type into C#, it is inevitable.

@ArcturusZhang thanks. For 3, I just found that we could have a TypeSpecInputDecoratorConverter, so in the future we can have concrete type for the decorators we do care in generator. Problem solved.

I wander if we can identify the type of argument value according to the json-element-type. If not, I will file an request to ask TCGC to emit valueType for the arguments of decorator which will help us to correct convert the value from json.

@chunyu3 chunyu3 added this pull request to the merge queue Aug 15, 2024
Merged via the queue into microsoft:main with commit 1dc5711 Aug 15, 2024
@chunyu3 chunyu3 deleted the emitDecorator branch August 15, 2024 08:29
sarangan12 pushed a commit to sarangan12/typespec that referenced this pull request Sep 16, 2024
Fix microsoft#4031

When we specify the allowed-emit-decorators in emitter, TCGC will help
to emit out decoration information for each item (client, property,
operation), e.g final-state-var, our .NET input-Model need to contain
this information.

This PR will 

- emit decorators to all kind of models, operations and operations
- add reference resolve strategy in TypeSpecInputDecoratorInfoConverter
to support parse by reference
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:csharp Issue for the C# client emitter: @typespec/http-client-csharp

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Emit decorator list for models

7 participants