Skip to content

Commit 210783c

Browse files
committed
Angular
- fix date serialization improved for generic types ASP.NET - controller/hub read performance increased Core - duplicated type reading improved Reflection - type reading improved
1 parent e6efda2 commit 210783c

File tree

11 files changed

+49
-103
lines changed

11 files changed

+49
-103
lines changed

Angular/Writers/AngularServiceWriter.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -465,12 +465,13 @@ private bool WriteDateFixes(ModelTransferObject model, bool isModelEnumerable, M
465465
}
466466
foreach (PropertyTransferObject property in model.Properties)
467467
{
468-
if (typeChain.Any(type => type.Name == property.Type.Name && type.Namespace == property.Type.Namespace))
468+
TypeTransferObject type = model.Generics.FirstOrDefault(generic => generic.Alias?.Name == property.Name)?.Type ?? property.Type;
469+
if (typeChain.Any(typeFromChain => typeFromChain.Name == property.Type.Name && typeFromChain.Namespace == property.Type.Namespace))
469470
{
470471
continue;
471472
}
472473
string propertyName = Formatter.FormatProperty(property.Name, configuration);
473-
if (property.Type.Name == nameof(DateTime))
474+
if (type.Name == nameof(DateTime))
474475
{
475476
datePropertyFound = true;
476477
if (isModelEnumerable)
@@ -482,13 +483,14 @@ private bool WriteDateFixes(ModelTransferObject model, bool isModelEnumerable, M
482483
innerCode.AddLine(this.WriteFieldChain(chain).Field(propertyName).Assign(Code.This().Method("convertToDate", this.WriteFieldChain(chain).Field(propertyName))).Close());
483484
}
484485
}
485-
ModelTransferObject propertyModel = property.Type as ModelTransferObject;
486+
ModelTransferObject propertyModel = type as ModelTransferObject;
486487
TypeTransferObject entryType = propertyModel?.Generics.FirstOrDefault()?.Type;
488+
entryType = entryType == null ? null : model.Generics.FirstOrDefault(generic => generic.Alias?.Name == entryType.Name)?.Type ?? entryType;
487489
ModelTransferObject entryModel = entryType as ModelTransferObject ?? transferObjects.OfType<ModelTransferObject>().FirstOrDefault(x => x.Name == entryType?.Name && x.Namespace == entryType?.Namespace);
488490
List<string> nextChain = new List<string>(chain);
489491
nextChain.Add(propertyName);
490492
List<TypeTransferObject> nextTypeChain = new List<TypeTransferObject>(typeChain);
491-
nextTypeChain.Add(property.Type);
493+
nextTypeChain.Add(type);
492494
if (propertyModel != null && propertyModel.IsEnumerable() && entryModel != null)
493495
{
494496
datePropertyFound = this.WriteDateFixes(entryModel, true, innerCode, nextChain, nextTypeChain, transferObjects, configuration) || datePropertyFound;

AspDotNet/Readers/AspDotNetControllerReader.cs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ public virtual void Read(AspDotNetReadConfiguration configuration, List<ITransfe
114114
}
115115
methodVersions.Sort();
116116
returnType = returnType.IgnoreGeneric(typesToIgnore);
117-
this.modelReader.Read(returnType, transferObjects, configuration.Controller);
118117
Attribute methodRouteAttribute = methodAttributes.FirstOrDefault(x => x.GetType().Name == "RouteAttribute");
119118
if (methodRouteAttribute != null)
120119
{
@@ -124,15 +123,11 @@ public virtual void Read(AspDotNetReadConfiguration configuration, List<ITransfe
124123
{
125124
HttpServiceActionTransferObject action = new HttpServiceActionTransferObject();
126125
action.Name = actionTypes.Count == 1 ? method.Name : $"{actionType.Key}{method.Name.FirstCharToUpper()}";
127-
action.ReturnType = returnType.ToTransferObject(configuration.Controller);
126+
action.ReturnType = this.modelReader.Read(returnType, transferObjects, configuration.Controller);
128127
action.Route = actionType.Value ?? fallbackRoute;
129128
action.Type = actionType.Key;
130129
action.Version = methodVersions.LastOrDefault();
131130
ParameterInfo[] parameters = method.GetParameters().Where(parameter => parameter.GetCustomAttributes().Select(attribute => attribute.GetType().Name).All(attributeName => attributeName != "FromServicesAttribute" && attributeName != "FromHeaderAttribute")).ToArray();
132-
foreach (ParameterInfo parameter in parameters)
133-
{
134-
this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Controller);
135-
}
136131
action.RequireBodyParameter = action.Type.IsBodyParameterRequired();
137132
foreach (ParameterInfo parameter in parameters)
138133
{
@@ -143,7 +138,7 @@ public virtual void Read(AspDotNetReadConfiguration configuration, List<ITransfe
143138
string fullRoute = $"{controller.Route}/{action.Route}";
144139
HttpServiceActionParameterTransferObject actionParameter = new HttpServiceActionParameterTransferObject();
145140
actionParameter.Name = parameter.Name;
146-
actionParameter.Type = parameter.ParameterType.ToTransferObject(configuration.Controller);
141+
actionParameter.Type = this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Controller);
147142
actionParameter.FromBody = this.IsFromBodyParameter(parameter, action.Type);
148143
actionParameter.FromQuery = this.IsFromQueryParameter(parameter);
149144
actionParameter.Inline = fullRoute.Contains($"{{{parameter.Name}}}");

AspDotNet/Readers/AspDotNetHubReader.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,10 @@ public void Read(AspDotNetReadConfiguration configuration, List<ITransferObject>
5656
}
5757
foreach (ParameterInfo parameter in method.GetParameters())
5858
{
59-
this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Hub);
6059
action.Parameters.Add(new HttpServiceActionParameterTransferObject
6160
{
6261
Name = parameter.Name,
63-
Type = parameter.ParameterType.ToTransferObject(configuration.Hub)
62+
Type = this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Hub)
6463
});
6564
}
6665
hub.Actions.Add(action);
@@ -74,11 +73,10 @@ public void Read(AspDotNetReadConfiguration configuration, List<ITransferObject>
7473
action.Name = method.Name;
7574
foreach (ParameterInfo parameter in method.GetParameters())
7675
{
77-
this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Hub);
7876
action.Parameters.Add(new HttpServiceActionParameterTransferObject
7977
{
8078
Name = parameter.Name,
81-
Type = parameter.ParameterType.ToTransferObject(configuration.Hub)
79+
Type = this.modelReader.Read(parameter.ParameterType, transferObjects, configuration.Hub)
8280
});
8381
}
8482
hub.Events.Add(action);

Core/Transfer/Extensions/TypeExtension.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

Core/Transfer/Extensions/TypeTransferObjectExtension.cs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -21,53 +21,5 @@ public static TypeTemplate ToTemplate(this TypeTransferObject type)
2121
type.Generics.Where(x => x.Type != null).ForEach(g => genericTypeTemplate.Types.Add(g.Type.ToTemplate()));
2222
return genericTypeTemplate;
2323
}
24-
25-
public static void FromType(this TypeTransferObject transferObject, Type type, IFromTypeOptions options = null)
26-
{
27-
if (type == null)
28-
{
29-
return;
30-
}
31-
if (type.IsGenericType)
32-
{
33-
transferObject.Name = type.Name.Split('`').First();
34-
transferObject.Namespace = type.Namespace;
35-
foreach (Type argument in type.GetGenericArguments())
36-
{
37-
transferObject.Generics.Add(new GenericAliasTransferObject { Type = argument.ToTransferObject(options) });
38-
}
39-
}
40-
else if (type.IsArray)
41-
{
42-
transferObject.Name = "Array";
43-
transferObject.Namespace = "System";
44-
Type argument = type.GetElementType() ?? typeof(object);
45-
transferObject.Generics.Add(new GenericAliasTransferObject { Type = argument.ToTransferObject(options) });
46-
}
47-
else if (type.ContainsGenericParameters)
48-
{
49-
transferObject.Name = type.Name;
50-
transferObject.HasUsing = false;
51-
}
52-
else
53-
{
54-
transferObject.Name = type.Name;
55-
transferObject.Namespace = type.Namespace;
56-
transferObject.IsNullable = !type.IsValueType;
57-
}
58-
if (transferObject.Name == nameof(Nullable))
59-
{
60-
transferObject.IsNullable = true;
61-
}
62-
if (options?.ReplaceName != null)
63-
{
64-
for (int index = 0; index < options.ReplaceName.Count; index++)
65-
{
66-
string replaceName = options.ReplaceName[index];
67-
string replaceWith = options.ReplaceWithName?.Skip(index).FirstOrDefault() ?? string.Empty;
68-
transferObject.Name = transferObject.Name.Replace(replaceName, replaceWith);
69-
}
70-
}
71-
}
7224
}
7325
}

