Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[update] nullable Parameter For OpenApi3 #739

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions src/Writing/OpenAPISpecWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?: '',
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/Fixtures/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ paths:
name: filters
description: 'The filters.'
example: consequatur
nullable: true
required: false
schema:
type: string
Expand All @@ -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
Expand Down Expand Up @@ -192,6 +194,7 @@ paths:
type: string
description: ''
example: consequatur
nullable: true
-
in: header
name: Custom-Header
Expand Down Expand Up @@ -283,6 +286,7 @@ paths:
schema:
type: array
description: Details.
nullable: true
example:
- first_name: 'John'
last_name: 'Doe'
Expand Down
6 changes: 6 additions & 0 deletions tests/Unit/OpenAPISpecWriterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down Expand Up @@ -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',
Expand All @@ -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' => [
Expand All @@ -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',
],
Expand Down
Loading