Azure / autorest.csharp

Extension for AutoRest (https://github.com/Azure/autorest) that generates C# code
MIT License
142 stars 166 forks source link

prepend-rp-prefix behavior has changed #3908

Open cataggar opened 1 year ago

cataggar commented 1 year ago

The bahavior of prepend-rp-prefix has changed. It no longer takes the last word after the last dot for the RP Name. For <RootNamespace>Azure.ResourceManager.Aaa.Bbb.Avs</RootNamespace> it used to calculate Avs as the RP prefix. Now it calculates AaaBbbAvs as the RP prefix.

image

Is there a way for me to get back the previous behavior? Or is there a way for me to explicitly set the RP prefix as Avs in the csproj or autorest.md`?

cataggar commented 1 year ago

I'm not sure what is causing this change. The line MgmtContext.Context.DefaultNamespace.Split('.').Last() looks like it should result in Avs as the prefix.

https://github.com/Azure/autorest.csharp/blob/7ec42556b6515821f1f1f02b654c98b583a1cf61/src/AutoRest.CSharp/Mgmt/Decorator/Transformer/CommonSingleWordModels.cs#L43-L56

cataggar commented 1 year ago

This broke when we upgraded from 3.0.0-beta.20230531.2 to 3.0.0-beta.20230804.1. It looks like it was caused by

cc @ArcturusZhang @m-nash

How do I get the prefix to be just Avs like before?

cataggar commented 1 year ago

The use case is that we generating test clients for different API versions. They are in different C# projects and different C# namespaces.

Before Avs was the prefix. With the #3470 change, V20230901Avs is the prefix, which is not what I want.

ArcturusZhang commented 1 year ago

This is by design, we made this change because we need to ensure as much as possible that types in different namespaces to have different names. There are two RPs Azure.ResourceManager.PostgreSql.FlexibleServers and Azure.ResourceManager.MySql.FlexibleServers which contains very similar models therefore we have to change the logic to take everything after Azure.ResourceManager to make sure there model factory could have a different name.

cataggar commented 1 year ago

I understand, but that breaks our use case. A safer alternative would have been a way to configure the name and then configure the name for those RPs. Now, adding a way to configure it would also be a solution to unbreak my use case. Can you point me to the code for this logic? Is it different just for Azure.ResourceManager?

ArcturusZhang commented 1 year ago

I understand, but that breaks our use case. A safer alternative would have been a way to configure the name and then configure the name for those RPs. Now, adding a way to configure it would also be a solution to unbreak my use case. Can you point me to the code for this logic? Is it different just for Azure.ResourceManager?

Sorry I forgot to include the solution on my previous comment. You could use the rename-mapping functionality to rename a thing to whatever you want. You could find some document around this here: https://github.com/Azure/autorest.csharp/blob/73514b6dde9b060462c2b82c82ba8bbf2d5d1a70/docs/mgmt/polishing.md#rename-a-type

cataggar commented 1 year ago

@ArcturusZhang, that workaround only appears to work for models. There are several other files impacted including the model factory and these:

image

ArcturusZhang commented 12 months ago

@ArcturusZhang, that workaround only appears to work for models. There are several other files impacted including the model factory and these:

image

Oh - for these, you could use customization code to add something like:

[CodeGenType("ThePreviousGeneratedName")] // for instance you could `v2023_03_01AvsExtensions` here
public partial class YourNewName
{} // it does not need to contain anything inside this partial class

In the meantime, I believe it should be somewhere that we could assign the "rp name". I will have an investigation to see where we could put it