Fixed CSharpDataTypeMappingProvider for string type.

This commit is contained in:
Marek Stój 2012-05-12 10:56:59 +02:00
parent 2633eb77e2
commit 43d15d4d00
3 changed files with 152 additions and 149 deletions

View File

@ -12,151 +12,150 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
*/ */
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Net; using System.Net;
using System.Text; using System.Text;
using System.Web; using System.Web;
using Newtonsoft.Json; using Newtonsoft.Json;
using SwaggerRuntime.Exceptions; using SwaggerRuntime.Exceptions;
namespace SwaggerRuntime.Common namespace SwaggerRuntime.Common
{ {
public abstract class ApiBase public abstract class ApiBase
{ {
private readonly string _apiBaseUrl; private readonly string _apiBaseUrl;
private readonly ISecurityHandler _securityHandler; private readonly ISecurityHandler _securityHandler;
protected ApiBase(string apiBaseUrl, ISecurityHandler securityHandler) protected ApiBase(string apiBaseUrl, ISecurityHandler securityHandler)
{ {
if (string.IsNullOrEmpty(apiBaseUrl)) if (string.IsNullOrEmpty(apiBaseUrl))
{ {
throw new ArgumentException("Argument can't be null nor empty.", "apiBaseUrl"); throw new ArgumentException("Argument can't be null nor empty.", "apiBaseUrl");
} }
if (securityHandler == null) if (securityHandler == null)
{ {
throw new ArgumentNullException("securityHandler"); throw new ArgumentNullException("securityHandler");
} }
_apiBaseUrl = apiBaseUrl; _apiBaseUrl = apiBaseUrl;
_securityHandler = securityHandler; _securityHandler = securityHandler;
} }
protected string InvokeApi(string resourceUrl, string method, IDictionary<string, string> queryParams, Object postData, IDictionary<string, string> headerParams) protected string InvokeApi(string resourceUrl, string method, IDictionary<string, string> queryParams, Object postData, IDictionary<string, string> headerParams)
{ {
string absoluteResourceUrl = _apiBaseUrl + resourceUrl; string absoluteResourceUrl = _apiBaseUrl + resourceUrl;
if (queryParams.Count > 0) if (queryParams.Count > 0)
{ {
bool isFirst = true; bool isFirst = true;
foreach (string queryParamName in queryParams.Keys) foreach (string queryParamName in queryParams.Keys)
{ {
absoluteResourceUrl += isFirst ? "?" : "&"; absoluteResourceUrl += isFirst ? "?" : "&";
isFirst = false; isFirst = false;
absoluteResourceUrl += queryParamName + "=" + EncodeUrl(queryParams[queryParamName]); absoluteResourceUrl += queryParamName + "=" + EncodeUrl(queryParams[queryParamName]);
} }
} }
var headers = new Dictionary<string, string>(); var headers = new Dictionary<string, string>();
_securityHandler.PopulateSecurityInfo(absoluteResourceUrl, headers); _securityHandler.PopulateSecurityInfo(absoluteResourceUrl, headers);
var request = (HttpWebRequest)WebRequest.Create(absoluteResourceUrl); var request = (HttpWebRequest)WebRequest.Create(absoluteResourceUrl);
request.Method = method; request.Method = method;
foreach (KeyValuePair<string, string> headerKvp in headers) foreach (KeyValuePair<string, string> headerKvp in headers)
{ {
request.Headers[headerKvp.Key] = headerKvp.Value; request.Headers[headerKvp.Key] = headerKvp.Value;
} }
if (headerParams != null) if (headerParams != null)
{ {
foreach (KeyValuePair<string, string> headerKvp in headerParams) foreach (KeyValuePair<string, string> headerKvp in headerParams)
{ {
request.Headers[headerKvp.Key] = headerKvp.Value; request.Headers[headerKvp.Key] = headerKvp.Value;
} }
} }
using (var response = (HttpWebResponse)request.GetResponse()) using (var response = (HttpWebResponse)request.GetResponse())
using (Stream responseStream = response.GetResponseStream()) using (Stream responseStream = response.GetResponseStream())
{ {
if (responseStream == null) if (responseStream == null)
{ {
throw new IOException("Couldn't get response stream."); throw new IOException("Couldn't get response stream.");
} }
if (response.StatusCode != HttpStatusCode.OK) if (response.StatusCode != HttpStatusCode.OK)
{ {
throw new ApiException((int)response.StatusCode); throw new ApiException((int)response.StatusCode);
} }
using (var sr = new StreamReader(responseStream)) using (var sr = new StreamReader(responseStream))
{ {
return sr.ReadToEnd(); return sr.ReadToEnd();
} }
} }
} }
protected T Deserialize<T>(string response) protected T Deserialize<T>(string response)
{ {
var jsonSerializer = new JsonSerializer(); var jsonSerializer = new JsonSerializer();
using (var sr = new StringReader(response)) using (var sr = new StringReader(response))
using (var jtr = new JsonTextReader(sr)) using (var jtr = new JsonTextReader(sr))
{ {
return jsonSerializer.Deserialize<T>(jtr); return jsonSerializer.Deserialize<T>(jtr);
} }
} }
protected string Serialize(object input) protected string Serialize(object input)
{ {
var jsonSerializer = new JsonSerializer(); var jsonSerializer = new JsonSerializer();
var sb = new StringBuilder(); var sb = new StringBuilder();
using (var sw = new StringWriter(sb)) using (var sw = new StringWriter(sb))
{ {
jsonSerializer.Serialize(sw, input); jsonSerializer.Serialize(sw, input);
} }
return sb.ToString(); return sb.ToString();
} }
protected string ToPathValue(string value) protected string ToPathValue(string value)
{ {
return EncodeUrl(value ?? ""); return EncodeUrl(value ?? "");
} }
protected string ToPathValue(IEnumerable<object> objects) protected string ToPathValue(IEnumerable<object> objects)
{ {
StringBuilder outSb = new StringBuilder(); StringBuilder outSb = new StringBuilder();
foreach (object obj in objects) foreach (object obj in objects)
{ {
outSb.Append(obj.ToString()); outSb.Append(obj.ToString());
outSb.Append(","); outSb.Append(",");
} }
string output = outSb.ToString(); string output = outSb.ToString();
if (output.IndexOf(",") != -1) if (output.IndexOf(",") != -1)
{ {
output = output.Substring(0, output.LastIndexOf(",")); output = output.Substring(0, output.LastIndexOf(","));
} }
return EncodeUrl(output); return EncodeUrl(output);
} }
protected string EncodeUrl(string value) protected string EncodeUrl(string value)
{ {
// TODO IMM HI: do we need to replace: .replaceAll("\\+", "%20"); ???? return HttpUtility.UrlEncode(value);
return HttpUtility.UrlEncode(value); }
} }
} }
}

