Skip to content

Commit

Permalink
Merge pull request #628 from appwrite/feat-nullable-required
Browse files Browse the repository at this point in the history
Nullable required
  • Loading branch information
lohanidamodar authored Apr 11, 2023
2 parents 53f9782 + 7003fa9 commit 2ac74d5
Show file tree
Hide file tree
Showing 13 changed files with 18 additions and 16 deletions.
2 changes: 2 additions & 0 deletions src/Spec/Swagger2.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ public function getMethods($service)
'class' => $parameter['x-class'] ?? null,
'description' => $parameter['description'] ?? '',
'required' => $parameter['required'] ?? false,
'nullable' => $parameter['x-nullable'] ?? false,
'default' => $parameter['default'] ?? null,
'example' => $parameter['x-example'] ?? null,
'isUploadID' => $parameter['x-upload-id'] ?? false,
Expand Down Expand Up @@ -245,6 +246,7 @@ public function getMethods($service)
$param['required'] = (in_array($key, $bodyRequired));
$param['example'] = $value['x-example'] ?? null;
$param['isUploadID'] = $value['x-upload-id'] ?? false;
$param['nullable'] = $value['x-nullable'] ?? false;
$param['array'] = [
'type' => $value['items']['type'] ?? '',
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class {{ service.name | caseUcfirst }} : Service {
activity: ComponentActivity,
{%~ endif %}
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required %}? = null{% endif %},
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
{%~ endfor %}
{%~ if method.responseModel | hasGenericType(spec) %}
nestedType: Class<T>,
Expand Down Expand Up @@ -198,7 +198,7 @@ class {{ service.name | caseUcfirst }} : Service {
activity: ComponentActivity,
{%~ endif %}
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required %}? = null{% endif %},
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
{%~ endfor %}
{%~ if 'multipart/form-data' in method.consumes %}
onProgress: ((UploadProgress) -> Unit)? = null
Expand Down
2 changes: 1 addition & 1 deletion templates/cli/lib/commands/command.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ const {{ service.name | caseLower }}{{ method.name | caseUcfirst }} = async ({ {
{% autoescape false %}
.description(`{{ method.description | replace({'`':'\''}) | replace({'\n':' '}) | replace({'\n \n':' '}) }}`)
{% for parameter in method.parameters.all %}
.{% if parameter.required %}requiredOption{% else %}option{% endif %}(`--{{ parameter.name | escapeKeyword }} {% if parameter.array.type|length > 0 %}[{% else %}<{% endif %}{{ parameter.name | escapeKeyword }}{% if parameter.array.type|length > 0 %}...{% endif %}{% if parameter.array.type|length > 0 %}]{% else %}>{% endif %}`, `{{ parameter.description | replace({'`':'\''}) | replace({'\n':' '}) | replace({'\n \n':' '}) }}`{% if parameter | typeName == 'boolean' %}, parseBool{% elseif parameter | typeName == 'number' %}, parseInteger{% endif %})
.{% if parameter.required and not parameter.nullable %}requiredOption{% else %}option{% endif %}(`--{{ parameter.name | escapeKeyword }} {% if parameter.array.type|length > 0 %}[{% else %}<{% endif %}{{ parameter.name | escapeKeyword }}{% if parameter.array.type|length > 0 %}...{% endif %}{% if parameter.array.type|length > 0 %}]{% else %}>{% endif %}`, `{{ parameter.description | replace({'`':'\''}) | replace({'\n':' '}) | replace({'\n \n':' '}) }}`{% if parameter | typeName == 'boolean' %}, parseBool{% elseif parameter | typeName == 'number' %}, parseInteger{% endif %})
{% endfor %}
{% if method.type == 'location' %}
.requiredOption(`--destination <path>`, `output file path.`)
Expand Down
2 changes: 1 addition & 1 deletion templates/dart/README.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies:
You can install packages from the command line:

```bash
pub get {{ language.params.packageName }}
dart pub add {{ language.params.packageName }}
```

{% if sdk.gettingStarted %}
Expand Down
2 changes: 1 addition & 1 deletion templates/dart/lib/services/service.dart.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
part of {{ language.params.packageName }};
{% macro parameter(parameter) %}{% if parameter.required %}required {{ parameter | typeName }}{% else %}{{ parameter | typeName }}?{% endif %} {{ parameter.name | caseCamel | overrideIdentifier }}{% endmacro %}
{% macro parameter(parameter) %}{% if parameter.required %}required {% endif %}{{ parameter | typeName }}{% if not parameter.required or parameter.nullable %}?{% endif %} {{ parameter.name | caseCamel | overrideIdentifier }}{% endmacro %}
{% macro method_parameters(parameters, consumes) %}
{% if parameters|length > 0 %}{{ '{' }}{% for parameter in parameters %}{{ _self.parameter(parameter) }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %}, Function(UploadProgress)? onProgress{% endif %}{{ '}' }}{% endif %}
{% endmacro %}
Expand Down
2 changes: 1 addition & 1 deletion templates/deno/src/services/service.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
* @throws {AppwriteException}
* @returns {Promise}
*/
async {{ method.name | caseCamel }}{% if generics %}<{{generics}}>{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: UploadProgress) => {}{% endif %}): Promise<{% if method.type == 'webAuth' %}Response{% elseif method.type == 'location' %}Response{% else %}{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% if generics_return %}<{{generics_return}}>{% endif %}{% else %}Response{% endif %}{% endif %}> {
async {{ method.name | caseCamel }}{% if generics %}<{{generics}}>{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required or parameter.nullable %}?{% endif %}: {{ parameter | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: UploadProgress) => {}{% endif %}): Promise<{% if method.type == 'webAuth' %}Response{% elseif method.type == 'location' %}Response{% else %}{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% if generics_return %}<{{generics_return}}>{% endif %}{% else %}Response{% endif %}{% endif %}> {
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/README.md.twig
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ dependencies:
You can install packages from the command line:

```bash
pub get {{ language.params.packageName }}
flutter pub add {{ language.params.packageName }}
```

{% if sdk.gettingStarted %}
Expand Down
2 changes: 1 addition & 1 deletion templates/flutter/lib/services/service.dart.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
part of {{ language.params.packageName }};
{% macro parameter(parameter) %}{% if parameter.required %}required {{ parameter | typeName }}{% else %}{{ parameter | typeName }}?{% endif %} {{ parameter.name | caseCamel | overrideIdentifier }}{% endmacro %}
{% macro parameter(parameter) %}{% if parameter.required %}required {% endif %}{{ parameter | typeName }}{% if not parameter.required or parameter.nullable %}?{% endif %} {{ parameter.name | caseCamel | overrideIdentifier }}{% endmacro %}
{% macro method_parameters(parameters, consumes) %}
{% if parameters|length > 0 %}{{ '{' }}{% for parameter in parameters %}{{ _self.parameter(parameter) }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in consumes %}, Function(UploadProgress)? onProgress{% endif %}{{ '}' }}{% endif %}
{% endmacro %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class {{ service.name | caseUcfirst }} : Service {
activity: ComponentActivity,
{%~ endif %}
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required %}? = null{% endif %},
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
{%~ endfor %}
{%~ if method.responseModel | hasGenericType(spec) %}
nestedType: Class<T>,
Expand Down Expand Up @@ -101,7 +101,7 @@ class {{ service.name | caseUcfirst }} : Service {
@Throws({{ spec.title | caseUcfirst }}Exception::class)
suspend fun {{ method.name | caseCamel }}(
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required %}? = null{% endif %},
{{ parameter.name | caseCamel }}: {{ parameter | typeName }}{%~ if not parameter.required or parameter.nullable %}? = null{% endif %},
{%~ endfor %}
{%~ if 'multipart/form-data' in method.consumes %}
onProgress: ((UploadProgress) -> Unit)? = null
Expand Down
4 changes: 2 additions & 2 deletions templates/node/index.d.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ declare module "{{ language.params.npmPackage|caseDash }}" {

{% for service in spec.services %}
export class {{ service.name | caseUcfirst }} extends Service {
constructor(client: Client{% for parameter in service.globalParams %}, {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName }}{% if not parameter.required %}|null{% endif %}{% endfor %});
constructor(client: Client{% for parameter in service.globalParams %}, {{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName }}{% if not parameter.required or parameter.nullable %}|null{% endif %}{% endfor %});

{% if service.globalParams | length %}
{% for parameter in service.globalParams %}
Expand Down Expand Up @@ -237,7 +237,7 @@ declare module "{{ language.params.npmPackage|caseDash }}" {
* @throws {{ '{' }}{{ spec.title | caseUcfirst}}Exception}
* @returns {Promise}
*/
{{ method.name | caseCamel }}{% if generics %}<{{generics}}>{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}): Promise<{% if method.type == 'location' %}Buffer{% else %}{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% if generics_return %}<{{generics_return}}>{% endif %}{% else %}string{% endif %}{% endif %}>;
{{ method.name | caseCamel }}{% if generics %}<{{generics}}>{% endif %}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required or parameter.nullable %}?{% endif %}: {{ parameter | typeName }}{% if not loop.last %}, {% endif %}{% endfor %}): Promise<{% if method.type == 'location' %}Buffer{% else %}{% if method.responseModel and method.responseModel != 'any' %}{% if not spec.definitions[method.responseModel].additionalProperties %}Models.{% endif %}{{method.responseModel | caseUcfirst}}{% if generics_return %}<{{generics_return}}>{% endif %}{% else %}string{% endif %}{% endif %}>;
{% endfor %}
}
{% endfor %}
Expand Down
2 changes: 1 addition & 1 deletion templates/php/base/params.twig
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$params = [];
{% if method.parameters.all | length %}
{% for parameter in method.parameters.all %}
{% if parameter.required %}
{% if parameter.required and not parameter.nullable %}
if (!isset(${{ parameter.name | caseCamel | escapeKeyword }})) {
throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"');
}
Expand Down
4 changes: 2 additions & 2 deletions templates/swift/Sources/Services/Service.swift.twig
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ open class {{ service.name | caseUcfirst }}: Service {
{%~ endif %}
open func {{ method.name | caseCamel }}{% if method.responseModel | hasGenericType(spec) %}<T>{% endif %}(
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes or method.responseModel | hasGenericType(spec) %},{% endif %}
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required or parameter.nullable %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes or method.responseModel | hasGenericType(spec) %},{% endif %}

{%~ endfor %}
{%~ if method.responseModel | hasGenericType(spec) %}
Expand Down Expand Up @@ -92,7 +92,7 @@ open class {{ service.name | caseUcfirst }}: Service {
{%~ endif %}
open func {{ method.name | caseCamel }}(
{%~ for parameter in method.parameters.all %}
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes %},{% endif %}
{{ parameter.name | caseCamel | escapeKeyword }}: {{ parameter | typeName | raw }}{% if not parameter.required or parameter.nullable %}? = nil{% endif %}{% if not loop.last or 'multipart/form-data' in method.consumes %},{% endif %}

{%~ endfor %}
{%~ if 'multipart/form-data' in method.consumes %}
Expand Down
2 changes: 1 addition & 1 deletion templates/web/src/services/template.ts.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class {{ service.name | caseUcfirst }} extends Service {
* @returns {% if method.type == 'webAuth' %}{void|string}{% elseif method.type == 'location' %}{URL}{% else %}{Promise}{% endif %}

*/
{% if method.type != 'location' and method.type != 'webAuth'%}async {% endif %}{{ method.name | caseCamel }}{{ method.responseModel | getGenerics(spec) | raw }}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required %}?{% endif %}: {{ parameter | getPropertyType(method) | raw }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: UploadProgress) => {}{% endif %}): {{ method | getReturn(spec) | raw }} {
{% if method.type != 'location' and method.type != 'webAuth'%}async {% endif %}{{ method.name | caseCamel }}{{ method.responseModel | getGenerics(spec) | raw }}({% for parameter in method.parameters.all %}{{ parameter.name | caseCamel | escapeKeyword }}{% if not parameter.required or parameter.nullable %}?{% endif %}: {{ parameter | getPropertyType(method) | raw }}{% if not loop.last %}, {% endif %}{% endfor %}{% if 'multipart/form-data' in method.consumes %}, onProgress = (progress: UploadProgress) => {}{% endif %}): {{ method | getReturn(spec) | raw }} {
{% for parameter in method.parameters.all %}
{% if parameter.required %}
if (typeof {{ parameter.name | caseCamel | escapeKeyword }} === 'undefined') {
Expand Down

0 comments on commit 2ac74d5

Please sign in to comment.