From 93c0134da929e02c24f7fd99c7ccdaa3d631c243 Mon Sep 17 00:00:00 2001 From: Russell Horton Date: Thu, 20 Dec 2012 14:25:14 -0800 Subject: [PATCH] better url escaping for path values --- src/main/resources/php/Swagger.mustache | 35 ++++++++++++++++--------- src/main/resources/php/api.mustache | 18 +------------ 2 files changed, 23 insertions(+), 30 deletions(-) diff --git a/src/main/resources/php/Swagger.mustache b/src/main/resources/php/Swagger.mustache index d3e69440369..53e45e7dda9 100644 --- a/src/main/resources/php/Swagger.mustache +++ b/src/main/resources/php/Swagger.mustache @@ -67,7 +67,7 @@ class APIClient { } if (is_object($postData) or is_array($postData)) { - $postData = json_encode($postData); + $postData = json_encode(self::sanitizeForSerialization($postData)); } $url = $this->apiServer . $resourcePath; @@ -119,10 +119,21 @@ class APIClient { $response_info['http_code']); } + 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 @@ -132,15 +143,15 @@ class APIClient { */ public static function toPathValue($object) { if (is_array($object)) { - return implode(',', $object); + return rawurlencode(implode(',', $object)); } 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 string $class class name is passed as a string @@ -178,17 +189,14 @@ class APIClient { if (! property_exists($class, $true_property)) { if (substr($property, -1) == 's') { $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'))) { settype($value, $type); $instance->{$true_property} = $value; @@ -209,3 +217,4 @@ class APIClient { ?> + diff --git a/src/main/resources/php/api.mustache b/src/main/resources/php/api.mustache index 46b77012a80..796f1bf9fe8 100644 --- a/src/main/resources/php/api.mustache +++ b/src/main/resources/php/api.mustache @@ -26,22 +26,6 @@ class {{classname}} { $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}} /** * {{nickname}} @@ -77,7 +61,7 @@ class {{classname}} { {{#pathParams}} if(${{paramName}} != null) { $resourcePath = str_replace("{" . "{{paramName}}" . "}", - ${{paramName}}, $resourcePath); + $this->apiClient->toPathValue(${{paramName}}), $resourcePath); } {{/pathParams}}