microsoft / typespec

https://typespec.io/
MIT License
4.45k stars 208 forks source link

how to interpret body in bodyRoot #4815

Open qiaozha opened 2 days ago

qiaozha commented 2 days ago

Playground Link In this tsp

op test(@bodyRoot body: {@body body: Test}): void;

is the body parameter Test or an anonymous model with a property named body whose type is Test? the openapi3 emitter result shows the former Image

but the type graph shows the later? Image

it's a real case https://github.com/Azure/azure-rest-api-specs/blob/ca4b71ab0e3bb7e4c44b44bf4d84d9c51f7b8264/specification/workloads/Workloads.SAPVirtualInstance.Management/SAPCentralServerInstance.tsp#L61, and seems quite common as azure operation template uses bodyRoot here https://github.com/Azure/typespec-azure/blob/main/packages/typespec-azure-resource-manager/lib/operations.tsp#L502

According to https://github.com/microsoft/typespec/issues/2868#issue-2110782074 it feels like the former is correct?

qiaozha commented 2 days ago

Can we have a linter rule to prevent the usage of body in bodyRoot? I wonder if there's a real case that customer has to write their typespec this way?

In the above case, the Azure operation template has been changed from @body to @bodyRoot according to this https://github.com/Azure/typespec-azure/pull/708/files pr, but is it really expected? I wonder if we can change the service typespec instead of changing the operation template here?

timotheeguerin commented 1 day ago

The azure specs shouldn't use @body here it has no value and just pass start request

allenjzhang commented 1 day ago

@qiaozha , do you know the reason for them to declare this way? Why wouldn't it work just using StartRequest as Request parameter?

  start is ArmResourceActionAsync<
    SAPCentralServerInstance,
    {
      @doc("SAP Central Services instance start request body.")
      @body
      body?: StartRequest;
    },
    Foundations.OperationStatusResult
  >;
qiaozha commented 1 day ago

My guess is they want to override the body description, I can double confirm with the service team. But if service team should not use @body decorator in the operation template, would it be an indicator that we should add this linter rule?