Skip to content

Commit

Permalink
ReqParamTrait: if we allow json from docBlock, we must use classes in…
Browse files Browse the repository at this point in the history
…stead of assocs in schema struct, changed docBlock parsing
  • Loading branch information
handcode committed Jun 29, 2022
1 parent 52466d2 commit 07af4f0
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions traits/RequestParamActionTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,17 @@ private function generateJson($parameters, $actionId)
}
}

// assign additionalData from docBlock in paramStruct
foreach ($additionalData as $name => $value) {
// if value looks like json object or array, get struct from json string
if ((substr($value, 0, 1) === '[' && substr($value, -1) !== ']') || (substr($value, 0, 2) === '{"' && substr($value, -1, 2) === '"}') ) {
$value = trim($value);
if ( preg_match('#^(\{.+\})|(\[.+\])$#', $value)) {
$value = json_decode($value);
}
$paramStruct->$name = $value;
}

// set enum options
if (\is_array($enumData)) {
// if we want string, cast keys to string, otherwise we would get IDs as int
if ($paramStruct->type === 'string') {
Expand All @@ -181,15 +184,15 @@ private function generateJson($parameters, $actionId)

// ensure options is set...
if (!isset($paramStruct->options)) {
$paramStruct->options = [];
$paramStruct->options = new \stdClass();
}
// ... and add enum_titles, ensure strings
$paramStruct->options['enum_titles'] = array_map('strval', array_values($enumData));
$paramStruct->options->enum_titles = array_map('strval', array_values($enumData));
}

}

// add to required if param is not optional
// add to required list if param is not optional
if (!$parameter->isOptional()) {
$requiredFields[] = $parameterName;
// TODO: how to check other types?
Expand Down

0 comments on commit 07af4f0

Please sign in to comment.