Skip to content

Commit

Permalink
Merge pull request #31 from OpenClassrooms/fix/allow-integer-when-exp…
Browse files Browse the repository at this point in the history
…ecting-number

fix(types-check): allow integers in number type check
  • Loading branch information
sidux authored Feb 16, 2024
2 parents 8d2b024 + 021e4bd commit f41c73a
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/Preparator/Error400BadTypesPreparator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected function prepareForBodyFields(Body $body, array $parameters, Operation
/** @var Schema $schema */
foreach ($body->getSchema()->properties as $property => $schema) {
foreach (self::SCHEMA_TYPES as $type) {
if ($type === $schema->type) {
if ($this->isAllowedType($schema->type, $type)) {
continue;
}

Expand Down Expand Up @@ -133,4 +133,9 @@ private function getSchemaExample(string $type)

return $examples[$type];
}

private function isAllowedType(string $passedType, string $testedType): bool
{
return $passedType === $testedType || (self::NUMBER_TYPE === $passedType && self::INTEGER_TYPE === $testedType);
}
}
70 changes: 70 additions & 0 deletions tests/Preparator/Error400BadTypesPreparatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,5 +180,75 @@ public function getData(): iterable
),
],
];

yield 'For floating-point number body field' => [
Api::create()
->addOperation(
Operation::create('test', '/test')
->setMethod('GET')
->addRequestBody(
new Body(
new Schema([
'type' => 'object',
'properties' => [
'foo' => [
'type' => 'number',
'format' => 'double',
],
],
'required' => ['foo'],
]),
'application/json'
)
)->addExample(
OperationExample::create('foo')
->setBody(
BodyExample::create([
'foo' => 3.14,
])
)
)
),
[
new TestCase(
Error400BadTypesPreparator::getName() . ' - test - foo_body_field_type_string',
OperationExample::create('test')
->setPath('/test')
->setBodyContent([
'foo' => 'foo',
])
->setResponse(ResponseExample::create('400'))
),
new TestCase(
Error400BadTypesPreparator::getName() . ' - test - foo_body_field_type_boolean',
OperationExample::create('test')
->setPath('/test')
->setBodyContent([
'foo' => true,
])
->setResponse(ResponseExample::create('400')),
),
new TestCase(
Error400BadTypesPreparator::getName() . ' - test - foo_body_field_type_array',
OperationExample::create('test')
->setPath('/test')
->setBodyContent([
'foo' => ['foo', 'bar'],
])
->setResponse(ResponseExample::create('400')),
),
new TestCase(
Error400BadTypesPreparator::getName() . ' - test - foo_body_field_type_object',
OperationExample::create('test')
->setPath('/test')
->setBodyContent([
'foo' => [
'foo' => 'bar',
],
])
->setResponse(ResponseExample::create('400')),
),
],
];
}
}

0 comments on commit f41c73a

Please sign in to comment.