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

View File

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