View File

@ -34,7 +34,7 @@ public class CSharpDataTypeMappingProvider implements DataTypeMappingProvider {
private static final Map<String, String> _primitiveValueMap; private static final Map<String, String> _primitiveValueMap;
private static final Map<String, String> _primitiveObjectMap; private static final Map<String, String> _primitiveObjectMap;
private NamingPolicyProvider _nameGenerator = new CamelCaseNamingPolicyProvider(); private NamingPolicyProvider _nameGenerator = new CSharpNamingPolicyProvider();
static { static {
_primitiveValueMap = new HashMap<String, String>(); _primitiveValueMap = new HashMap<String, String>();

View File

@ -45,6 +45,10 @@ public class CSharpNamingPolicyProvider implements NamingPolicyProvider {
@Override @Override
public String applyClassNamingPolicy(String input) { public String applyClassNamingPolicy(String input) {
if (input != null && input.length() > 0) { if (input != null && input.length() > 0) {
if ("string".equalsIgnoreCase(input)) {
return "string";
}
String output = input.substring(0, 1).toUpperCase() + input.substring(1); String output = input.substring(0, 1).toUpperCase() + input.substring(1);
// class name can't have . so if dot exists remove the same // class name can't have . so if dot exists remove the same
output = output.replace(".", ""); output = output.replace(".", "");