Skip to content

Fix task and void issues #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 11, 2017
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
27 changes: 25 additions & 2 deletions src/WebApiTestApplication/Controllers/TestController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,41 @@ public string GetSomethingElse(int id, [FromUri]DummyClass y, string hole)

[HttpGet]
[Route("GetEnumerableString")]
public IEnumerable<string> GetEnumerableString()
public IEnumerable<string> GetEnumerableString(string hole)
{
return new List<string>();
}

[HttpGet]
[Route("GetIHttpActionResult")]
public IHttpActionResult GetIHttpActionResult()
public IHttpActionResult GetIHttpActionResult(string hole)
{
return new OkResult(new HttpRequestMessage());
}

[HttpGet]
[Route("GetVoidTask")]
public async Task GetVoidTask(string hole)
{
await Task.Delay(0);
}

[HttpGet]
[Route("GetStringTask")]
public async Task<string> GetStringTask(string hole)
{
await Task.Delay(0);
return "";
}

[HttpGet]
[Route("GetEnumerableStringTask")]
public async Task<IEnumerable<string>> GetEnumerableStringTask(string hole)
{
await Task.Delay(0);
return new List<string>();
}

[HttpPost]
[Route("")]
public string Post(string hole, DummyClass value)
Expand Down
79 changes: 77 additions & 2 deletions src/WebApiTestApplication/Scripts/Endpoints/Endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace Endpoints {
GetSomethingElse: (args?: IGetSomethingElse) => IGetSomethingElseWithCall
GetEnumerableString: (args?: IGetEnumerableString) => IGetEnumerableStringWithCall
GetIHttpActionResult: (args?: IGetIHttpActionResult) => IGetIHttpActionResultWithCall
GetVoidTask: (args?: IGetVoidTask) => IGetVoidTaskWithCall
GetStringTask: (args?: IGetStringTask) => IGetStringTaskWithCall
GetEnumerableStringTask: (args?: IGetEnumerableStringTask) => IGetEnumerableStringTaskWithCall
Post: (args?: IPost) => IPostWithCall
Post1: (args?: IPost1) => IPost1WithCall
Post2: (args?: IPost2) => IPost2WithCall
Expand Down Expand Up @@ -186,6 +189,7 @@ namespace Endpoints {
}

export interface IGetEnumerableString {
hole: string;
}

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

export class GetEnumerableString implements IGetEnumerableString, IEndpoint {
_verb = 'GET';
hole: string;

constructor(args?: IGetEnumerableString) {
constructor(args: IGetEnumerableString) {
this.hole = args != null ? args.hole : null;
}

toString = (): string => {
Expand All @@ -205,6 +211,7 @@ namespace Endpoints {
}

export interface IGetIHttpActionResult {
hole: string;
}

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

export class GetIHttpActionResult implements IGetIHttpActionResult, IEndpoint {
_verb = 'GET';
hole: string;

constructor(args?: IGetIHttpActionResult) {
constructor(args: IGetIHttpActionResult) {
this.hole = args != null ? args.hole : null;
}

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

export interface IGetVoidTask {
hole: string;
}

export interface IGetVoidTaskWithCall extends IGetVoidTask, IEndpoint {
call(): ng.IPromise<void>;
callCached(): ng.IPromise<void>;
}

export class GetVoidTask implements IGetVoidTask, IEndpoint {
_verb = 'GET';
hole: string;

constructor(args: IGetVoidTask) {
this.hole = args != null ? args.hole : null;
}

toString = (): string => {
return `/api/Test/${this.hole}/actions/GetVoidTask`;
}
}

export interface IGetStringTask {
hole: string;
}

export interface IGetStringTaskWithCall extends IGetStringTask, IEndpoint {
call(): ng.IPromise<string>;
callCached(): ng.IPromise<string>;
}

export class GetStringTask implements IGetStringTask, IEndpoint {
_verb = 'GET';
hole: string;

constructor(args: IGetStringTask) {
this.hole = args != null ? args.hole : null;
}

toString = (): string => {
return `/api/Test/${this.hole}/actions/GetStringTask`;
}
}

export interface IGetEnumerableStringTask {
hole: string;
}

export interface IGetEnumerableStringTaskWithCall extends IGetEnumerableStringTask, IEndpoint {
call(): ng.IPromise<string[]>;
callCached(): ng.IPromise<string[]>;
}

export class GetEnumerableStringTask implements IGetEnumerableStringTask, IEndpoint {
_verb = 'GET';
hole: string;

constructor(args: IGetEnumerableStringTask) {
this.hole = args != null ? args.hole : null;
}

toString = (): string => {
return `/api/Test/${this.hole}/actions/GetEnumerableStringTask`;
}
}

export interface IPost {
hole: string;
}
Expand Down
43 changes: 41 additions & 2 deletions src/WebApiTestApplication/Scripts/Endpoints/Service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ namespace Endpoints {
});
},

GetEnumerableString: (args?: Endpoints.Test.IGetEnumerableString): Endpoints.Test.IGetEnumerableStringWithCall => {
GetEnumerableString: (args: Endpoints.Test.IGetEnumerableString): Endpoints.Test.IGetEnumerableStringWithCall => {
var endpoint = new Endpoints.Test.GetEnumerableString(args);
return _.extendOwn(endpoint, {
call() {
Expand All @@ -101,7 +101,7 @@ namespace Endpoints {
});
},

GetIHttpActionResult: (args?: Endpoints.Test.IGetIHttpActionResult): Endpoints.Test.IGetIHttpActionResultWithCall => {
GetIHttpActionResult: (args: Endpoints.Test.IGetIHttpActionResult): Endpoints.Test.IGetIHttpActionResultWithCall => {
var endpoint = new Endpoints.Test.GetIHttpActionResult(args);
return _.extendOwn(endpoint, {
call<TView>() {
Expand All @@ -114,6 +114,45 @@ namespace Endpoints {
});
},

GetVoidTask: (args: Endpoints.Test.IGetVoidTask): Endpoints.Test.IGetVoidTaskWithCall => {
var endpoint = new Endpoints.Test.GetVoidTask(args);
return _.extendOwn(endpoint, {
call() {
return AngularEndpointsService.call<void>(this, null);
},

callCached() {
return AngularEndpointsService.callCached<void>(this, null);
}
});
},

GetStringTask: (args: Endpoints.Test.IGetStringTask): Endpoints.Test.IGetStringTaskWithCall => {
var endpoint = new Endpoints.Test.GetStringTask(args);
return _.extendOwn(endpoint, {
call() {
return AngularEndpointsService.call<string>(this, null);
},

callCached() {
return AngularEndpointsService.callCached<string>(this, null);
}
});
},

GetEnumerableStringTask: (args: Endpoints.Test.IGetEnumerableStringTask): Endpoints.Test.IGetEnumerableStringTaskWithCall => {
var endpoint = new Endpoints.Test.GetEnumerableStringTask(args);
return _.extendOwn(endpoint, {
call() {
return AngularEndpointsService.call<string[]>(this, null);
},

callCached() {
return AngularEndpointsService.callCached<string[]>(this, null);
}
});
},

Post: (args: Endpoints.Test.IPost): Endpoints.Test.IPostWithCall => {
var endpoint = new Endpoints.Test.Post(args);
return _.extendOwn(endpoint, {
Expand Down
33 changes: 26 additions & 7 deletions src/WebApiToTypeScript/Types/TypeService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class TypeService : ServiceAware
private Dictionary<string, List<Type>> PrimitiveTypesMapping { get; }
= new Dictionary<string, List<Type>>();

private List<string> ReservedWords { get; set; }
private List<string> ReservedWords { get; set; }
= new List<string>();

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

var type = cSharpType;
var type = StripTask(cSharpType) ?? cSharpType;

var typeName = type.FullName;

var typeMapping = getTypeMapping(parameterName, cSharpType.FullName);
Expand All @@ -161,20 +162,20 @@ public TypeScriptType GetTypeScriptType(TypeReference cSharpType, string paramet
var tsTypeName = typeMapping.TypeScriptTypeName;
result.TypeName = tsTypeName;
result.InterfaceName = tsTypeName;
result.IsPrimitive = TypeService.IsPrimitiveTypeScriptType(result.TypeName);
result.IsPrimitive = IsPrimitiveTypeScriptType(result.TypeName);
result.IsEnum = tsTypeName.StartsWith($"{Config.EnumsNamespace}")
|| result.IsPrimitive;

return result;
}

typeName = TypeService.StripNullable(type) ?? typeName;
typeName = StripNullable(type) ?? typeName;

var collectionType = TypeService.StripCollection(type);
var collectionType = StripCollection(type);
result.IsCollection = collectionType != null;
typeName = collectionType ?? typeName;

var typeDefinition = TypeService.GetTypeDefinition(typeName);
var typeDefinition = GetTypeDefinition(typeName);

if (typeDefinition?.IsEnum ?? false)
{
Expand All @@ -197,7 +198,7 @@ public TypeScriptType GetTypeScriptType(TypeReference cSharpType, string paramet
return result;
}

var primitiveType = TypeService.GetPrimitiveTypeScriptType(typeName);
var primitiveType = GetPrimitiveTypeScriptType(typeName);

if (!string.IsNullOrEmpty(primitiveType))
{
Expand Down Expand Up @@ -312,6 +313,23 @@ public string StripCollection(TypeReference type)
return null;
}

public TypeReference StripTask(TypeReference type)
{
var taskType = "System.Threading.Tasks.Task";

var genericType = type as GenericInstanceType;
if (genericType != null && genericType.FullName.StartsWith(taskType))
{
return genericType.GenericArguments.Single();
}
else if (type.FullName.StartsWith(taskType))
{
return this.Types.First(x => x.FullName == "System.Void");
}

return null;
}

private void LoadReservedWords()
{
ReservedWords = new List<string>
Expand All @@ -328,6 +346,7 @@ private void LoadPrimitiveTypesMapping()
mapping["boolean"] = new List<Type> { typeof(bool) };
mapping["number"] = new List<Type> { typeof(byte), typeof(short), typeof(int), typeof(long), typeof(float), typeof(double), typeof(decimal) };
mapping["any"] = new List<Type> { typeof(object) };
mapping["void"] = new List<Type> { typeof(void) };
}
}
}