Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,9 @@ private static CSharpType EnsureNamespace(InputProperty? specProperty, CSharpTyp
// Use the TypeFactory to get the correct namespace for the type which respects any customizations that have
// been applied to the generated types.
var newType = CodeModelGenerator.Instance.TypeFactory.CreateCSharpType(inputType);
if (specProperty?.IsRequired == false && newType?.IsCollection == false)
{
newType = newType.WithNullable(true);
}

if (newType != null)
{
return newType;
return type.IsNullable ? newType.WithNullable(true) : newType;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ public async Task CanChangeListOfModelToReadOnlyListOfModel()
var elementType = listProp.Type.ElementType;
Assert.AreEqual("global::Sample.Models.Foo", elementType.ToString());
Assert.AreEqual("Sample.Models", elementType.Namespace);
Assert.IsTrue(elementType.IsNullable);
Assert.IsFalse(elementType.IsNullable);
}

[Test]
Expand Down Expand Up @@ -336,9 +336,9 @@ public async Task CanChangeListOfEnumToReadOnlyListOfEnum()
Assert.IsTrue(listProp.Type.IsList);

var elementType = listProp.Type.ElementType;
Assert.AreEqual("global::Sample.Models.Foo?", elementType.ToString());
Assert.AreEqual("global::Sample.Models.Foo", elementType.ToString());
Assert.AreEqual("Sample.Models", elementType.Namespace);
Assert.IsTrue(elementType.IsNullable);
Assert.IsFalse(elementType.IsNullable);
Assert.IsFalse(elementType.IsStruct);
Assert.IsFalse(elementType.IsLiteral);
}
Expand Down Expand Up @@ -414,7 +414,7 @@ public async Task CanChangeDictionaryOfModelToReadOnlyDictionaryOfModel()
var elementType = listProp.Type.ElementType;
Assert.AreEqual("global::Sample.Models.Foo", elementType.ToString());
Assert.AreEqual("Sample.Models", elementType.Namespace);
Assert.IsTrue(elementType.IsNullable);
Assert.IsFalse(elementType.IsNullable);
}

[Test]
Expand Down Expand Up @@ -483,7 +483,7 @@ public async Task CanChangeModelPropertyWithChangedNamespace()

var modelProp = modelTypeProvider.CanonicalView.Properties[0];
Assert.AreEqual("Prop1", modelProp.Name);
Assert.IsTrue(modelProp.Type.IsNullable);
Assert.IsFalse(modelProp.Type.IsNullable);
Assert.IsFalse(modelProp.Body.HasSetter);
Assert.AreEqual("global::Updated.Namespace.Models.Foo", modelProp.Type.ToString());
Assert.AreEqual("Updated.Namespace.Models", modelProp.Type.Namespace);
Expand Down Expand Up @@ -626,6 +626,76 @@ public async Task CanChangeEnumToExtensibleEnum()
Assert.IsTrue(enumProvider.DeclarationModifiers.HasFlag(TypeSignatureModifiers.Public | TypeSignatureModifiers.Partial | TypeSignatureModifiers.Struct | TypeSignatureModifiers.ReadOnly));
}

[Test]
public async Task CanChangeEnumNullableTypeModelProperty()
{
var inputEnum = InputFactory.Int32Enum(
"Foo",
[("val1", 1), ("val2", 2), ("val3", 3)],
isExtensible: false
);
var props = new[]
{
InputFactory.Property("Prop1", inputEnum),
};

var inputModel = InputFactory.Model("mockInputModel", properties: props);

var mockGenerator = await MockHelpers.LoadMockGeneratorAsync(
inputModelTypes: [inputModel],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

var modelTypeProvider = mockGenerator.Object.OutputLibrary.TypeProviders.Single(t => t.Name == "MockInputModel");
AssertCommon(modelTypeProvider, "Sample.Models", "MockInputModel");

// the property should be added to the custom code view
Assert.AreEqual(1, modelTypeProvider.CustomCodeView!.Properties.Count);
// the canonical type should be changed
Assert.AreEqual(1, modelTypeProvider.CanonicalView!.Properties.Count);

var modelProp = modelTypeProvider.CanonicalView.Properties[0];
Assert.AreEqual("Prop1", modelProp.Name);
Assert.IsFalse(modelProp.Type.IsNullable);
Assert.IsFalse(modelProp.Body.HasSetter);
Assert.AreEqual("global::Sample.Models.Foo", modelProp.Type.ToString());
Assert.AreEqual("Sample.Models", modelProp.Type.Namespace);
}

[Test]
public async Task CanChangeEnumTypeModelPropertyToNullable()
{
var inputEnum = InputFactory.Int32Enum(
"Foo",
[("val1", 1), ("val2", 2), ("val3", 3)],
isExtensible: false
);
var props = new[]
{
InputFactory.Property("Prop1", inputEnum),
};

var inputModel = InputFactory.Model("mockInputModel", properties: props);

var mockGenerator = await MockHelpers.LoadMockGeneratorAsync(
inputModelTypes: [inputModel],
compilation: async () => await Helpers.GetCompilationFromDirectoryAsync());

var modelTypeProvider = mockGenerator.Object.OutputLibrary.TypeProviders.Single(t => t.Name == "MockInputModel");
AssertCommon(modelTypeProvider, "Sample.Models", "MockInputModel");

// the property should be added to the custom code view
Assert.AreEqual(1, modelTypeProvider.CustomCodeView!.Properties.Count);
// the canonical type should be changed
Assert.AreEqual(1, modelTypeProvider.CanonicalView!.Properties.Count);

var modelProp = modelTypeProvider.CanonicalView.Properties[0];
Assert.AreEqual("Prop1", modelProp.Name);
Assert.IsTrue(modelProp.Type.IsNullable);
Assert.IsFalse(modelProp.Body.HasSetter);
Assert.AreEqual("global::Sample.Models.Foo?", modelProp.Type.ToString());
Assert.AreEqual("Sample.Models", modelProp.Type.Namespace);
}

[Test]
public async Task CanChangeExtensibleEnumToEnum()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Sample.Models;

public partial class MockInputModel
{
public readonly IReadOnlyDictionary<string, Foo> Prop1 { get; };
public readonly IReadOnlyDictionary<string, Foo> Prop1 { get; }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using SampleTypeSpec;

namespace Sample.Models;

public partial class MockInputModel
{
public Foo Prop1 { get; };
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using System;
using System.Collections.Generic;
using SampleTypeSpec;

namespace Sample.Models;

public partial class MockInputModel
{
public Foo? Prop1 { get; };
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Sample.Models;

public partial class MockInputModel
{
public readonly IReadOnlyList<Foo> Prop1 { get; };
public readonly IReadOnlyList<Foo> Prop1 { get; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ namespace Sample.Models;

public partial class MockInputModel
{
public Foo Prop1 { get; };
public Foo Prop1 { get; }
}
Loading