better url escaping for path values

This commit is contained in:
Russell Horton 2012-12-20 14:25:14 -08:00
parent 5840120124
commit 93c0134da9
2 changed files with 23 additions and 30 deletions

View File

@ -67,7 +67,7 @@ class APIClient {
} }
if (is_object($postData) or is_array($postData)) { if (is_object($postData) or is_array($postData)) {
$postData = json_encode($postData); $postData = json_encode(self::sanitizeForSerialization($postData));
} }
$url = $this->apiServer . $resourcePath; $url = $this->apiServer . $resourcePath;
@ -119,10 +119,21 @@ class APIClient {
$response_info['http_code']); $response_info['http_code']);
} }
return $data; return $data;
} }
/**
* Build a JSON POST object
*/
public static function sanitizeForSerialization($postData) {
foreach ($postData as $key => $value) {
if (is_a($value, "DateTime")) {
$postData->{$key} = $value->format(DateTime::ISO8601);
}
}
return $postData;
}
/** /**
* Take value and turn it into a string suitable for inclusion in * Take value and turn it into a string suitable for inclusion in
@ -132,15 +143,15 @@ class APIClient {
*/ */
public static function toPathValue($object) { public static function toPathValue($object) {
if (is_array($object)) { if (is_array($object)) {
return implode(',', $object); return rawurlencode(implode(',', $object));
} else { } else {
return $object; return rawurlencode($object);
} }
} }
/** /**
* Derialize a JSON string into an object * Deserialize a JSON string into an object
* *
* @param object $object object or primitive to be deserialized * @param object $object object or primitive to be deserialized
* @param string $class class name is passed as a string * @param string $class class name is passed as a string
@ -178,17 +189,14 @@ class APIClient {
if (! property_exists($class, $true_property)) { if (! property_exists($class, $true_property)) {
if (substr($property, -1) == 's') { if (substr($property, -1) == 's') {
$true_property = substr($property, 0, -1); $true_property = substr($property, 0, -1);
if (! property_exists($class, $true_property)) {
trigger_error("class $class has no property $property"
. " or $true_property", E_USER_WARNING);
}
} else {
trigger_error("class $class has no property $property",
E_USER_WARNING);
} }
} }
$type = $classVars['swaggerTypes'][$true_property]; if (array_key_exists($true_property, $classVars['swaggerTypes'])) {
$type = $classVars['swaggerTypes'][$true_property];
} else {
$type = 'string';
}
if (in_array($type, array('string', 'int', 'float', 'bool'))) { if (in_array($type, array('string', 'int', 'float', 'bool'))) {
settype($value, $type); settype($value, $type);
$instance->{$true_property} = $value; $instance->{$true_property} = $value;
@ -209,3 +217,4 @@ class APIClient {
?> ?>

View File

@ -26,22 +26,6 @@ class {{classname}} {
$this->apiClient = $apiClient; $this->apiClient = $apiClient;
} }
{{#queryParams}}
if ('{{paramName}}' in params):
queryParams['{{paramName}}'] = self.apiClient.toPathValue(params['{{paramName}}'])
{{/queryParams}}
{{#headerParams}}
if ('{{paramName}}' in params):
headerParams['{{paramName}}'] = params['{{paramName}}']
{{/headerParams}}
{{#pathParams}}
if ('{{paramName}}' in params):
resourcePath = resourcePath.replace('{' + '{{baseName}}' + '}', self.apiClient.toPathValue(params['{{paramName}}']))
{{/pathParams}}
{{#operation}} {{#operation}}
/** /**
* {{nickname}} * {{nickname}}
@ -77,7 +61,7 @@ class {{classname}} {
{{#pathParams}} {{#pathParams}}
if(${{paramName}} != null) { if(${{paramName}} != null) {
$resourcePath = str_replace("{" . "{{paramName}}" . "}", $resourcePath = str_replace("{" . "{{paramName}}" . "}",
${{paramName}}, $resourcePath); $this->apiClient->toPathValue(${{paramName}}), $resourcePath);
} }
{{/pathParams}} {{/pathParams}}