microsoft / microsoft/typespec

[http-client-csharp] We should stop calling `CreateModel` method randomly

Open
#8,221 3 comments 0 reactions 0 assignees View on GitHub
emitter:client:csharp
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

In `TypeFactory`, we have a few methods that return a type from a given input type, including:
- `CreateCSharpType`
- `CreateModel`
- `CreateEnum`
- `CreateClient`

In mgmt generator, it is very common, that we want to return an existing type from resourcemanager, when we have a particular input model.
In this case, we need to do this:
```
protected override CSharpType? CreateCSharpTypeCore(InputType type)
{
if (type is InputModelType modelType && ShouldReplace(modelType))
{
return typeof(ManagedServiceIdentity);
}
return base.CreateCSharpTypeCore(type);
}
```
by doing this, we would imagine, for this particular model, this will short cut its call to `CreateModel` therefore we should not need to do something in `CreateModelCore`.
but this would not work - because in some places, some code would call `CreateModel` directly, therefore weird behavior would happen.
To solve this, in addition, we will have to do this as well:
```
protected override ModelProvider? CreateModelCore(InputModelType modelType)
{
if (ShouldReplace(modelType))
{
return SystemObjectType(modelType);// we need to construct a ModelProvider for this model here, which means this replaced type would never be a framework type!
}
}
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.