Skip to content

Commit 38a8c7a

Browse files
Revert "Fix LastContractPropertiesMap overriding collection property type when element type changes" (#10411)
Reverts #10319 This change is leading to breaks in the Search library. The original behavior probably makes more sense. GAed property types cannot be changed without introducing new property names via custom code.
1 parent ab5caa4 commit 38a8c7a

3 files changed

Lines changed: 1 addition & 64 deletions

File tree

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/src/Providers/ModelProvider.cs

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -538,17 +538,9 @@ protected internal override PropertyProvider[] BuildProperties()
538538
// Targeted backcompat fix for the case where properties were previously generated as read-only collections
539539
if (outputProperty.Type.IsReadWriteList || outputProperty.Type.IsReadWriteDictionary)
540540
{
541-
// We compare Arguments by name (not just ElementType) to cover both list element types
542-
// and dictionary key/value types. This ensures we only override the collection wrapper
543-
// (e.g. IReadOnlyList<T> → IList<T>) and not when the element type itself has changed.
544-
// We use AreNamesEqual rather than Equals because the argument types may come from
545-
// different sources (TypeProvider vs compiled assembly) but represent the same logical type.
546541
if (LastContractPropertiesMap.TryGetValue(outputProperty.Name,
547542
out CSharpType? lastContractPropertyType) &&
548-
!outputProperty.Type.Equals(lastContractPropertyType) &&
549-
outputProperty.Type.Arguments.Count == lastContractPropertyType.Arguments.Count &&
550-
outputProperty.Type.Arguments.Zip(lastContractPropertyType.Arguments).All(
551-
pair => pair.First.AreNamesEqual(pair.Second)))
543+
!outputProperty.Type.Equals(lastContractPropertyType))
552544
{
553545
outputProperty.Type = lastContractPropertyType.ApplyInputSpecProperty(property);
554546
CodeModelGenerator.Instance.Emitter.Info($"Changed property {Name}.{outputProperty.Name} type to {lastContractPropertyType} to match last contract.");

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/ModelProviderTests.cs

Lines changed: 0 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,51 +1062,6 @@ await MockHelpers.LoadMockGeneratorAsync(
10621062
Assert.IsTrue(moreItemsProperty!.Type.Equals(new CSharpType(typeof(IReadOnlyDictionary<,>), typeof(string), elementEnumProvider.Type)));
10631063
}
10641064

1065-
[Test]
1066-
public async Task BackCompat_CollectionPropertyTypeNotOverriddenWhenElementTypeChanges()
1067-
{
1068-
// Simulate the scenario where the element type of a collection property has changed
1069-
// (e.g., from Record<unknown>[] to BulkVMConfiguration[] via @typeChangedFrom).
1070-
// The last contract has IList<IDictionary<string, BinaryData>> but the new code model
1071-
// produces IList<NewElementModel>. The override should NOT apply because the element
1072-
// type has changed.
1073-
var newElementModel = InputFactory.Model(
1074-
"NewElementModel",
1075-
properties:
1076-
[
1077-
InputFactory.Property("name", InputPrimitiveType.String)
1078-
]);
1079-
var inputModel = InputFactory.Model(
1080-
"MockInputModel",
1081-
properties:
1082-
[
1083-
InputFactory.Property("items", InputFactory.Array(newElementModel)),
1084-
InputFactory.Property("moreItems", InputFactory.Dictionary(newElementModel))
1085-
]);
1086-
1087-
await MockHelpers.LoadMockGeneratorAsync(
1088-
inputModelTypes: [inputModel, newElementModel],
1089-
lastContractCompilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
1090-
1091-
var modelProvider = CodeModelGenerator.Instance.OutputLibrary.TypeProviders.SingleOrDefault(t => t.Name == "MockInputModel") as ModelProvider;
1092-
Assert.IsNotNull(modelProvider);
1093-
1094-
var newElementModelProvider = CodeModelGenerator.Instance.OutputLibrary.TypeProviders.SingleOrDefault(t => t.Name == "NewElementModel") as ModelProvider;
1095-
Assert.IsNotNull(newElementModelProvider);
1096-
1097-
// The items property should use the new element type (IList<NewElementModel>), not be
1098-
// overridden to the old type (IList<IDictionary<string, BinaryData>>) from last contract
1099-
var itemsProperty = modelProvider!.Properties.FirstOrDefault(p => p.Name == "Items");
1100-
Assert.IsNotNull(itemsProperty);
1101-
Assert.IsTrue(itemsProperty!.Type.Equals(new CSharpType(typeof(IList<>), newElementModelProvider!.Type)));
1102-
1103-
// The moreItems property should use the new element type (IDictionary<string, NewElementModel>), not be
1104-
// overridden to the old type (IDictionary<string, IDictionary<string, BinaryData>>) from last contract
1105-
var moreItemsProperty = modelProvider.Properties.FirstOrDefault(p => p.Name == "MoreItems");
1106-
Assert.IsNotNull(moreItemsProperty);
1107-
Assert.IsTrue(moreItemsProperty!.Type.Equals(new CSharpType(typeof(IDictionary<,>), typeof(string), newElementModelProvider.Type)));
1108-
}
1109-
11101065
[Test]
11111066
public async Task BackCompat_InternalTypesAreIgnored()
11121067
{

packages/http-client-csharp/generator/Microsoft.TypeSpec.Generator/test/Providers/ModelProviders/TestData/ModelProviderTests/BackCompat_CollectionPropertyTypeNotOverriddenWhenElementTypeChanges/MockInputModel.cs

Lines changed: 0 additions & 10 deletions
This file was deleted.

0 commit comments

Comments
 (0)