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

[TCGC] - use original model for simple spread instead of creating a new anonymous model #1132

Closed
4 tasks done
joheredi opened this issue Jul 8, 2024 · 4 comments · Fixed by #1388
Closed
4 tasks done
Assignees
Labels
lib:tcgc Issues for @azure-tools/typespec-client-generator-core library

Comments

@joheredi
Copy link
Member

joheredi commented Jul 8, 2024

Clear and concise description of the problem

There are scenarios where tracking the source model of anonymous models is beneficial. For instance, in TypeScript code generation, this feature would enable us to map serializer/deserializer types between RLC (Rest Level Client) and Modular. This functionality is crucial for bridging gaps between RLC and Modular types.

Example
Consider the following TypeSpec model:

model Foo {
   bar: {
      baz: string;
   }
} 

With the ability to track the source model, we could implement the following TypeScript code:

declare function deserializeFooBar(input: FooRest["bar"]): BarModel;

Here, BarModel is generated from TCGC, but there is no equivalent interface in RLC as it doesn't generate anonymous models.

Checklist

  • Follow our Code of Conduct
  • Check that this issue is about the Azure libraries for typespec. For feature request in the typespec language or core libraries file it in the TypeSpec repo
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
@joheredi joheredi added the lib:tcgc Issues for @azure-tools/typespec-client-generator-core library label Jul 8, 2024
@tadelesh
Copy link
Member

could you clarify the source meaning? for example, if anonymous model is in parameter, what should the source model be? other cases are model property, return type, etc.

@iscai-msft
Copy link
Contributor

@ArcturusZhang has the following comment:

For this typespec:

model Bar {}
model Baz {}
op foo(...Bar): Baz;

the TCGC result gives us a FooRequest as the model of body parameter. Since it is speading model Bar, could we get back the model Bar as the body parameter in this case?
This help us reduce the size of the generated assembly.

We agree that there are cases that returning the original model is not possible, such as an operation with spread of two different models, in those cases, we could still do the FooRequest as body because it is combining the two models.
But we would like to get the model back if the model is there.

@tadelesh
Copy link
Member

tadelesh commented Aug 8, 2024

@ArcturusZhang has the following comment:

For this typespec:

model Bar {}
model Baz {}
op foo(...Bar): Baz;

the TCGC result gives us a FooRequest as the model of body parameter. Since it is speading model Bar, could we get back the model Bar as the body parameter in this case? This help us reduce the size of the generated assembly.

We agree that there are cases that returning the original model is not possible, such as an operation with spread of two different models, in those cases, we could still do the FooRequest as body because it is combining the two models. But we would like to get the model back if the model is there.

@joheredi could you confirm if it is enough for js?

@tadelesh tadelesh self-assigned this Aug 8, 2024
@joheredi
Copy link
Member Author

joheredi commented Aug 8, 2024

Yes I think this would be good enough

@tadelesh tadelesh changed the title [TCGC] - SourceModel for anonymous models [TCGC] - use original model for simple spread instead of creating a new anonymous model Aug 9, 2024
github-merge-queue bot pushed a commit that referenced this issue Aug 22, 2024
use original model for body parameter in `SdkServiceOperation` if it is
from a simple spread.

simple spread means spread model without any `@header`, `@query` or
`@path` property and the spread model is the only source of http body.

examples, 

`op test(...Model): void;`

`op test(@Header h: string, @query q: string, ...Model): void;`

these two operation will have `Model` as body parameter.

```
op test(...A): void;
model A {
  @query
  q: string;
  prop: string;
}
```
this operation will have an anonymous model with only `prop` property
and generated name `TestRequest` as body parameter.

resolve: #1132


cc: @ArcturusZhang @joheredi

---------

Co-authored-by: Dapeng Zhang <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
lib:tcgc Issues for @azure-tools/typespec-client-generator-core library
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants