Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

namespace {{invokerPackage}};

use ArrayAccess;
use DateTimeInterface;
use DateTime;
use GuzzleHttp\Psr7\Utils;
Expand Down Expand Up @@ -316,20 +317,33 @@ class ObjectSerializer
}

/**
* Take value and turn it into a string suitable for inclusion in
* Take value and turn it into an array suitable for inclusion in
* the http body (form parameter). If it's a string, pass through unchanged
* If it's a datetime object, format it in ISO8601
*
* @param string|\SplFileObject $value the value of the form parameter
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
*
* @return string the form string
* @return array [key => value] of formdata
*/
public static function toFormValue(string|\SplFileObject $value): string
{
public static function toFormValue(
string $key,
string|bool|array|DateTime|ArrayAccess|\SplFileObject $value,
): array {
if ($value instanceof \SplFileObject) {
return $value->getRealPath();
return [$key => $value->getRealPath()];
} elseif (is_array($value) || $value instanceof ArrayAccess) {
$flattened = [];
$result = [];

self::flattenArray(json_decode(json_encode($value), true), $flattened);

foreach ($flattened as $k => $v) {
$result["{$key}{$k}"] = self::toString($v);
}

return $result;
} else {
return self::toString($value);
return [$key => self::toString($value)];
}
}

Expand Down Expand Up @@ -598,4 +612,58 @@ class ObjectSerializer

return $qs ? (string) substr($qs, 0, -1) : '';
}

/**
* Flattens an array of Model object and generates an array compatible
* with formdata - a single-level array where the keys use bracket
* notation to signify nested data.
*
* credit: https://github.com/FranBar1966/FlatPHP
*/
private static function flattenArray(
ArrayAccess|array $source,
array &$destination,
string $start = '',
) {
$opt = [
'prefix' => '[',
'suffix' => ']',
'suffix-end' => true,
'prefix-list' => '[',
'suffix-list' => ']',
'suffix-list-end' => true,
];

if (!is_array($source)) {
$source = (array) $source;
}

if (array_is_list($source)) {
$currentPrefix = $opt['prefix-list'];
$currentSuffix = $opt['suffix-list'];
$currentSuffixEnd = $opt['suffix-list-end'];
} else {
$currentPrefix = $opt['prefix'];
$currentSuffix = $opt['suffix'];
$currentSuffixEnd = $opt['suffix-end'];
}

$currentName = $start;

foreach ($source as $key => $val) {
$currentName .= $currentPrefix.$key;

if (is_array($val) && !empty($val)) {
$currentName .= "{$currentSuffix}";
self::flattenArray($val, $destination, $currentName);
} else {
if ($currentSuffixEnd) {
$currentName .= $currentSuffix;
}
$destination[$currentName] = self::toString($val);
}

$currentName = $start;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -759,13 +759,13 @@ use {{invokerPackage}}\ObjectSerializer;
$formParams['{{baseName}}'][] = $paramFile instanceof \Psr\Http\Message\StreamInterface
? $paramFile
: \GuzzleHttp\Psr7\Utils::tryFopen(
ObjectSerializer::toFormValue($paramFile),
ObjectSerializer::toFormValue('{{baseName}}', $paramFile)['{{baseName}}'],
'rb'
);
}
{{/isFile}}
{{^isFile}}
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('{{baseName}}', ${{paramName}}));
{{/isFile}}
}
{{/formParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace {{invokerPackage}};

use ArrayAccess;
use GuzzleHttp\Psr7\Utils;
use {{modelPackage}}\ModelInterface;

Expand Down Expand Up @@ -315,20 +316,31 @@ class ObjectSerializer
}

/**
* Take value and turn it into a string suitable for inclusion in
* Take value and turn it into an array suitable for inclusion in
* the http body (form parameter). If it's a string, pass through unchanged
* If it's a datetime object, format it in ISO8601
*
* @param string|\SplFileObject $value the value of the form parameter
* @param string|bool|array|DateTime|ArrayAccess|\SplFileObject $value the value of the form parameter
*
* @return string the form string
* @return array [key => value] of formdata
*/
public static function toFormValue($value)
public static function toFormValue(string $key, mixed $value)
{
if ($value instanceof \SplFileObject) {
return $value->getRealPath();
return [$key => $value->getRealPath()];
} elseif (is_array($value) || $value instanceof ArrayAccess) {
$flattened = [];
$result = [];

self::flattenArray(json_decode(json_encode($value), true), $flattened);

foreach ($flattened as $k => $v) {
$result["{$key}{$k}"] = self::toString($v);
}

return $result;
} else {
return self::toString($value);
return [$key => self::toString($value)];
}
}

Expand Down Expand Up @@ -605,4 +617,81 @@ class ObjectSerializer

return $qs ? (string) substr($qs, 0, -1) : '';
}

