diff --git a/src/Preparator/Error400BadTypesPreparator.php b/src/Preparator/Error400BadTypesPreparator.php index 81837a8..8092cfb 100644 --- a/src/Preparator/Error400BadTypesPreparator.php +++ b/src/Preparator/Error400BadTypesPreparator.php @@ -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; } @@ -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); + } } diff --git a/tests/Preparator/Error400BadTypesPreparatorTest.php b/tests/Preparator/Error400BadTypesPreparatorTest.php index 8c2a5ea..4bb20fb 100644 --- a/tests/Preparator/Error400BadTypesPreparatorTest.php +++ b/tests/Preparator/Error400BadTypesPreparatorTest.php @@ -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')), + ), + ], + ]; } }