Support default values and property initializers (#378)
This commit is contained in:
Родитель
4166b4706b
Коммит
f7e1b0839b
|
@ -165,12 +165,6 @@ namespace AutoRest.CSharp.V3.CodeGen
|
|||
return Scope();
|
||||
}
|
||||
|
||||
public CodeWriterScope ForEach(string statement)
|
||||
{
|
||||
LineRaw($"foreach({statement})");
|
||||
return Scope();
|
||||
}
|
||||
|
||||
public CodeWriterScope Switch(string value)
|
||||
{
|
||||
LineRaw($"switch({value})");
|
||||
|
@ -183,9 +177,6 @@ namespace AutoRest.CSharp.V3.CodeGen
|
|||
public void EnumValue(string value, bool includeComma = true) =>
|
||||
LineRaw($"{value}{(includeComma ? "," : String.Empty)}");
|
||||
|
||||
public void AutoProperty(string modifiers, CSharpType type, string name, bool isReadOnly = false, string? initializer = null) =>
|
||||
LineRaw($"{modifiers} {Pair(type, name)} {{ get; {(isReadOnly ? "internal set; " : "set; ")}}}{initializer}");
|
||||
|
||||
public void UseNamespace(CSharpNamespace @namespace)
|
||||
{
|
||||
_usingNamespaces.Add(@namespace);
|
||||
|
|
|
@ -71,8 +71,19 @@ namespace AutoRest.CSharp.V3.CodeGen
|
|||
foreach (var property in schema.Properties)
|
||||
{
|
||||
CSharpType propertyType = _typeFactory.CreateType(property.Type);
|
||||
var initializer = !propertyType.IsNullable && NeedsInitialization(property.Type) ? $" = new {writer.Type(_typeFactory.CreateConcreteType(property.Type))}();" : null;
|
||||
writer.AutoProperty("public", propertyType, property.Name, property.IsReadOnly, initializer);
|
||||
writer.Append($"public {propertyType} {property.Name}");
|
||||
writer.AppendRaw(property.IsReadOnly ? "{ get; internal set; }" : "{ get; set; }");
|
||||
|
||||
if (property.DefaultValue != null)
|
||||
{
|
||||
writer.Append($" = {property.DefaultValue.Value.Value:L};");
|
||||
}
|
||||
else if (NeedsInitialization(property))
|
||||
{
|
||||
writer.Append($" = new {_typeFactory.CreateConcreteType(property.Type)}();");
|
||||
}
|
||||
|
||||
writer.Line();
|
||||
}
|
||||
|
||||
if (schema.ImplementsDictionary != null)
|
||||
|
@ -120,7 +131,26 @@ namespace AutoRest.CSharp.V3.CodeGen
|
|||
}
|
||||
}
|
||||
|
||||
private static bool NeedsInitialization(ClientTypeReference reference) => reference is CollectionTypeReference || reference is DictionaryTypeReference;
|
||||
private bool NeedsInitialization(ClientObjectProperty property)
|
||||
{
|
||||
// TODO: This logic shouldn't be base only on type nullability
|
||||
if (property.Type.IsNullable)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (property.Type is CollectionTypeReference || property.Type is DictionaryTypeReference)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if (property.Type is SchemaTypeReference schemaType)
|
||||
{
|
||||
return _typeFactory.ResolveReference(schemaType) is ClientObject;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
private void WriteSealedChoiceSchema(CodeWriter writer, ClientEnum schema)
|
||||
{
|
||||
|
|
|
@ -147,7 +147,6 @@ namespace AutoRest.TestServer.Tests
|
|||
await new EnumOperations(ClientDiagnostics, pipeline, host).PutReferencedAsync( Colors.RedColor));
|
||||
|
||||
[Test]
|
||||
[Ignore("Model not generated correctly")]
|
||||
public Task PutEnumReferencedConstant() => TestStatus(async (host, pipeline) =>
|
||||
await new EnumOperations(ClientDiagnostics, pipeline, host).PutReferencedConstantAsync( new RefColorConstant()));
|
||||
|
||||
|
|
|
@ -16,21 +16,8 @@ namespace AutoRest.TestServer.Tests
|
|||
[Test]
|
||||
public Task ConstantsInBody() => Test(async (host, pipeline) =>
|
||||
{
|
||||
var value = new Product
|
||||
{
|
||||
ConstString = "constant",
|
||||
ConstInt = 0,
|
||||
Child = new ChildProduct
|
||||
{
|
||||
ConstProperty = "constant"
|
||||
},
|
||||
ConstChild = new ConstantProduct
|
||||
{
|
||||
ConstProperty = "constant",
|
||||
ConstProperty2 = "constant2"
|
||||
}
|
||||
};
|
||||
var result = await new AllOperations(ClientDiagnostics, pipeline, string.Empty, host).PostWithConstantInBodyAsync( value);
|
||||
var value = new Product();
|
||||
var result = await new AllOperations(ClientDiagnostics, pipeline, string.Empty, host).PostWithConstantInBodyAsync(value);
|
||||
Assert.AreEqual(value.ConstString, result.Value.ConstString);
|
||||
Assert.AreEqual(value.ConstInt, result.Value.ConstInt);
|
||||
Assert.AreEqual(value.Child.ConstProperty, result.Value.Child.ConstProperty);
|
||||
|
|
|
@ -9,7 +9,7 @@ namespace body_complex.Models.V20160229
|
|||
{
|
||||
Kind = null;
|
||||
}
|
||||
public string Kind { get; internal set; }
|
||||
public string Kind { get; internal set; } = "Kind1";
|
||||
public string? PropB1 { get; set; }
|
||||
public MyBaseHelperType? Helper { get; set; }
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace body_string.Models.V100
|
|||
{
|
||||
public partial class RefColorConstant
|
||||
{
|
||||
public string ColorConstant { get; set; }
|
||||
public string ColorConstant { get; set; } = "green-color";
|
||||
public string? Field1 { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace validation.Models.V100
|
|||
{
|
||||
public partial class ChildProduct
|
||||
{
|
||||
public string ConstProperty { get; set; }
|
||||
public string ConstProperty { get; set; } = "constant";
|
||||
public int? Count { get; set; }
|
||||
}
|
||||
}
|
||||
|
|
|
@ -5,7 +5,7 @@ namespace validation.Models.V100
|
|||
{
|
||||
public partial class ConstantProduct
|
||||
{
|
||||
public string ConstProperty { get; set; }
|
||||
public string ConstProperty2 { get; set; }
|
||||
public string ConstProperty { get; set; } = "constant";
|
||||
public string ConstProperty2 { get; set; } = "constant2";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,10 +10,10 @@ namespace validation.Models.V100
|
|||
public ICollection<string>? DisplayNames { get; set; }
|
||||
public int? Capacity { get; set; }
|
||||
public string? Image { get; set; }
|
||||
public ChildProduct Child { get; set; }
|
||||
public ConstantProduct ConstChild { get; set; }
|
||||
public float ConstInt { get; set; }
|
||||
public string ConstString { get; set; }
|
||||
public string ConstStringAsEnum { get; set; }
|
||||
public ChildProduct Child { get; set; } = new ChildProduct();
|
||||
public ConstantProduct ConstChild { get; set; } = new ConstantProduct();
|
||||
public float ConstInt { get; set; } = 0F;
|
||||
public string ConstString { get; set; } = "constant";
|
||||
public string ConstStringAsEnum { get; set; } = "constant_string_as_enum";
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче