Skip to content

Commit

Permalink
Don't snake/camel case keys on incoming input, if they are all uppercase
Browse files Browse the repository at this point in the history
  • Loading branch information
specialtactics committed Aug 5, 2019
1 parent 5fc54b6 commit f0fdc28
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ public static function camelCaseArrayKeys($array, $levels = null)
$value = &$array[$key];
unset($array[$key]);

// Transform key
$transformedKey = Str::camel($key);
// Transform key - but we probably don't want to transform all uppercase keys
if (strtoupper($key) !== $key) {
$transformedKey = Str::camel($key);
} else {
$transformedKey = $key;
}

// Recurse
if (is_array($value) && (is_null($levels) || --$levels > 0)) {
Expand Down Expand Up @@ -54,8 +58,12 @@ public static function snakeCaseArrayKeys(array $array, $levels = null)
$value = &$array[$key];
unset($array[$key]);

// Transform key
$transformedKey = Str::snake($key);
// Transform key - but we probably don't want to transform all uppercase keys
if (strtoupper($key) !== $key) {
$transformedKey = Str::snake($key);
} else {
$transformedKey = $key;
}

// Recurse
if (is_array($value) && (is_null($levels) || --$levels > 0)) {
Expand Down
37 changes: 37 additions & 0 deletions test/tests/Unit/SnakeCaseMiddlewareTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Specialtactics\L5Api\Tests\Unit;

use Dingo\Api\Http\Request;
use Mockery;
use Specialtactics\L5Api\Http\Middleware\SnakeCaseInputParameterKeys;
use Specialtactics\L5Api\Tests\BaseTestCase;

class SnakeCaseMiddlewareTest extends BaseTestCase
{
/**
* Test that we are not snake_casing keys which are all uppercase
*
* @test
*/
public function dontTransformAllUpercaseKeys()
{
// Setup request
$content = [
'keyShouldTransform' => true,
'AUD/USD' => '0.80',
'AUD' => '0.75'
];

$request = Request::create('/test', 'POST', [], [], [], ['CONTENT_TYPE' => 'application/json'], json_encode($content));

$snakeCaseMiddleware = new SnakeCaseInputParameterKeys;
$snakeCaseMiddleware->handle($request, function($request) {});

$input = $request->input();

$this->assertArrayHasKey('key_should_transform', $input);
$this->assertArrayHasKey('AUD/USD', $input);
$this->assertArrayHasKey('AUD', $input);
}
}

0 comments on commit f0fdc28

Please sign in to comment.