Skip to content
Merged
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
39 changes: 37 additions & 2 deletions src/ServiceStack/NativeTypes/TypeScript/TypeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,32 @@ public TypeScriptGenerator(MetadataTypesConfig config)
{"Decimal", "number"},
{"List", "Array"},
};
private static string declaredEmptyString = "\"\"";
private static Dictionary<string, string> primitiveDefaultValues = new Dictionary<string, string>
{
{"String", declaredEmptyString},
{"string", declaredEmptyString},
{"Boolean", "false"},
{"DateTime", declaredEmptyString},
{"DateTimeOffset", declaredEmptyString},
{"TimeSpan", declaredEmptyString},
{"Guid", declaredEmptyString},
{"Char", declaredEmptyString},
{"int", "0"},
{"float", "0"},
{"double", "0"},
{"Byte", "0"},
{"Int16", "0"},
{"Int32", "0"},
{"Int64", "0"},
{"UInt16", "0"},
{"UInt32", "0"},
{"UInt64", "0"},
{"Single", "0"},
{"Double", "0"},
{"Decimal", "0"},
{"List", "[]"}
};

public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMetadata nativeTypes)
{
Expand Down Expand Up @@ -92,7 +118,9 @@ public string GetCode(MetadataTypes metadata, IRequest request, INativeTypesMeta
var responseTypes = metadata.Operations
.Where(x => x.Response != null)
.Select(x => x.Response).ToHashSet();
var types = metadata.Types.ToHashSet();

// Base Types need to be written first
var types = metadata.Types.OrderBy(x => x.Inherits == null ? 0 : 1).ToHashSet();

var allTypes = new List<MetadataType>();
allTypes.AddRange(types);
Expand Down Expand Up @@ -284,7 +312,14 @@ private string AppendType(ref StringBuilderWrapper sb, MetadataType type, string
if (returnType == "any")
returnType = "Object";

responseTypeExpression = "createResponse() {{ return new {0}(); }}".Fmt(returnType);
// This is to avoid invalid syntax such as "return new string()"
string replaceReturnType;
if (primitiveDefaultValues.TryGetValue(returnType, out replaceReturnType))
returnType = replaceReturnType;

responseTypeExpression = replaceReturnType == null ?
"createResponse() {{ return new {0}(); }}".Fmt(returnType) :
"createResponse() {{ return {0}; }}".Fmt(returnType);
}
else if (implStr == "IReturnVoid")
{
Expand Down