Skip to content

Commit 40387cc

Browse files
Merge pull request #2 from OpenClassrooms/OC-XXXXX_allow_header_deserialization
fix(validator): allow header deserialization
2 parents 58c52c9 + 3551339 commit 40387cc

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

src/PSR7/SpecFinder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ public function findHeaderSpecs(OperationAddress $addr): array
247247
}
248248

249249
$headerData = json_decode(json_encode($p->getSerializableData()), true);
250-
unset($headerData['in'], $headerData['name']);
250+
unset($headerData['name']);
251251
try {
252252
$headerSpecs[$p->name] = new HeaderSpec($headerData);
253253
} catch (TypeErrorException $e) {

src/PSR7/Validators/SerializedParameter.php

+30
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,10 @@ private function castToSchemaType($value, ?string $type)
170170
return $this->convertToSerializationStyle($value, $this->schema);
171171
}
172172

173+
if (($type === CebeType::OBJECT) && is_string($value)) {
174+
return $this->convertToSerializationStyle($value, $this->schema);
175+
}
176+
173177
return $value;
174178
}
175179

@@ -185,6 +189,9 @@ protected function convertToSerializationStyle($value, ?CebeSchema $schema)
185189
case 'path':
186190
return $this->convertToSerializationStyleForPath($value, $schema);
187191

192+
case 'header':
193+
return $this->convertToSerializationStyleForHeader($value, $schema);
194+
188195
default:
189196
return $this->convertToSerializationStyleForQuery($value, $schema);
190197
}
@@ -257,6 +264,29 @@ protected function convertToSerializationStyleForPath($value, ?CebeSchema $schem
257264
return $value;
258265
}
259266

267+
/**
268+
* @param mixed $value
269+
* @param CebeSchema|null $schema - optional schema of value to convert it in case of DeepObject serialisation
270+
*
271+
* @return mixed
272+
*/
273+
protected function convertToSerializationStyleForHeader($value, ?CebeSchema $schema)
274+
{
275+
$value = explode(',', $value);
276+
277+
if (! is_iterable($value)) {
278+
throw TypeMismatch::becauseTypeDoesNotMatch(['iterable'], $value);
279+
}
280+
281+
$array = [];
282+
foreach ($value as &$val) {
283+
$splitVal = explode('=', $val);
284+
$array[$splitVal[0]] = $this->castToSchemaType($splitVal[1], $schema->properties[$splitVal[0]]->type ?? null);
285+
}
286+
287+
return $array;
288+
}
289+
260290
/**
261291
* @param mixed $value
262292
* @param CebeSchema|null $schema - optional schema of value to convert it in case of DeepObject serialisation

0 commit comments

Comments
 (0)