Skip to content

Commit 3d72f92

Browse files
committed
Fix task and void issues
1 parent 864f0ad commit 3d72f92

File tree

4 files changed

+169
-13
lines changed

4 files changed

+169
-13
lines changed

src/WebApiTestApplication/Controllers/TestController.cs

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,18 +117,41 @@ public string GetSomethingElse(int id, [FromUri]DummyClass y, string hole)
117117

118118
[HttpGet]
119119
[Route("GetEnumerableString")]
120-
public IEnumerable<string> GetEnumerableString()
120+
public IEnumerable<string> GetEnumerableString(string hole)
121121
{
122122
return new List<string>();
123123
}
124124

125125
[HttpGet]
126126
[Route("GetIHttpActionResult")]
127-
public IHttpActionResult GetIHttpActionResult()
127+
public IHttpActionResult GetIHttpActionResult(string hole)
128128
{
129129
return new OkResult(new HttpRequestMessage());
130130
}
131131

132+
[HttpGet]
133+
[Route("GetVoidTask")]
134+
public async Task GetVoidTask(string hole)
135+
{
136+
await Task.Delay(0);
137+
}
138+
139+
[HttpGet]
140+
[Route("GetStringTask")]
141+
public async Task<string> GetStringTask(string hole)
142+
{
143+
await Task.Delay(0);
144+
return "";
145+
}
146+
147+
[HttpGet]
148+
[Route("GetEnumerableStringTask")]
149+
public async Task<IEnumerable<string>> GetEnumerableStringTask(string hole)
150+
{
151+
await Task.Delay(0);
152+
return new List<string>();
153+
}
154+
132155
[HttpPost]
133156
[Route("")]
134157
public string Post(string hole, DummyClass value)

