microsoft / typespec

https://typespec.io/
MIT License
4.55k stars 222 forks source link

[http-server-csharp] Support omit-unreachable-types setting in config to exclude types not used in operations #5126

Open ArcturusZhang opened 1 week ago

ArcturusZhang commented 1 week ago

I am generating the petstore spec, the model PetId is not really used, we use it to spread it as parameters:

op create(...PetId): Pet;

but this model is actually not used. The server gen still generates it, and it cannot compile because this model has a property with the same name as the model.

public partial class PetId
{
    public int PetId {get; set;} // this will not compile.
}

Either, the server gen should not generate the model that is not used, or we should do something on the generated model to ensure it never has a property with the same name as its enclosing type. Or I think we should do both.

markcowl commented 1 week ago

@ArcturusZhang This is currently by design. Using an alias will prevent such a type from being emitted. Eventually, I think all emitters should have an equivalent of the omit-unreachable-types setting that limits the generated types to those that are reachable from operations. But generating all user-supplied types should be the default.

Keeping this issue to track omit-unreachable-types as a setting

ArcturusZhang commented 6 days ago

@ArcturusZhang This is currently by design. Using an alias will prevent such a type from being emitted. Eventually, I think all emitters should have an equivalent of the omit-unreachable-types setting that limits the generated types to those that are reachable from operations. But generating all user-supplied types should be the default.

Keeping this issue to track omit-unreachable-types as a setting

make sense. C# generator has this setting, unreference-type-handling.