/**
* Flattens an array of Model object and generates an array compatible
* with formdata - a single-level array where the keys use bracket
* notation to signify nested data.
*
* @param \ArrayAccess|array $source
*
* credit: https://github.com/FranBar1966/FlatPHP
*/
private static function flattenArray(
mixed $source,
array &$destination,
string $start = '',
) {
$opt = [
'prefix' => '[',
'suffix' => ']',
'suffix-end' => true,
'prefix-list' => '[',
'suffix-list' => ']',
'suffix-list-end' => true,
];

if (!is_array($source)) {
$source = (array) $source;
}

/**
* array_is_list only in PHP >= 8.1
*
* credit: https://www.php.net/manual/en/function.array-is-list.php#127044
*/
if (!function_exists('array_is_list')) {
function array_is_list(array $array)
{
$i = -1;

foreach ($array as $k => $v) {
++$i;
if ($k !== $i) {
return false;
}
}

return true;
}
}

if (array_is_list($source)) {
$currentPrefix = $opt['prefix-list'];
$currentSuffix = $opt['suffix-list'];
$currentSuffixEnd = $opt['suffix-list-end'];
} else {
$currentPrefix = $opt['prefix'];
$currentSuffix = $opt['suffix'];
$currentSuffixEnd = $opt['suffix-end'];
}

$currentName = $start;

foreach ($source as $key => $val) {
$currentName .= $currentPrefix.$key;

if (is_array($val) && !empty($val)) {
$currentName .= "{$currentSuffix}";
self::flattenArray($val, $destination, $currentName);
} else {
if ($currentSuffixEnd) {
$currentName .= $currentSuffix;
}
$destination[$currentName] = self::toString($val);
}

$currentName = $start;
}
}
}
4 changes: 2 additions & 2 deletions modules/openapi-generator/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -677,13 +677,13 @@ use {{invokerPackage}}\ObjectSerializer;
$paramFiles = is_array(${{paramName}}) ? ${{paramName}} : [${{paramName}}];
foreach ($paramFiles as $paramFile) {
$formParams['{{baseName}}'][] = \GuzzleHttp\Psr7\Utils::tryFopen(
ObjectSerializer::toFormValue($paramFile),
ObjectSerializer::toFormValue('{{baseName}}', $paramFile)['{{baseName}}'],
'rb'
);
}
{{/isFile}}
{{^isFile}}
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('{{baseName}}', ${{paramName}}));
{{/isFile}}
}
{{/formParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,13 +593,13 @@ use function sprintf;
$paramFiles = is_array(${{paramName}}) ? ${{paramName}} : [${{paramName}}];
foreach ($paramFiles as $paramFile) {
$formParams['{{baseName}}'][] = \GuzzleHttp\Psr7\try_fopen(
ObjectSerializer::toFormValue($paramFile),
ObjectSerializer::toFormValue('{{baseName}}', $paramFile)['{{baseName}}'],
'rb'
);
}
{{/isFile}}
{{^isFile}}
$formParams['{{baseName}}'] = ObjectSerializer::toFormValue(${{paramName}});
$formParams = array_merge($formParams, ObjectSerializer::toFormValue('{{baseName}}', ${{paramName}}));
{{/isFile}}
}
{{/formParams}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,37 @@

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

import java.util.Properties;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatNoException;

import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

/**
* Test class for {@link GlobalSettings}
*
* @author Edoardo Patti
*/
public class GlobalSettingsTest {

private static final Object OBJECT = new Object();

@BeforeClass
public void setUp() {
((Logger) LoggerFactory.getLogger(GlobalSettings.class)).setLevel(Level.DEBUG);
Properties props = new Properties(2);
props.put("test1", OBJECT);
props.put(OBJECT, "test2");
System.getProperties().putAll(props);
}

@Test
public void testNonStringSystemProperties() {
assertThat(GlobalSettings.getProperty(OBJECT.toString())).isNotNull();
assertThat(GlobalSettings.getProperty("test1")).isNotNull();
assertThatNoException().isThrownBy(GlobalSettings::log);
}
@BeforeClass
public void setUp() {
((Logger) LoggerFactory.getLogger(GlobalSettings.class)).setLevel(Level.DEBUG);
Properties props = new Properties(2);
props.put("test1", 789);
props.put(345, "test2");
System.getProperties().putAll(props);
}

@Test
public void testNonStringSystemProperties() {
assertThat(GlobalSettings.getProperty("345")).isEqualTo("test2");
assertThat(GlobalSettings.getProperty("test1")).isEqualTo("789");
assertThatNoException().isThrownBy(GlobalSettings::log);
}

}
Loading
Loading