Skip to content

Commit 428f839

Browse files
[http-client-csharp] Omit model factory method if it contains internal param type (microsoft#5657)
fixes: microsoft#4913
1 parent 4cc9005 commit 428f839

File tree

4 files changed

+90
-2
lines changed

4 files changed

+90
-2
lines changed

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/src/Providers/ModelFactoryProvider.cs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,23 @@ protected override MethodProvider[] BuildMethods()
7070
foreach (var model in _models)
7171
{
7272
var modelProvider = CodeModelPlugin.Instance.TypeFactory.CreateModel(model);
73-
if (modelProvider is null || modelProvider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal))
73+
74+
if (modelProvider is null)
7475
continue;
7576

77+
var fullConstructor = modelProvider.FullConstructor;
78+
if (modelProvider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Internal)
79+
|| fullConstructor.Signature.Parameters.Any(p => !p.Type.IsPublic))
80+
{
81+
continue;
82+
}
83+
7684
var typeToInstantiate = modelProvider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Abstract)
7785
? modelProvider.DerivedModels.FirstOrDefault(m => m.IsUnknownDiscriminatorModel)
7886
: modelProvider;
7987
if (typeToInstantiate is null)
8088
continue;
8189

82-
var fullConstructor = modelProvider.FullConstructor;
8390
var binaryDataParam = fullConstructor.Signature.Parameters.FirstOrDefault(p => p.Name.Equals(AdditionalBinaryDataParameterName));
8491

8592
// Use a custom constructor if the generated full constructor was suppressed or customized

packages/http-client-csharp/generator/Microsoft.Generator.CSharp/test/Providers/ModelFactories/ModelFactoriesCustomizationTests.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,56 @@ public async Task CanChangeAccessibilityOfModelFactory()
125125
ValidateModelFactoryCommon(modelFactory);
126126
}
127127

128+
// This test validates that the model factory method for a model is omitted if the
129+
// model type is customized to be internal.
130+
[Test]
131+
public async Task OmitsModelFactoryMethodIfModelTypeInternal()
132+
{
133+
var plugin = await MockHelpers.LoadMockPluginAsync(
134+
inputModelTypes: [
135+
InputFactory.Model(
136+
"mockInputModel",
137+
properties:
138+
[
139+
InputFactory.Property("Prop1", InputPrimitiveType.String),
140+
InputFactory.Property("OptionalBool", InputPrimitiveType.Boolean, isRequired: false)
141+
])
142+
],
143+
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
144+
var csharpGen = new CSharpGen();
145+
146+
await csharpGen.ExecuteAsync();
147+
148+
// Model factory should be omitted since there are no methods to generate
149+
var modelFactory = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ModelFactoryProvider);
150+
Assert.IsNull(modelFactory);
151+
}
152+
153+
// This test validates that the model factory method for a model is omitted if the
154+
// any of the model's serialization ctor have parameters whose type are customized to be internal.
155+
[Test]
156+
public async Task OmitsModelFactoryMethodIfParamTypeInternal()
157+
{
158+
var modelProperty = InputFactory.Property("Prop1", InputFactory.Model("otherModel"));
159+
var plugin = await MockHelpers.LoadMockPluginAsync(
160+
inputModelTypes: [
161+
InputFactory.Model(
162+
"mockInputModel",
163+
properties:
164+
[
165+
modelProperty,
166+
])
167+
],
168+
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());
169+
var csharpGen = new CSharpGen();
170+
171+
await csharpGen.ExecuteAsync();
172+
173+
// Model factory should be omitted since there are no methods to generate
174+
var modelFactory = plugin.Object.OutputLibrary.TypeProviders.SingleOrDefault(t => t is ModelFactoryProvider);
175+
Assert.IsNull(modelFactory);
176+
}
177+
128178
[TestCase(true)]
129179
[TestCase(false)]
130180
public async Task CanCustomizeModelFullConstructor(bool extraParameters)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using Microsoft.Generator.CSharp.Customization;
5+
6+
#nullable disable
7+
8+
namespace Sample.Models;
9+
10+
internal partial class MockInputModel
11+
{
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Collections.ObjectModel;
4+
using Microsoft.Generator.CSharp.Customization;
5+
6+
#nullable disable
7+
8+
namespace Sample.Models;
9+
10+
public partial class MockInputModel
11+
{
12+
internal OtherModel Prop1 { get; set; }
13+
}
14+
15+
internal partial class OtherModel
16+
{
17+
18+
}

0 commit comments

Comments
 (0)