Azure / typespec-azure

About TypeSpec Azure Libraries
https://azure.github.io/typespec-azure/
MIT License
13 stars 37 forks source link

[TCGC] Add `Spread` tag in the operation body parameter #1513

Open chunyu3 opened 4 weeks ago

chunyu3 commented 4 weeks ago

When an operation's body parameter is spread, the Usage of the body parameter model will append Usage. But spread is a meta-data of a body parameter, because a model can be used in more than one operations, and not all operations will spread the body.

e.g.

model Thing
{
  id: string;
  age: int32;
}

op spread(...Thing): void
op op2(@body thing:Thing): void

The body of op2 will be spread by mistake.

solution: tag spread in the SdkHttpParameter instead of body model type Usage

msyyc commented 4 weeks ago

FYI: For now, emitters could use the following way to judge it

function isSpreadBody(bodyParam: SdkBodyParameter | undefined): boolean {   return bodyParam?.type.kind === "model" && bodyParam.type !== bodyParam.correspondingMethodParams[0]?.type;}
haolingdong-msft commented 4 weeks ago

Another concern I have for spread usage of model is: for emitters, we have to translate model usage into "input" or "output", "spread" usage on model is not useful. So I would agree to put the spread usage into body parameter.

chunyu3 commented 4 weeks ago

FYI: For now, emitters could use the following way to judge it

function isSpreadBody(bodyParam: SdkBodyParameter | undefined): boolean {   return bodyParam?.type.kind === "model" && bodyParam.type !== bodyParam.correspondingMethodParams[0]?.type;}

This is a non-safe workaround. considering that the model which contains a property with its own type(circle model). Yes. it is a workaround to unblock us. But eventually we need to add a flag spread to body parameter

tadelesh commented 3 weeks ago

With current design, the usage in model is just used to identify whether emitter need to generate this model. If a model is used both in spread and non-spread case, you need to use correspondingMethodParams to identify if the body parameter is spread or not.