From bc38069cb58b1e0f91d5ead935c7fe5ed64baee0 Mon Sep 17 00:00:00 2001 From: Denis Karmyshakov Date: Wed, 17 Oct 2018 17:52:40 +0300 Subject: [PATCH] nullable for all optional fields --- Java/blocks/class/fields-converters.twig | 18 +++++++++--------- Java/blocks/class/fields-getters-setters.twig | 8 ++++---- Java/blocks/class/fields-read-object.twig | 6 +++--- Java/blocks/class/fields-validate.twig | 8 ++++---- Java/blocks/class/fields-write-object.twig | 2 +- Java/blocks/class/fields.twig | 4 ++-- Java/blocks/class/init-parameters-fields.twig | 2 +- Java/blocks/class/type-parameters.twig | 2 +- 8 files changed, 25 insertions(+), 25 deletions(-) diff --git a/Java/blocks/class/fields-converters.twig b/Java/blocks/class/fields-converters.twig index 8395936..2416fb0 100644 --- a/Java/blocks/class/fields-converters.twig +++ b/Java/blocks/class/fields-converters.twig @@ -11,7 +11,7 @@ @TypeConverter @Nullable - public static String serialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { + public static String serialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final {{ utils.formatValueType(field.type.type, true, true) }} value) { if (value == null) { return null; } @@ -24,12 +24,12 @@ @TypeConverter @Nullable - public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true, true) }} deserialize{{ field.type.type.itemsType.baseTypeName }}List(@Nullable final String value) { if (value == null) { return null; } try { - return LoganSquare.parseList(value, {{ utils.formatValueType(field.type.type.itemsType, true) }}.class); + return LoganSquare.parseList(value, {{ utils.formatValueType(field.type.type.itemsType, true, true) }}.class); } catch (final IOException exception) { return null; } @@ -40,7 +40,7 @@ @TypeConverter @Nullable - public static String serialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { + public static String serialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final {{ utils.formatValueType(field.type.type, true, true) }} value) { if (value == null) { return null; } @@ -53,12 +53,12 @@ @TypeConverter @Nullable - public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true, true) }} deserialize{{ field.type.type.valuesType.baseTypeName }}Map(@Nullable final String value) { if (value == null) { return null; } try { - return LoganSquare.parseMap(value, {{ utils.formatValueType(field.type.type.valuesType, true) }}.class); + return LoganSquare.parseMap(value, {{ utils.formatValueType(field.type.type.valuesType, true, true) }}.class); } catch (final IOException exception) { return null; } @@ -74,7 +74,7 @@ @TypeConverter @Nullable - public static String serialize{{ field.type.type.baseTypeName }}(@Nullable final {{ utils.formatValueType(field.type.type, true) }} value) { + public static String serialize{{ field.type.type.baseTypeName }}(@Nullable final {{ utils.formatValueType(field.type.type, true, true) }} value) { if (value == null) { return null; } @@ -87,12 +87,12 @@ @TypeConverter @Nullable - public static {{ utils.formatValueType(field.type.type, true) }} deserialize{{ field.type.type.baseTypeName }}(@Nullable final String value) { + public static {{ utils.formatValueType(field.type.type, true, true) }} deserialize{{ field.type.type.baseTypeName }}(@Nullable final String value) { if (value == null) { return null; } try { - return LoganSquare.parse(value, {{ utils.formatValueType(field.type.type, true) }}.class); + return LoganSquare.parse(value, {{ utils.formatValueType(field.type.type, true, true) }}.class); } catch (final IOException exception) { return null; } diff --git a/Java/blocks/class/fields-getters-setters.twig b/Java/blocks/class/fields-getters-setters.twig index 31e7b5c..c06e9ad 100644 --- a/Java/blocks/class/fields-getters-setters.twig +++ b/Java/blocks/class/fields-getters-setters.twig @@ -6,9 +6,9 @@ * {{ field.description }} */ {%- endif %} - {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} - public {{ utils.formatValueType(field.type.type, field.nullable) }} {% if (field.type.type.baseTypeName == "Bool") and (field.name matches "^is[A-Z,0-9].*") -%}{{ field.name }}{%- else -%}get{{ capitalize(field.name) }}{%- endif -%}() { - {%- if field.nullable %} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} + public {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {% if (field.type.type.baseTypeName == "Bool") and (field.name matches "^is[A-Z,0-9].*") -%}{{ field.name }}{%- else -%}get{{ capitalize(field.name) }}{%- endif -%}() { + {%- if field.nullable or field.optional %} if (this.{{ field.name }} == null) { return null; } @@ -26,7 +26,7 @@ * {{ field.description }} */ {%- endif %} - public void set{{ capitalize(field.name) }}({{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} final {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }}) { + public void set{{ capitalize(field.name) }}({{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }}) { this.{{ field.name }} = {{ field.name }}; } {%- endfor -%} diff --git a/Java/blocks/class/fields-read-object.twig b/Java/blocks/class/fields-read-object.twig index e0d994c..e003380 100644 --- a/Java/blocks/class/fields-read-object.twig +++ b/Java/blocks/class/fields-read-object.twig @@ -1,7 +1,7 @@ {%- import '../../utils.twig' as utils -%} {% for field in fields %} - {%- if field.nullable %} - this.{{ field.name }} = ({{ utils.formatValueType(field.type.type, field.nullable) }}) inputStream.readObject(); + {%- if field.nullable or field.optional %} + this.{{ field.name }} = ({{ utils.formatValueType(field.type.type, field.nullable, field.optional) }}) inputStream.readObject(); {%- elseif field.type.type.baseTypeName == "Int" %} this.{{ field.name }} = inputStream.readInt(); {%- elseif field.type.type.baseTypeName == "Long" %} @@ -13,6 +13,6 @@ {%- elseif field.type.type.baseTypeName == "Bool" %} this.{{ field.name }} = inputStream.readBoolean(); {%- else %} - this.{{ field.name }} = ({{ utils.formatValueType(field.type.type, field.nullable) }}) inputStream.readObject(); + this.{{ field.name }} = ({{ utils.formatValueType(field.type.type, field.nullable, field.optional) }}) inputStream.readObject(); {%- endif -%} {%- endfor -%} diff --git a/Java/blocks/class/fields-validate.twig b/Java/blocks/class/fields-validate.twig index 3f00681..7987aae 100644 --- a/Java/blocks/class/fields-validate.twig +++ b/Java/blocks/class/fields-validate.twig @@ -1,6 +1,6 @@ {%- import '../../utils.twig' as utils -%} {%- for field in fields %} - {%- if not (field.nullable) + {%- if not (field.nullable or field.optional) and field.type.type.baseTypeName != "Int" and field.type.type.baseTypeName != "Long" and field.type.type.baseTypeName != "Double" @@ -8,7 +8,7 @@ validateNotNull(this.{{ field.name }}); {%- endif %} {%- if field.type.type.baseTypeName == "Map" %} - {%- if field.nullable %} + {%- if field.nullable or field.optional %} if (this.{{ field.name }} != null) { validateCollection(this.{{ field.name }}.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); } @@ -16,7 +16,7 @@ validateCollection(this.{{ field.name }}.values(), CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); {%- endif %} {%- elseif field.type.type.baseTypeName == "Array" %} - {%- if field.nullable %} + {%- if field.nullable or field.optional %} if (this.{{ field.name }} != null) { validateCollection(this.{{ field.name }}, CollectionValidationRule.EXCEPTION_IF_ANY_INVALID); } @@ -32,7 +32,7 @@ and field.type.type.baseTypeName != "DateTime" and (field.type.values is empty) %} if (this.{{ field.name }} instanceof ApiModel) { - {%- if field.nullable %} + {%- if field.nullable or field.optional %} if (this.{{ field.name }} != null) { ((ApiModel) this.{{ field.name }}).validate(); } diff --git a/Java/blocks/class/fields-write-object.twig b/Java/blocks/class/fields-write-object.twig index 1810561..e1195de 100644 --- a/Java/blocks/class/fields-write-object.twig +++ b/Java/blocks/class/fields-write-object.twig @@ -1,5 +1,5 @@ {% for field in fields %} - {%- if field.nullable %} + {%- if field.nullable or field.optional %} outputStream.writeObject(this.{{ field.name }}); {%- elseif field.type.type.baseTypeName == "Int" %} outputStream.writeInt(this.{{ field.name }}); diff --git a/Java/blocks/class/fields.twig b/Java/blocks/class/fields.twig index 7a79cd1..5fdd289 100644 --- a/Java/blocks/class/fields.twig +++ b/Java/blocks/class/fields.twig @@ -7,7 +7,7 @@ * {{ field.description }} */ {%- endif %} - {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} {%- if (storageAttributes is not null) %} {%- if (field.type.storable) %} @TypeConverters({{ field.type.type.baseTypeName }}.class) @@ -17,5 +17,5 @@ {%- endif %} {%- endif %} @JsonField(name = "{{ field.jsonName }}") - private {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }}; + private {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }}; {%- endfor -%} diff --git a/Java/blocks/class/init-parameters-fields.twig b/Java/blocks/class/init-parameters-fields.twig index 977caae..64216fa 100644 --- a/Java/blocks/class/init-parameters-fields.twig +++ b/Java/blocks/class/init-parameters-fields.twig @@ -2,6 +2,6 @@ {%- if fields is not empty -%} {%- for field in fields %} - {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable) }} final {{ utils.formatValueType(field.type.type, field.nullable) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %} + {{ utils.writeNullCheckAnnotation(field.type.type.baseTypeName, field.nullable, field.optional) }} final {{ utils.formatValueType(field.type.type, field.nullable, field.optional) }} {{ field.name }} {%- if not (loop.last) %}, {% endif %} {%- endfor -%} {%- endif -%} diff --git a/Java/blocks/class/type-parameters.twig b/Java/blocks/class/type-parameters.twig index c1eefc9..c348725 100644 --- a/Java/blocks/class/type-parameters.twig +++ b/Java/blocks/class/type-parameters.twig @@ -2,7 +2,7 @@ {%- if typeParameters is not empty -%} < {%- for typeParameter in typeParameters %} -{{- utils.formatValueType(typeParameter, true) -}}{%- if not (loop.last) %}, {% endif %} +{{- utils.formatValueType(typeParameter, true, true) -}}{%- if not (loop.last) %}, {% endif %} {%- endfor -%} > {%- endif -%}