diff --git a/src/Writing/OpenAPISpecWriter.php b/src/Writing/OpenAPISpecWriter.php index 509c01f0..0a347133 100644 --- a/src/Writing/OpenAPISpecWriter.php +++ b/src/Writing/OpenAPISpecWriter.php @@ -456,9 +456,11 @@ public function generateFieldData($field): array $field = new Parameter($field); } + $fieldData = []; + if ($field->type === 'file') { // See https://swagger.io/docs/specification/describing-request-body/file-upload/ - return [ + $fieldData = [ 'type' => 'string', 'format' => 'binary', 'description' => $field->description ?: '', @@ -498,10 +500,8 @@ public function generateFieldData($field): array } } } - - return $fieldData; } else if ($field->type === 'object') { - return [ + $fieldData = [ 'type' => 'object', 'description' => $field->description ?: '', 'example' => $field->example, @@ -510,17 +510,20 @@ public function generateFieldData($field): array })->all()), ]; } else { - $schema = [ + $fieldData = [ 'type' => static::normalizeTypeName($field->type), 'description' => $field->description ?: '', 'example' => $field->example, ]; if (!empty($field->enumValues)) { - $schema['enum'] = $field->enumValues; + $fieldData['enum'] = $field->enumValues; } + } - return $schema; + if (isset($field['required']) && !$field['required']) { + $fieldData['nullable'] = true; } + return $fieldData; } protected function operationId(OutputEndpointData $endpoint): string diff --git a/tests/Fixtures/openapi.yaml b/tests/Fixtures/openapi.yaml index 9ea3a135..4ffc86bd 100644 --- a/tests/Fixtures/openapi.yaml +++ b/tests/Fixtures/openapi.yaml @@ -120,6 +120,7 @@ paths: name: filters description: 'The filters.' example: consequatur + nullable: true required: false schema: type: string @@ -130,6 +131,7 @@ paths: name: url_encoded description: 'Used for testing that URL parameters will be URL-encoded where needed.' example: '+ []&=' + nullable: true required: false schema: type: string @@ -192,6 +194,7 @@ paths: type: string description: '' example: consequatur + nullable: true - in: header name: Custom-Header @@ -283,6 +286,7 @@ paths: schema: type: array description: Details. + nullable: true example: - first_name: 'John' last_name: 'Doe' diff --git a/tests/Unit/OpenAPISpecWriterTest.php b/tests/Unit/OpenAPISpecWriterTest.php index 41990675..d5a20b17 100644 --- a/tests/Unit/OpenAPISpecWriterTest.php +++ b/tests/Unit/OpenAPISpecWriterTest.php @@ -234,6 +234,7 @@ public function adds_query_parameters_correctly_as_parameters_on_operation_objec 'type' => 'string', 'description' => 'A query param', 'example' => 'hahoho', + 'nullable' => true, ], ], $results['paths']['/path1']['get']['parameters'][0]); } @@ -341,6 +342,7 @@ public function adds_body_parameters_correctly_as_requestBody_on_operation_objec 'description' => 'String param', 'example' => 'hahoho', 'type' => 'string', + 'nullable' => true, ], 'booleanParam' => [ 'description' => 'Boolean param', @@ -361,8 +363,10 @@ public function adds_body_parameters_correctly_as_requestBody_on_operation_objec 'description' => 'Object param field', 'example' => 119.0, 'type' => 'number', + 'nullable' => true, ], ], + 'nullable' => true, ], ], 'required' => [ @@ -384,11 +388,13 @@ public function adds_body_parameters_correctly_as_requestBody_on_operation_objec 'description' => 'File param', 'type' => 'string', 'format' => 'binary', + 'nullable' => true, ], 'numberArrayParam' => [ 'description' => 'Number array param', 'example' => [186.9], 'type' => 'array', + 'nullable' => true, 'items' => [ 'type' => 'number', ],