Core/Transfer/GenericAliasTransferObject.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
{
33
public class GenericAliasTransferObject
44
{
5-
public string Alias { get; set; }
5+
public TypeTransferObject Alias { get; set; }
66
public TypeTransferObject Type { get; set; }
77
}
8-
}
8+
}

Core/Transfer/TypeTransferObject.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ namespace KY.Generator.Transfer
99
public class TypeTransferObject : ITransferObject
1010
{
1111
public string Name { get; set; }
12+
public string OriginalName { get; set; }
1213
public string Namespace { get; set; }
1314
public bool FromSystem { get; set; }
1415
public bool IsNullable { get; set; }
@@ -35,7 +36,7 @@ public TypeTransferObject(TypeTransferObject type)
3536

3637
public bool Equals(TypeTransferObject type)
3738
{
38-
return this.Name == type.Name && this.Namespace == type.Namespace;
39+
return (this.Name == type.Name || this.OriginalName == type.Name || this.Name == type.OriginalName) && this.Namespace == type.Namespace;
3940
}
4041

4142
public TypeTransferObject Clone()

Core/Transfer/Writers/ModelWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ protected virtual ClassTemplate WriteClass(IModelConfiguration configuration, Mo
115115
classTemplate.IsAbstract = model.IsAbstract;
116116
if (model.IsGeneric)
117117
{
118-
classTemplate.Generics.AddRange(model.Generics.Where(x => x.Alias != null).Select(x => new ClassGenericTemplate(x.Alias)));
118+
classTemplate.Generics.AddRange(model.Generics.Where(x => x.Alias != null).Select(x => new ClassGenericTemplate(x.Alias.Name)));
119119
}
120120
foreach (TypeTransferObject interFace in model.Interfaces)
121121
{

Reflection/Readers/ReflectionModelReader.cs

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,16 @@ public class ReflectionModelReader
1414
{
1515
public ModelTransferObject Read(Type type, List<ITransferObject> transferObjects, IFromTypeOptions options = null)
1616
{
17-
bool isFromSystem = type.Namespace != null && type.Namespace.StartsWith("System");
1817
ModelTransferObject model = new ModelTransferObject { Language = ReflectionLanguage.Instance };
19-
model.FromType(type, options);
18+
model.Name = model.OriginalName = type.Name;
19+
model.Namespace = type.Namespace;
20+
model.IsNullable = !type.IsValueType;
21+
model.IsGeneric = type.IsGenericType;
22+
model.FromSystem = type.Namespace != null && type.Namespace.StartsWith("System");
23+
if (model.IsGeneric)
24+
{
25+
model.Name = type.Name.Split('`').First();
26+
}
2027
ModelTransferObject existingModel = transferObjects.OfType<ModelTransferObject>().FirstOrDefault(entry => entry.Equals(model));
2128
if (existingModel != null)
2229
{
@@ -31,7 +38,7 @@ public ModelTransferObject Read(Type type, List<ITransferObject> transferObjects
3138
{
3239
this.ReadArray(type, model, transferObjects);
3340
}
34-
else if (type.IsGenericType && isFromSystem)
41+
else if (type.IsGenericType && model.FromSystem)
3542
{
3643
this.ReadGenericFromSystem(type, model, transferObjects);
3744
}
@@ -43,31 +50,44 @@ public ModelTransferObject Read(Type type, List<ITransferObject> transferObjects
4350
else if (type.ContainsGenericParameters)
4451
{
4552
model.HasUsing = false;
53+
model.Namespace = null;
4654
}
47-
else if (!isFromSystem)
55+
else if (!model.FromSystem)
4856
{
4957
transferObjects.Add(model);
5058
this.ReadClass(type, model, transferObjects);
5159
}
60+
if (model.Name == nameof(Nullable))
61+
{
62+
model.IsNullable = true;
63+
}
64+
if (!model.FromSystem && options?.ReplaceName != null)
65+
{
66+
for (int index = 0; index < options.ReplaceName.Count; index++)
67+
{
68+
string replaceName = options.ReplaceName[index];
69+
string replaceWith = options.ReplaceWithName?.Skip(index).FirstOrDefault() ?? string.Empty;
70+
model.Name = model.Name.Replace(replaceName, replaceWith);
71+
}
72+
}
5273
return model;
5374
}
5475

5576
private void ReadArray(Type type, ModelTransferObject model, List<ITransferObject> transferObjects)
5677
{
5778
Logger.Trace($"Reflection read array {type.Name} ({type.Namespace})");
79+
model.Name = "Array";
5880
model.IsGeneric = true;
59-
model.FromSystem = true;
60-
this.Read(type.GetElementType(), transferObjects);
81+
model.HasUsing = false;
82+
model.Generics.Add(new GenericAliasTransferObject { Type = this.Read(type.GetElementType(), transferObjects) });
6183
}
6284

6385
private void ReadGenericFromSystem(Type type, ModelTransferObject model, List<ITransferObject> transferObjects)
6486
{
6587
Logger.Trace($"Reflection read generic system type {type.Name}<{string.Join(",", type.GetGenericArguments().Select(x => x.Name))}> ({type.Namespace})");
66-
model.IsGeneric = true;
67-
model.FromSystem = true;
6888
foreach (Type argument in type.GenericTypeArguments)
6989
{
70-
this.Read(argument, transferObjects);
90+
model.Generics.Add(new GenericAliasTransferObject{ Type = this.Read(argument, transferObjects)});
7191
}
7292
}
7393

@@ -104,17 +124,16 @@ private void ReadClass(Type type, ModelTransferObject model, List<ITransferObjec
104124
if (type.IsGenericType)
105125
{
106126
Type genericType = type.GetGenericTypeDefinition();
107-
model.IsGeneric = true;
108127
model.Generics.Clear();
109128
if (genericType is TypeInfo typeInfo)
110129
{
111130
for (int index = 0; index < typeInfo.GenericTypeParameters.Length; index++)
112131
{
113-
string alias = typeInfo.GenericTypeParameters[index].Name;
132+
Type alias = typeInfo.GenericTypeParameters[index];
114133
Type argument = type.GenericTypeArguments[index];
115134
model.Generics.Add(new GenericAliasTransferObject
116135
{
117-
Alias = alias,
136+
Alias = this.Read(alias,transferObjects),
118137
Type = this.Read(argument, transferObjects)
119138
});
120139
}

Tests/WebApiController/ClientApp/src/app/parameter-on-controller/services/parameter-on.service.ts

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,4 @@
1-
// ------------------------------------------------------------------------------
2-
// <auto-generated>
3-
// This code was generated with KY.Generator 7.2.0.0
4-
//
5-
// Manual changes to this file may cause unexpected behavior in your application.
6-
// Manual changes to this file will be overwritten if the code is regenerated.
7-
// </auto-generated>
8-
// ------------------------------------------------------------------------------
9-
/* eslint-disable */
1+
/* eslint-disable */
102
// tslint:disable
113

124
import { HttpClient } from "@angular/common/http";

0 commit comments

Comments
 (0)