src/WebApiTestApplication/Scripts/Endpoints/Endpoints.ts

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ namespace Endpoints {
4141
GetSomethingElse: (args?: IGetSomethingElse) => IGetSomethingElseWithCall
4242
GetEnumerableString: (args?: IGetEnumerableString) => IGetEnumerableStringWithCall
4343
GetIHttpActionResult: (args?: IGetIHttpActionResult) => IGetIHttpActionResultWithCall
44+
GetVoidTask: (args?: IGetVoidTask) => IGetVoidTaskWithCall
45+
GetStringTask: (args?: IGetStringTask) => IGetStringTaskWithCall
46+
GetEnumerableStringTask: (args?: IGetEnumerableStringTask) => IGetEnumerableStringTaskWithCall
4447
Post: (args?: IPost) => IPostWithCall
4548
Post1: (args?: IPost1) => IPost1WithCall
4649
Post2: (args?: IPost2) => IPost2WithCall
@@ -186,6 +189,7 @@ namespace Endpoints {
186189
}
187190

188191
export interface IGetEnumerableString {
192+
hole: string;
189193
}
190194

191195
export interface IGetEnumerableStringWithCall extends IGetEnumerableString, IEndpoint {
@@ -195,8 +199,10 @@ namespace Endpoints {
195199

196200
export class GetEnumerableString implements IGetEnumerableString, IEndpoint {
197201
_verb = 'GET';
202+
hole: string;
198203

199-
constructor(args?: IGetEnumerableString) {
204+
constructor(args: IGetEnumerableString) {
205+
this.hole = args != null ? args.hole : null;
200206
}
201207

202208
toString = (): string => {
@@ -205,6 +211,7 @@ namespace Endpoints {
205211
}
206212

207213
export interface IGetIHttpActionResult {
214+
hole: string;
208215
}
209216

210217
export interface IGetIHttpActionResultWithCall extends IGetIHttpActionResult, IEndpoint {
@@ -214,15 +221,83 @@ namespace Endpoints {
214221

215222
export class GetIHttpActionResult implements IGetIHttpActionResult, IEndpoint {
216223
_verb = 'GET';
224+
hole: string;
217225

218-
constructor(args?: IGetIHttpActionResult) {
226+
constructor(args: IGetIHttpActionResult) {
227+
this.hole = args != null ? args.hole : null;
219228
}
220229

221230
toString = (): string => {
222231
return `/api/Test/${this.hole}/actions/GetIHttpActionResult`;
223232
}
224233
}
225234

235+
export interface IGetVoidTask {
236+
hole: string;
237+
}
238+
239+
export interface IGetVoidTaskWithCall extends IGetVoidTask, IEndpoint {
240+
call(): ng.IPromise<void>;
241+
callCached(): ng.IPromise<void>;
242+
}
243+
244+
export class GetVoidTask implements IGetVoidTask, IEndpoint {
245+
_verb = 'GET';
246+
hole: string;
247+
248+
constructor(args: IGetVoidTask) {
249+
this.hole = args != null ? args.hole : null;
250+
}
251+
252+
toString = (): string => {
253+
return `/api/Test/${this.hole}/actions/GetVoidTask`;
254+
}
255+
}
256+
257+
export interface IGetStringTask {
258+
hole: string;
259+
}
260+
261+
export interface IGetStringTaskWithCall extends IGetStringTask, IEndpoint {
262+
call(): ng.IPromise<string>;
263+
callCached(): ng.IPromise<string>;
264+
}
265+
266+
export class GetStringTask implements IGetStringTask, IEndpoint {
267+
_verb = 'GET';
268+
hole: string;
269+
270+
constructor(args: IGetStringTask) {
271+
this.hole = args != null ? args.hole : null;
272+
}
273+
274+
toString = (): string => {
275+
return `/api/Test/${this.hole}/actions/GetStringTask`;
276+
}
277+
}
278+
279+
export interface IGetEnumerableStringTask {
280+
hole: string;
281+
}
282+
283+
export interface IGetEnumerableStringTaskWithCall extends IGetEnumerableStringTask, IEndpoint {
284+
call(): ng.IPromise<string[]>;
285+
callCached(): ng.IPromise<string[]>;
286+
}
287+
288+
export class GetEnumerableStringTask implements IGetEnumerableStringTask, IEndpoint {
289+
_verb = 'GET';
290+
hole: string;
291+
292+
constructor(args: IGetEnumerableStringTask) {
293+
this.hole = args != null ? args.hole : null;
294+
}
295+
296+
toString = (): string => {
297+
return `/api/Test/${this.hole}/actions/GetEnumerableStringTask`;
298+
}
299+
}
300+
226301
export interface IPost {
227302
hole: string;
228303
}

src/WebApiTestApplication/Scripts/Endpoints/Service.ts

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace Endpoints {
8888
});
8989
},
9090

91-
GetEnumerableString: (args?: Endpoints.Test.IGetEnumerableString): Endpoints.Test.IGetEnumerableStringWithCall => {
91+
GetEnumerableString: (args: Endpoints.Test.IGetEnumerableString): Endpoints.Test.IGetEnumerableStringWithCall => {
9292
var endpoint = new Endpoints.Test.GetEnumerableString(args);
9393
return _.extendOwn(endpoint, {
9494
call() {
@@ -101,7 +101,7 @@ namespace Endpoints {
101101
});
102102
},
103103

104-
GetIHttpActionResult: (args?: Endpoints.Test.IGetIHttpActionResult): Endpoints.Test.IGetIHttpActionResultWithCall => {
104+
GetIHttpActionResult: (args: Endpoints.Test.IGetIHttpActionResult): Endpoints.Test.IGetIHttpActionResultWithCall => {
105105
var endpoint = new Endpoints.Test.GetIHttpActionResult(args);
106106
return _.extendOwn(endpoint, {
107107
call<TView>() {
@@ -114,6 +114,45 @@ namespace Endpoints {
114114
});
115115
},
116116

117+
GetVoidTask: (args: Endpoints.Test.IGetVoidTask): Endpoints.Test.IGetVoidTaskWithCall => {
118+
var endpoint = new Endpoints.Test.GetVoidTask(args);
119+
return _.extendOwn(endpoint, {
120+
call() {
121+
return AngularEndpointsService.call<void>(this, null);
122+
},
123+
124+
callCached() {
125+
return AngularEndpointsService.callCached<void>(this, null);
126+
}
127+
});
128+
},
129+
130+
GetStringTask: (args: Endpoints.Test.IGetStringTask): Endpoints.Test.IGetStringTaskWithCall => {
131+
var endpoint = new Endpoints.Test.GetStringTask(args);
132+
return _.extendOwn(endpoint, {
133+
call() {
134+
return AngularEndpointsService.call<string>(this, null);
135+
},
136+
137+
callCached() {
138+
return AngularEndpointsService.callCached<string>(this, null);
139+
}
140+
});
141+
},
142+
143+
GetEnumerableStringTask: (args: Endpoints.Test.IGetEnumerableStringTask): Endpoints.Test.IGetEnumerableStringTaskWithCall => {
144+
var endpoint = new Endpoints.Test.GetEnumerableStringTask(args);
145+
return _.extendOwn(endpoint, {
146+
call() {
147+
return AngularEndpointsService.call<string[]>(this, null);
148+
},
149+
150+
callCached() {
151+
return AngularEndpointsService.callCached<string[]>(this, null);
152+
}
153+
});
154+
},
155+
117156
Post: (args: Endpoints.Test.IPost): Endpoints.Test.IPostWithCall => {
118157
var endpoint = new Endpoints.Test.Post(args);
119158
return _.extendOwn(endpoint, {

src/WebApiToTypeScript/Types/TypeService.cs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class TypeService : ServiceAware
1616
private Dictionary<string, List<Type>> PrimitiveTypesMapping { get; }
1717
= new Dictionary<string, List<Type>>();
1818

19-
private List<string> ReservedWords { get; set; }
19+
private List<string> ReservedWords { get; set; }
2020
= new List<string>();
2121

2222
public List<TypeDefinition> Types { get; }
@@ -151,7 +151,8 @@ public TypeScriptType GetTypeScriptType(TypeReference cSharpType, string paramet
151151
{
152152
var result = new TypeScriptType();
153153

154-
var type = cSharpType;
154+
var type = StripTask(cSharpType) ?? cSharpType;
155+
155156
var typeName = type.FullName;
156157

157158
var typeMapping = getTypeMapping(parameterName, cSharpType.FullName);
@@ -161,20 +162,20 @@ public TypeScriptType GetTypeScriptType(TypeReference cSharpType, string paramet
161162
var tsTypeName = typeMapping.TypeScriptTypeName;
162163
result.TypeName = tsTypeName;
163164
result.InterfaceName = tsTypeName;
164-
result.IsPrimitive = TypeService.IsPrimitiveTypeScriptType(result.TypeName);
165+
result.IsPrimitive = IsPrimitiveTypeScriptType(result.TypeName);
165166
result.IsEnum = tsTypeName.StartsWith($"{Config.EnumsNamespace}")
166167
|| result.IsPrimitive;
167168

168169
return result;
169170
}
170171

171-
typeName = TypeService.StripNullable(type) ?? typeName;
172+
typeName = StripNullable(type) ?? typeName;
172173

173-
var collectionType = TypeService.StripCollection(type);
174+
var collectionType = StripCollection(type);
174175
result.IsCollection = collectionType != null;
175176
typeName = collectionType ?? typeName;
176177

177-
var typeDefinition = TypeService.GetTypeDefinition(typeName);
178+
var typeDefinition = GetTypeDefinition(typeName);
178179

179180
if (typeDefinition?.IsEnum ?? false)
180181
{
@@ -197,7 +198,7 @@ public TypeScriptType GetTypeScriptType(TypeReference cSharpType, string paramet
197198
return result;
198199
}
199200

200-
var primitiveType = TypeService.GetPrimitiveTypeScriptType(typeName);
201+
var primitiveType = GetPrimitiveTypeScriptType(typeName);
201202

202203
if (!string.IsNullOrEmpty(primitiveType))
203204
{
@@ -312,6 +313,23 @@ public string StripCollection(TypeReference type)
312313
return null;
313314
}
314315

316+
public TypeReference StripTask(TypeReference type)
317+
{
318+
var taskType = "System.Threading.Tasks.Task";
319+
320+
var genericType = type as GenericInstanceType;
321+
if (genericType != null && genericType.FullName.StartsWith(taskType))
322+
{
323+
return genericType.GenericArguments.Single();
324+
}
325+
else if (type.FullName.StartsWith(taskType))
326+
{
327+
return this.Types.First(x => x.FullName == "System.Void");
328+
}
329+
330+
return null;
331+
}
332+
315333
private void LoadReservedWords()
316334
{
317335
ReservedWords = new List<string>
@@ -328,6 +346,7 @@ private void LoadPrimitiveTypesMapping()
328346
mapping["boolean"] = new List<Type> { typeof(bool) };
329347
mapping["number"] = new List<Type> { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) };
330348
mapping["any"] = new List<Type> { typeof(object) };
349+
mapping["void"] = new List<Type> { typeof(void) };
331350
}
332351
}
333352
}

0 commit comments

Comments
 (0)