diff --git a/src/SDK/Language/ReactNative.php b/src/SDK/Language/ReactNative.php index 7778e8652..24eb2f7b6 100644 --- a/src/SDK/Language/ReactNative.php +++ b/src/SDK/Language/ReactNative.php @@ -128,7 +128,7 @@ public function getFiles(): array * @param array $nestedTypes * @return string */ - public function getTypeName(array $parameter, array $spec = []): string + public function getTypeName(array $parameter, array $method = []): string { if (isset($parameter['enumName'])) { return \ucfirst($parameter['enumName']); @@ -151,6 +151,22 @@ public function getTypeName(array $parameter, array $spec = []): string return 'string[]'; case self::TYPE_FILE: return '{name: string, type: string, size: number, uri: string}'; + case self::TYPE_OBJECT: + if (empty($method)) { + return $parameter['type']; + } + switch ($method['responseModel']) { + case 'user': + return "Partial"; + case 'document': + if ($method['method'] === 'post') { + return "Document extends Models.DefaultDocument ? Partial & Record : Partial & Omit"; + } + if ($method['method'] === 'patch' || $method['method'] === 'put') { + return "Document extends Models.DefaultDocument ? Partial & Record : Partial & Partial>"; + } + } + break; } return $parameter['type']; diff --git a/src/SDK/Language/Web.php b/src/SDK/Language/Web.php index 2890d294c..27147cd8a 100644 --- a/src/SDK/Language/Web.php +++ b/src/SDK/Language/Web.php @@ -177,6 +177,26 @@ public function getParamExample(array $param): string return $output; } + public function getReadOnlyProperties(array $parameter, string $responseModel, array $spec = []): array + { + $properties = []; + + if ( + !isset($spec['definitions'][$responseModel]['properties']) || + !is_array($spec['definitions'][$responseModel]['properties']) + ) { + return $properties; + } + + foreach ($spec['definitions'][$responseModel]['properties'] as $property) { + if (isset($property['readOnly']) && $property['readOnly']) { + $properties[] = $property['name']; + } + } + + return $properties; + } + public function getTypeName(array $parameter, array $method = []): string { if (isset($parameter['enumName'])) { @@ -224,7 +244,7 @@ public function getTypeName(array $parameter, array $method = []): string if ($method['method'] === 'post') { return "Document extends Models.DefaultDocument ? Partial & Record : Partial & Omit"; } - if ($method['method'] === 'patch') { + if ($method['method'] === 'patch' || $method['method'] === 'put') { return "Document extends Models.DefaultDocument ? Partial & Record : Partial & Partial>"; } } @@ -336,6 +356,9 @@ public function getFilters(): array new TwigFilter('getPropertyType', function ($value, $method = []) { return $this->getTypeName($value, $method); }), + new TwigFilter('getReadOnlyProperties', function ($value, $responseModel, $spec = []) { + return $this->getReadOnlyProperties($value, $responseModel, $spec); + }), new TwigFilter('getSubSchema', function (array $property, array $spec) { return $this->getSubSchema($property, $spec); }), diff --git a/templates/node/src/services/template.ts.twig b/templates/node/src/services/template.ts.twig index 3846f5853..0f5e5d473 100644 --- a/templates/node/src/services/template.ts.twig +++ b/templates/node/src/services/template.ts.twig @@ -99,6 +99,14 @@ export class {{ service.name | caseUcfirst }} { } {%~ endif %} {%~ endfor %} + {%~ for parameter in method.parameters.all %} + {%~ if parameter.type == 'object' %} + {%~ for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %} + delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }}; + {%~ endfor %} + {%~ endif %} + {%~ endfor %} + const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %}; const payload: Payload = {}; {%~ for parameter in method.parameters.query %} diff --git a/templates/react-native/src/services/template.ts.twig b/templates/react-native/src/services/template.ts.twig index 2fb840d67..c0d20985d 100644 --- a/templates/react-native/src/services/template.ts.twig +++ b/templates/react-native/src/services/template.ts.twig @@ -56,6 +56,13 @@ export class {{ service.name | caseUcfirst }} extends Service { throw new {{spec.title | caseUcfirst}}Exception('Missing required parameter: "{{ parameter.name | caseCamel | escapeKeyword }}"'); } +{% endif %} +{% endfor %} +{% for parameter in method.parameters.all %} +{% if parameter.type == 'object' %} +{% for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %} + delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }}; +{% endfor %} {% endif %} {% endfor %} const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %}; diff --git a/templates/web/src/services/template.ts.twig b/templates/web/src/services/template.ts.twig index e9f685a4c..fd7e1bdbb 100644 --- a/templates/web/src/services/template.ts.twig +++ b/templates/web/src/services/template.ts.twig @@ -100,6 +100,14 @@ export class {{ service.name | caseUcfirst }} { } {%~ endif %} {%~ endfor %} + {%~ for parameter in method.parameters.all %} + {%~ if parameter.type == 'object' %} + {%~ for attribute in parameter | getReadOnlyProperties(method.responseModel, spec) %} + delete {{ parameter.name | caseCamel | escapeKeyword }}?.{{ attribute }}; + {%~ endfor %} + {%~ endif %} + {%~ endfor %} + const apiPath = '{{ method.path }}'{% for parameter in method.parameters.path %}.replace('{{ '{' }}{{ parameter.name | caseCamel | escapeKeyword }}{{ '}' }}', {{ parameter.name | caseCamel | escapeKeyword }}){% endfor %}; const payload: Payload = {}; {%~ for parameter in method.parameters.query %}