Skip to content

Commit 3ef7bc5

Browse files
author
Per Christian B. Viken
committed
feat(lib): Add default map from System.Object to any
1 parent c1c77c9 commit 3ef7bc5

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [unreleased]
99

10+
### Added
11+
12+
- Add default map from `System.Object` to `any`
13+
1014
### Fixed
1115

1216
- Fix writing dictionaries with custom types as values wrapped inside lists.

TypeContractor.Tests/TypeScript/TypeScriptConverterTests.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void Can_Convert_Simple_Types()
3333
result.FullName.Should().Be("TypeContractor.Tests.TypeScript.TypeScriptConverterTests+SimpleTypes");
3434
result.IsEnum.Should().BeFalse();
3535
result.EnumMembers.Should().BeNull();
36-
result.Properties.Should().HaveCount(5);
36+
result.Properties.Should().HaveCount(6);
3737
}
3838

3939
[Theory]
@@ -42,6 +42,7 @@ public void Can_Convert_Simple_Types()
4242
[InlineData(2)]
4343
[InlineData(3)]
4444
[InlineData(4)]
45+
[InlineData(5)]
4546
public void Converted_Simple_Properties_Looks_As_Expected(int propertyIndex)
4647
{
4748
var result = Sut.Convert(typeof(SimpleTypes));
@@ -104,6 +105,17 @@ public void Converted_Simple_Properties_Looks_As_Expected(int propertyIndex)
104105
prop.IsArray.Should().BeFalse();
105106
prop.IsNullable.Should().BeFalse();
106107
break;
108+
109+
case 5:
110+
prop.SourceName.Should().Be("SomeObject");
111+
prop.SourceType.Should().Be(typeof(object));
112+
prop.InnerSourceType.Should().BeNull();
113+
prop.DestinationName.Should().Be("someObject");
114+
prop.DestinationType.Should().Be("any");
115+
prop.IsBuiltin.Should().BeTrue();
116+
prop.IsArray.Should().BeFalse();
117+
prop.IsNullable.Should().BeFalse();
118+
break;
107119
}
108120
}
109121

@@ -123,13 +135,14 @@ public void Finds_Properties_From_Multiple_Base_Classes()
123135
var result = Sut.Convert(typeof(NestedInheritanceTest));
124136

125137
result.Properties.Should().NotBeNull();
126-
result.Properties.Should().HaveCount(7);
138+
result.Properties.Should().HaveCount(8);
127139
result.Properties.Should()
128140
.Contain(x => x.SourceName == "StringProperty")
129141
.And.Contain(x => x.SourceName == "NumberProperty")
130142
.And.Contain(x => x.SourceName == "NumbersProperty")
131143
.And.Contain(x => x.SourceName == "DoubleTime")
132144
.And.Contain(x => x.SourceName == "TimeyWimeySpan")
145+
.And.Contain(x => x.SourceName == "SomeObject")
133146
.And.Contain(x => x.SourceName == "InheritedProperty")
134147
.And.Contain(x => x.SourceName == "FinalProperty");
135148
}
@@ -243,6 +256,7 @@ private class SimpleTypes
243256
public IEnumerable<int> NumbersProperty { get; set; }
244257
public double DoubleTime { get; set; }
245258
public TimeSpan TimeyWimeySpan { get; set; }
259+
public object SomeObject { get; set; }
246260
}
247261

248262
private class TypeVisibility

TypeContractor.Tests/TypeScript/TypeScriptWriterTests.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,36 @@ public TypeScriptWriterTests()
2121
Sut = new TypeScriptWriter(_configuration.OutputPath);
2222
}
2323

24+
[Fact]
25+
public void Can_Write_Simple_Types()
26+
{
27+
// Arrange
28+
var types = new[] { typeof(SimpleTypes) }
29+
.Select(t => ContractedType.FromName(t.FullName!, t, _configuration));
30+
31+
var outputTypes = types
32+
.Select(_converter.Convert)
33+
.ToList() // Needed so `converter.Convert` runs before we concat
34+
.Concat(_converter.CustomMappedTypes.Values)
35+
.ToList();
36+
37+
// Act
38+
var result = Sut.Write(outputTypes.First(), outputTypes);
39+
40+
// Assert
41+
var file = File.ReadAllLines(result).Select(x => x.TrimStart());
42+
file.Should()
43+
.NotBeEmpty()
44+
.And.NotContainMatch("import * from")
45+
.And.Contain("export interface SimpleTypes {")
46+
.And.Contain("stringProperty: string;")
47+
.And.Contain("numberProperty?: number;")
48+
.And.Contain("numbersProperty: number[];")
49+
.And.Contain("doubleTime: number;")
50+
.And.Contain("timeyWimeySpan: string;")
51+
.And.Contain("someObject: any;");
52+
}
53+
2454
[Fact]
2555
public void Handles_Dictionary_With_Complex_Values()
2656
{
@@ -71,6 +101,16 @@ public void Handles_Dictionary_With_Nested_Dictionary_Values()
71101

72102
#region Test input
73103
#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable.
104+
private class SimpleTypes
105+
{
106+
public string StringProperty { get; set; }
107+
public int? NumberProperty { get; set; }
108+
public IEnumerable<int> NumbersProperty { get; set; }
109+
public double DoubleTime { get; set; }
110+
public TimeSpan TimeyWimeySpan { get; set; }
111+
public object SomeObject { get; set; }
112+
}
113+
74114
private class ComplexValueDictionary
75115
{
76116
public Dictionary<Guid, IEnumerable<FormulaDto>> Formulas { get; set; }

TypeContractor/TypeContractorConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public TypeContractorConfiguration AddDefaultSuffixes()
5959
/// </summary>
6060
/// <returns>The configuration object for continued chaining</returns>
6161
public TypeContractorConfiguration AddDefaultTypeMaps()
62-
{
62+
{
63+
AddCustomMap(typeof(object), DestinationTypes.AnyType);
6364
AddCustomMap(typeof(string), DestinationTypes.StringType);
6465
AddCustomMap(typeof(DateTime), DestinationTypes.StringType);
6566
AddCustomMap(typeof(DateTimeOffset), DestinationTypes.StringType);

TypeContractor/TypeScript/DestinationTypes.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
namespace TypeContractor.TypeScript;
22

33
public static class DestinationTypes
4-
{
4+
{
5+
public const string AnyType = "any";
56
public const string StringType = "string";
67
public const string Boolean = "boolean";
78
public const string Number = "number";

0 commit comments

Comments
 (0)