Skip to content

Commit b30b72d

Browse files
Update version of Azure Generator (Azure#51307)
* fix existing issues * fix the issue * refine the code for rename the resource type for resource base model * clean up
1 parent c63cf9d commit b30b72d

File tree

15 files changed

+180
-61
lines changed

15 files changed

+180
-61
lines changed

eng/Packages.Data.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,6 @@
455455
<PropertyGroup>
456456
<TestProxyVersion>1.0.0-dev.20250501.1</TestProxyVersion>
457457
<UnbrandedGeneratorVersion>1.0.0-alpha.20250715.1</UnbrandedGeneratorVersion>
458-
<AzureGeneratorVersion>1.0.0-alpha.20250703.1</AzureGeneratorVersion>
458+
<AzureGeneratorVersion>1.0.0-alpha.20250715.3</AzureGeneratorVersion>
459459
</PropertyGroup>
460460
</Project>

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Providers/InheritableSystemObjectModelProvider.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ internal class InheritableSystemObjectModelProvider : ModelProvider
1414
public InheritableSystemObjectModelProvider(Type type, InputModelType inputModel) : base(inputModel)
1515
{
1616
_type = type;
17+
CrossLanguageDefinitionId = inputModel.CrossLanguageDefinitionId;
1718
}
1819

20+
internal string CrossLanguageDefinitionId { get; }
21+
1922
protected override string BuildName() => _type.Name;
2023

2124
protected override string BuildRelativeFilePath()

eng/packages/http-client-csharp-mgmt/generator/Azure.Generator.Management/src/Visitors/NameVisitor.cs

Lines changed: 34 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Azure.Core;
55
using Azure.Generator.Management.Primitives;
6+
using Azure.Generator.Management.Providers;
67
using Microsoft.TypeSpec.Generator.ClientModel;
78
using Microsoft.TypeSpec.Generator.Input;
89
using Microsoft.TypeSpec.Generator.Primitives;
@@ -44,36 +45,44 @@ internal class NameVisitor : ScmLibraryVisitor
4445
}
4546
}
4647
}
48+
return base.PreVisitModel(model, type);
49+
}
4750

48-
// rename "Type" property to "ResourceType" in Azure.ResourceManager.CommonTypes.Resource
49-
bool typePropertyRenamed = false;
50-
if (model.CrossLanguageDefinitionId.Equals(KnownManagementTypes.ArmResourceId))
51+
protected override PropertyProvider? PreVisitProperty(InputProperty property, PropertyProvider? propertyProvider)
52+
{
53+
DoPreVisitPropertyForResourceTypeName(property, propertyProvider);
54+
DoPreVisitPropertyForUrlPropertyName(property, propertyProvider);
55+
return base.PreVisitProperty(property, propertyProvider);
56+
}
57+
58+
private void DoPreVisitPropertyForResourceTypeName(InputProperty property, PropertyProvider? propertyProvider)
59+
{
60+
if (propertyProvider == null || property is not InputModelProperty)
5161
{
52-
foreach (var property in model.Properties)
53-
{
54-
if (!typePropertyRenamed && property.Type is InputPrimitiveType primitiveType && KnownManagementTypes.TryGetPrimitiveType(primitiveType.CrossLanguageDefinitionId, out var knownType)
55-
&& knownType.Equals(typeof(ResourceType)) && property is InputModelProperty typeProperty)
56-
{
57-
typePropertyRenamed = true;
58-
typeProperty.Update(name: ResourceTypeName, isRequired: true);
59-
}
60-
if (property is InputModelProperty modelProperty && TryTransformUrlToUri(property.Name, out var newPropertyName))
61-
{
62-
modelProperty.Update(name: newPropertyName);
63-
}
64-
}
62+
return;
6563
}
66-
else
64+
var enclosingType = propertyProvider.EnclosingType;
65+
if (enclosingType is not InheritableSystemObjectModelProvider modelProvider
66+
|| !modelProvider.CrossLanguageDefinitionId.Equals(KnownManagementTypes.ArmResourceId))
6767
{
68-
foreach (var property in model.Properties)
69-
{
70-
if (property is InputModelProperty modelProperty && TryTransformUrlToUri(property.Name, out var newPropertyName))
71-
{
72-
modelProperty.Update(name: newPropertyName);
73-
}
74-
}
68+
return;
69+
}
70+
// the Azure.ResourceManager.CommonTypes.Resource defines its `type` property as an optional `armResourceType`
71+
// therefore here we need to change it to required because our common types define it as required
72+
if (propertyProvider.Type.Equals(_nullableResourceType))
73+
{
74+
propertyProvider.Update(name: ResourceTypeName, type: typeof(ResourceType));
75+
}
76+
}
77+
78+
private readonly CSharpType _nullableResourceType = new CSharpType(typeof(ResourceType), isNullable: true);
79+
80+
private void DoPreVisitPropertyForUrlPropertyName(InputProperty property, PropertyProvider? propertyProvider)
81+
{
82+
if (propertyProvider != null && TryTransformUrlToUri(propertyProvider.Name, out var newPropertyName))
83+
{
84+
propertyProvider.Update(name: newPropertyName);
7585
}
76-
return base.PreVisitModel(model, type);
7786
}
7887

7988
protected override MethodProvider? VisitMethod(MethodProvider method)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
#nullable disable
5+
6+
using System.ClientModel.Primitives;
7+
using MgmtTypeSpec.Models;
8+
9+
namespace MgmtTypeSpec
10+
{
11+
// This is temporary. In a near future, the generator will be able to generate all these attributes.
12+
[ModelReaderWriterBuildable(typeof(BarSettingsProperties))]
13+
[ModelReaderWriterBuildable(typeof(BarData))]
14+
[ModelReaderWriterBuildable(typeof(BarSettingsData))]
15+
[ModelReaderWriterBuildable(typeof(FooData))]
16+
[ModelReaderWriterBuildable(typeof(FooSettingsData))]
17+
[ModelReaderWriterBuildable(typeof(ZooData))]
18+
[ModelReaderWriterBuildable(typeof(BarListResult))]
19+
[ModelReaderWriterBuildable(typeof(BarProperties))]
20+
[ModelReaderWriterBuildable(typeof(FooListResult))]
21+
[ModelReaderWriterBuildable(typeof(FooProperties))]
22+
[ModelReaderWriterBuildable(typeof(FooSettingsPatch))]
23+
[ModelReaderWriterBuildable(typeof(FooSettingsProperties))]
24+
[ModelReaderWriterBuildable(typeof(FooSettingsUpdateProperties))]
25+
[ModelReaderWriterBuildable(typeof(PrivateLink))]
26+
[ModelReaderWriterBuildable(typeof(PrivateLinkListResult))]
27+
[ModelReaderWriterBuildable(typeof(PrivateLinkResourceProperties))]
28+
[ModelReaderWriterBuildable(typeof(ZooListResult))]
29+
[ModelReaderWriterBuildable(typeof(ZooPatch))]
30+
[ModelReaderWriterBuildable(typeof(ZooProperties))]
31+
[ModelReaderWriterBuildable(typeof(ZooUpdateProperties))]
32+
public partial class MgmtTypeSpecContext
33+
{
34+
}
35+
}

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BarData.Serialization.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/BarSettingsData.Serialization.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/FooData.Serialization.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/FooSettingsData.Serialization.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Models/FooProperties.Serialization.cs

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

eng/packages/http-client-csharp-mgmt/generator/TestProjects/Local/Mgmt-TypeSpec/src/Generated/Models/PrivateLink.Serialization.cs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)