Skip to content

Commit

Permalink
added support for param type stringJSON (#27)
Browse files Browse the repository at this point in the history
* added support for param type stringJSON

* switched updateInventory params from array(int) to strings. etsy actually supports these and they dont kill oauth

* basic setup for tests

* Updated request validator to allow array(int) to be passed as comma separated string + created tests for this behaviour

* put back correct type on update inventory params

* Changed param format for array(int)

* test for array(int) properties
  • Loading branch information
kievins authored and inakiabt committed Sep 7, 2017
1 parent 67d7573 commit 72746c3
Show file tree
Hide file tree
Showing 4 changed files with 1,293 additions and 327 deletions.
55 changes: 29 additions & 26 deletions src/Etsy/RequestValidator.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ public static function validateData($args, $methodInfo)
{
$validType = $methodsParams[$name];
$type = self::transformValueType(gettype($arg));

switch($type)
{
case 'array':
if (@array_key_exists('json', $arg)) {
if (@array_key_exists('json', $arg) && json_decode($arg['json']) !== NULL) {
$type = 'json';
$arg = $arg['json'];
break;
Expand All @@ -98,35 +99,37 @@ public static function validateData($args, $methodInfo)
$arg = @$arg[0];
} else {
$item_type = self::transformValueType(@gettype($arg[0]));
if($item_type == 'string' && preg_match("/^[\s0-9,]+$/", $arg[0])) { //is comma separated integer string
$item_type = 'int';
}
$type = 'array('.$item_type.')';
}
}
break;

break;
}
if ($validType !== $type)
{
if (substr($validType, 0, 4) === 'enum')
{
if ($arg === 'enum' || !preg_match("@".preg_quote($arg)."@", $validType))
{
$result['_invalid'][] = 'Invalid enum data param "'.$name.'" value ('.$arg.'): valid values "'.$validType.'"';
} else {
$result['_valid'][$name] = $arg;
}
} elseif ($type === 'array' && substr($validType, 0, 5) === 'array' ||
$type === 'string' && $validType === 'text')
{
$result['_valid'][$name] = $arg;
} elseif ($type === 'json' && substr($validType, 0, 5) === 'array')
{
$result['_valid'][$name] = $arg;
} else {
$result['_invalid'][] = RequestValidator::invalidParamType($name, $arg, $type, $validType);
}
} else {
$result['_valid'][$name] = $arg;
}

if ($validType !== $type) {
if (substr($validType, 0, 4) === 'enum') {
if ($arg === 'enum' || !preg_match("@" . preg_quote($arg) . "@", $validType)) {
$result['_invalid'][] = 'Invalid enum data param "' . $name . '" value (' . $arg . '): valid values "' . $validType . '"';
} else {
$result['_valid'][$name] = $arg;
}
} elseif ($type === 'array' && substr($validType, 0, 5) === 'array' ||
$type === 'string' && $validType === 'text'
) {
$result['_valid'][$name] = $arg;
} elseif ($type === 'json' && substr($validType, 0, 5) === 'array') {
$result['_valid'][$name] = $arg;
} elseif ($type === 'json' && substr($validType, 0, 6) === 'string') {
$result['_valid'][$name] = $arg;
} else {
$result['_invalid'][] = RequestValidator::invalidParamType($name, $arg, $type, $validType);
}
} else {
$result['_valid'][$name] = $arg;
}
} else {
$result['_invalid'][] = RequestValidator::invalidParam($name, $arg, gettype($arg));
}
Expand All @@ -144,4 +147,4 @@ public static function invalidParamType($name, $value, $type, $validType)
{
return 'Invalid data param type "'.$name.'" ('.(is_array($value) ? implode(', ', $value) : $value).': '.$type.'): required type "'.$validType.'"';
}
}
}
2 changes: 1 addition & 1 deletion src/Etsy/methods.json
Original file line number Diff line number Diff line change
Expand Up @@ -4293,4 +4293,4 @@
"visibility": "public",
"http_method": "GET"
}
}
}
65 changes: 63 additions & 2 deletions tests/Etsy/RequestValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,8 @@ public function testDataValidArrayType()
$method = 'createListing';
$args = array(
'data' => array(
'tags' => array('any, tag, other')
'tags' => array('any, tag, other'), //array(string)
'image_ids' => array('1, 2, 3'), //array(int)
)
);

Expand Down Expand Up @@ -413,4 +414,64 @@ public function testDataInvalidImageFileType()
$this->assertCount(1, $result['_invalid']);
$this->assertRegExp($this->invalidTypeRegExp, $result['_invalid'][0]);
}
}

public function testDataValidStringJSONType()
{
// "uri": "/listings"
$method = 'updateInventory';
$args = array(
'params' => array(
'listing_id' => 123456
),
'data' => array(
'price_on_property' => [implode(',', array(1, 2))],
'products' => array(
'json' => json_encode(array(1, 2, 3))
)
)
);

$result = RequestValidator::validateParams($args, $this->methods[$method]);
$this->assertArrayNotHasKey('_invalid', $result, print_r(@$result['_invalid'], true));
}

public function testDataValidEmptyStringJSONType()
{
// "uri": "/listings"
$method = 'updateInventory';
$args = array(
'params' => array(
'listing_id' => 123456
),
'data' => array(
'products' => array(
'json' => json_encode(array())
)
)
);

$result = RequestValidator::validateParams($args, $this->methods[$method]);
$this->assertArrayNotHasKey('_invalid', $result, print_r(@$result['_invalid'], true));
}

public function testDataInvalidStringJSONType()
{
// "uri": "/listings"
$method = 'updateInventory';
$args = array(
'params' => array(
'listing_id' => 123456
),
'data' => array(
'products' => array(
'json' => 'some string'
)
)
);

$result = RequestValidator::validateParams($args, $this->methods[$method]);
$this->assertArrayHasKey('_invalid', $result, print_r(@$result['_invalid'], true));
$this->assertCount(1, $result['_invalid']);
$this->assertRegExp($this->invalidTypeRegExp, $result['_invalid'][0]);
}
}
Loading

0 comments on commit 72746c3

Please sign in to comment.