Skip to content

Commit

Permalink
new C# codes generated
Browse files Browse the repository at this point in the history
  • Loading branch information
zijianhuang committed Jun 24, 2024
1 parent d850324 commit 464fed0
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 31 deletions.
59 changes: 28 additions & 31 deletions Fonlow.OpenApiClientGen.CS/ClientApiFunctionGen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,7 @@ void RenderGetOrDeleteImplementation(OperationType httpMethod)
new CodeSnippetExpression(uriText)));

clientMethod.Statements.Add(new CodeSnippetStatement(
ThreeTabs + $@"using (var httpRequestMessage = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.{httpMethod}, requestUri))
{{"
ThreeTabs + $"using var httpRequestMessage = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.{httpMethod}, requestUri);"
));

if (settings.HandleHttpRequestHeaders)
Expand Down Expand Up @@ -317,12 +316,12 @@ void RenderGetOrDeleteImplementation(OperationType httpMethod)

try1.FinallyStatements.Add(new CodeMethodInvokeExpression(resultReference, "Dispose"));

Add3TEndBacket(clientMethod);
//Add3TEndBacket(clientMethod); for using
}

static void AddNewtonSoftJsonTextReader(CodeStatementCollection statementCollection)
static void AddNewtonSoftJsonUsingTextReader(CodeStatementCollection statementCollection)
{
statementCollection.Add(new CodeSnippetStatement("\t\t\t\tusing (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream)))"));
statementCollection.Add(new CodeSnippetStatement("\t\t\t\tusing JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(streamContent));"));
}

void AddNewtonSoftJsonSerializerDeserialize(CodeStatementCollection statementCollection)
Expand Down Expand Up @@ -395,8 +394,7 @@ void AddRequestUriWithQueryAssignmentStatement()
AddRequestUriWithQueryAssignmentStatement();

clientMethod.Statements.Add(new CodeSnippetStatement(
ThreeTabs + $@"using (var httpRequestMessage = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.{httpMethod}, requestUri))
{{"
ThreeTabs + $"using var httpRequestMessage = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.{httpMethod}, requestUri);"
));

if (requestBodyCodeTypeReference != null)
Expand All @@ -411,8 +409,7 @@ void AddRequestUriWithQueryAssignmentStatement()
else
{
clientMethod.Statements.Add(new CodeSnippetStatement(
ThreeTabs + @"using (var requestWriter = new System.IO.StringWriter())
{
ThreeTabs + @"using var requestWriter = new System.IO.StringWriter();
var requestSerializer = JsonSerializer.Create(jsonSerializerSettings);"
));
clientMethod.Statements.Add(new CodeMethodInvokeExpression(new CodeSnippetExpression("requestSerializer"), "Serialize",
Expand Down Expand Up @@ -453,12 +450,12 @@ void AddRequestUriWithQueryAssignmentStatement()

try1.FinallyStatements.Add(new CodeMethodInvokeExpression(resultReference, "Dispose"));

if (requestBodyCodeTypeReference != null && !settings.UseSystemTextJson)
{
Add3TEndBacket(clientMethod);
}
//if (requestBodyCodeTypeReference != null && !settings.UseSystemTextJson)
//{
// //Add3TEndBacket(clientMethod);
//}

Add3TEndBacket(clientMethod);
////Add3TEndBacket(clientMethod); for using
}

static void Add3TEndBacket(CodeMemberMethod method)
Expand All @@ -481,14 +478,14 @@ void DeserializeContentString(CodeStatementCollection statementCollection)
statementCollection.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeVariableReferenceExpression("JsonSerializer"), "Deserialize", returnTypeReference),
new CodeSnippetExpression("stream"), new CodeSnippetExpression("jsonSerializerSettings"))));
new CodeSnippetExpression("streamContent"), new CodeSnippetExpression("jsonSerializerSettings"))));
}

void AddResponseMessageReadStream(CodeStatementCollection statementCollection)
{
statementCollection.Add(new CodeSnippetStatement(forAsync
? "\t\t\t\tvar stream = await responseMessage.Content.ReadAsStreamAsync();"
: "\t\t\t\tvar stream = responseMessage.Content.ReadAsStream();"));
? "\t\t\t\tvar streamContent = await responseMessage.Content.ReadAsStreamAsync();"
: "\t\t\t\tvar streamContent = responseMessage.Content.ReadAsStream();"));
}

