forked from microsoft/typespec
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(http-client-csharp): remove setters for model properties collecti…
…on types (microsoft#3233) This PR fixes the case where setters were being generated for model properties that were collection types. Model properties that are collection types will no longer generated a setter unless they are nullable. --------- Co-authored-by: ShivangiReja <[email protected]> Co-authored-by: Dapeng Zhang <[email protected]> Co-authored-by: ShivangiReja <[email protected]>
- Loading branch information
1 parent
32a25ec
commit d3a4454
Showing
4 changed files
with
163 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
...t-csharp/generator/Microsoft.Generator.CSharp/test/Models/Types/ModelTypeProviderTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Reflection; | ||
using Microsoft.Generator.CSharp.Input; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
namespace Microsoft.Generator.CSharp.Tests | ||
{ | ||
public class ModelTypeProviderTests | ||
{ | ||
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
private GeneratorContext _generatorContext; | ||
#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. | ||
private readonly string _configFilePath = Path.Combine(AppContext.BaseDirectory, "mocks"); | ||
private FieldInfo? _mockPlugin; | ||
|
||
[SetUp] | ||
public void Setup() | ||
{ | ||
// initialize the mock singleton instance of the plugin | ||
_mockPlugin = typeof(CodeModelPlugin).GetField("_instance", BindingFlags.Static | BindingFlags.NonPublic); | ||
_generatorContext = new GeneratorContext(Configuration.Load(_configFilePath)); | ||
} | ||
|
||
[TearDown] | ||
public void Teardown() | ||
{ | ||
_mockPlugin?.SetValue(null, null); | ||
} | ||
|
||
// Validates that the property body's setter is correctly set based on the property type | ||
[TestCaseSource(nameof(BuildProperties_ValidatePropertySettersTestCases))] | ||
public void BuildProperties_ValidatePropertySetters(InputModelProperty inputModelProperty, CSharpType type, bool hasSetter) | ||
{ | ||
var mockPluginInstance = new Mock<CodeModelPlugin>(_generatorContext) { }; | ||
var mockTypeFactory = new Mock<TypeFactory>() { }; | ||
mockTypeFactory.Setup(t => t.CreateCSharpType(It.IsAny<InputType>())).Returns(type); | ||
mockPluginInstance.SetupGet(p => p.TypeFactory).Returns(mockTypeFactory.Object); | ||
_mockPlugin?.SetValue(null, mockPluginInstance.Object); | ||
|
||
var props = new[] | ||
{ | ||
inputModelProperty | ||
}; | ||
|
||
var inputModel = new InputModelType("mockInputModel", "mockNamespace", "public", null, null, InputModelTypeUsage.RoundTrip, props, null, new List<InputModelType>(), null, null, null, false); | ||
var modelTypeProvider = new ModelTypeProvider(inputModel, null); | ||
var properties = modelTypeProvider.Properties; | ||
|
||
Assert.IsNotNull(properties); | ||
Assert.AreEqual(1, properties.Count); | ||
|
||
// validate the setter | ||
var prop1 = properties[0]; | ||
Assert.IsNotNull(prop1); | ||
|
||
var autoPropertyBody = prop1.PropertyBody as AutoPropertyBody; | ||
Assert.IsNotNull(autoPropertyBody); | ||
Assert.AreEqual(hasSetter, autoPropertyBody?.HasSetter); | ||
} | ||
|
||
public static IEnumerable<TestCaseData> BuildProperties_ValidatePropertySettersTestCases | ||
{ | ||
get | ||
{ | ||
// list property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputList("mockProp", new InputPrimitiveType(InputTypeKind.String, false), false, false), false, false, false), | ||
new CSharpType(typeof(IList<string>)), | ||
false); | ||
// read only list property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputList("mockProp", new InputPrimitiveType(InputTypeKind.String, false), false, false), false, true, false), | ||
new CSharpType(typeof(IReadOnlyList<string>)), | ||
false); | ||
// nullable list property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputList("mockProp", new InputPrimitiveType(InputTypeKind.String, false), false, false), false, false, false), | ||
new CSharpType(typeof(IList<string>), true), | ||
true); | ||
// dictionary property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputDictionary("mockProp", new InputPrimitiveType(InputTypeKind.String, false), new InputPrimitiveType(InputTypeKind.String, false), false), false, false, false), | ||
new CSharpType(typeof(IDictionary<string, string>)), | ||
false); | ||
// nullable dictionary property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputDictionary("mockProp", new InputPrimitiveType(InputTypeKind.String, false), new InputPrimitiveType(InputTypeKind.String, false), false), false, false, false), | ||
new CSharpType(typeof(IDictionary<string, string>), true), | ||
true); | ||
// primitive type property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputPrimitiveType(InputTypeKind.String, false), false, false, false), | ||
new CSharpType(typeof(string)), | ||
true); | ||
// read only primitive type property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputPrimitiveType(InputTypeKind.String, false), false, true, false), | ||
new CSharpType(typeof(string)), | ||
false); | ||
// readonlymemory property | ||
yield return new TestCaseData( | ||
new InputModelProperty("prop1", "prop1", "public", new InputList("mockProp", new InputPrimitiveType(InputTypeKind.String, false), true, false), false, false, false), | ||
new CSharpType(typeof(ReadOnlyMemory<>)), | ||
true); | ||
} | ||
} | ||
} | ||
} |