void AddReturnStatement(CodeStatementCollection statementCollection)
Expand All @@ -505,15 +502,15 @@ void AddReturnStatement(CodeStatementCollection statementCollection)
statementCollection.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeVariableReferenceExpression("JsonSerializer"), "Deserialize", new CodeTypeReference(typeof(System.String))),
new CodeSnippetExpression("stream"),
new CodeSnippetExpression("streamContent"),
new CodeSnippetExpression("jsonSerializerSettings"))));
}
else
{
statementCollection.Add(new CodeSnippetStatement("\t\t\t\tusing (System.IO.StreamReader streamReader = new System.IO.StreamReader(stream))"));
Add4TStartBacket(statementCollection);
statementCollection.Add(new CodeSnippetStatement("\t\t\t\tusing System.IO.StreamReader streamReader = new System.IO.StreamReader(streamContent);"));
//Add4TStartBacket(statementCollection); // for using
statementCollection.Add(new CodeMethodReturnStatement(new CodeSnippetExpression("streamReader.ReadToEnd();")));
Add4TEndBacket(statementCollection);
//Add4TEndBacket(statementCollection); for using
}
}
else
Expand All @@ -523,15 +520,15 @@ void AddReturnStatement(CodeStatementCollection statementCollection)
statementCollection.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(
new CodeMethodReferenceExpression(
new CodeVariableReferenceExpression("JsonSerializer"), "Deserialize", new CodeTypeReference(typeof(System.String))),
new CodeSnippetExpression("stream"),
new CodeSnippetExpression("streamContent"),
new CodeSnippetExpression("jsonSerializerSettings"))));
}
else
{
AddNewtonSoftJsonTextReader(statementCollection);
Add4TStartBacket(statementCollection);
AddNewtonSoftJsonUsingTextReader(statementCollection);
//Add4TStartBacket(statementCollection);
statementCollection.Add(new CodeMethodReturnStatement(new CodeSnippetExpression("jsonReader.ReadAsString()")));
Add4TEndBacket(statementCollection);
//Add4TEndBacket(statementCollection); for using
}
}
}
Expand All @@ -543,11 +540,11 @@ void AddReturnStatement(CodeStatementCollection statementCollection)
}
else
{
AddNewtonSoftJsonTextReader(statementCollection);
Add4TStartBacket(statementCollection);
AddNewtonSoftJsonUsingTextReader(statementCollection);
//Add4TStartBacket(statementCollection); // for using
AddNewtonSoftJsonSerializer(statementCollection);
AddNewtonSoftJsonSerializerDeserialize(statementCollection);
Add4TEndBacket(statementCollection);
//Add4TEndBacket(statementCollection); for using
}
}
else // then is complex.
Expand All @@ -558,11 +555,11 @@ void AddReturnStatement(CodeStatementCollection statementCollection)
}
else
{
AddNewtonSoftJsonTextReader(statementCollection);
Add4TStartBacket(statementCollection);
AddNewtonSoftJsonUsingTextReader(statementCollection);
//Add4TStartBacket(statementCollection); for using
AddNewtonSoftJsonSerializer(statementCollection);
AddNewtonSoftJsonSerializerDeserialize(statementCollection);
Add4TEndBacket(statementCollection);
//Add4TEndBacket(statementCollection); for using
}
}

Expand Down
57 changes: 57 additions & 0 deletions Fonlow.OpenApiClientGen.ClientTypes/CodeGenExceptions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Fonlow.OpenApiClientGen.ClientTypes
{
[Serializable]
public class CodeGenOperationException : Exception
{
public CodeGenOperationException()
{

}

/// <summary>
/// The thrower has no enough info to report, and the catcher with enough info should throw a new one with the info to be displayed in console.
/// </summary>
public bool Pending { get; set; }

public CodeGenOperationException(string message) : base(message) { }

public CodeGenOperationException(string message, Exception innerException) : base(message, innerException) { }
}

[Serializable]
public class CodeGenLoadPluginException : Exception
{
public CodeGenLoadPluginException()
{

}

public CodeGenLoadPluginException(string message) : base(message) { }

public CodeGenLoadPluginException(string message, Exception innerException) : base(message, innerException) { }

public string Description { get; set; }
}

[Serializable]
public class CodeGenReadPluginException : Exception
{
public CodeGenReadPluginException()
{

}

public CodeGenReadPluginException(string message) : base(message) { }

public CodeGenReadPluginException(string message, Exception innerException) : base(message, innerException) { }

public string Description { get; set; }
}

}

0 comments on commit 464fed0

Please sign in to comment.