diff --git a/api_generator/api_generator/generators/dart/dart_entities.py b/api_generator/api_generator/generators/dart/dart_entities.py index 7de84314c..a2763396c 100644 --- a/api_generator/api_generator/generators/dart/dart_entities.py +++ b/api_generator/api_generator/generators/dart/dart_entities.py @@ -175,44 +175,45 @@ class DartProperty(Property): def get_parse_strategy(self) -> str: prop_type = cast(DartPropertyType, self.property_type) prop_type_decl = prop_type.declaration() - required_cast = '' if self.optional or self.has_default else '!' - + required_cast = '' if self.optional and not self.has_default else '!' + fallback = f' fallback: {self.fallback_declaration},' if self.has_default else '' expr = 'Expr' if self.supports_expressions else '' - fallback = f', fallback: {self.fallback_declaration}' if self.supports_expressions and self.has_default else '' if isinstance(prop_type, Int): - return f"safeParseInt{expr}(json['{self.name}']{fallback}){required_cast}" + return f"safeParseInt{expr}(json['{self.name}'],{fallback}){required_cast}" elif isinstance(prop_type, Color): - return f"safeParseColor{expr}(json['{self.name}']{fallback}){required_cast}" + return f"safeParseColor{expr}(json['{self.name}'],{fallback}){required_cast}" elif isinstance(prop_type, Double): - return f"safeParseDouble{expr}(json['{self.name}']{fallback}){required_cast}" + return f"safeParseDouble{expr}(json['{self.name}'],{fallback}){required_cast}" elif isinstance(prop_type, (Bool, BoolInt)): - return f"safeParseBool{expr}(json['{self.name}']{fallback}){required_cast}" + return f"safeParseBool{expr}(json['{self.name}'],{fallback}){required_cast}" elif isinstance(prop_type, (String, StaticString)): - return f"safeParseStr{expr}(json['{self.name}']?.toString(){fallback}){required_cast}" + return f"safeParseStr{expr}(json['{self.name}']?.toString(),{fallback}){required_cast}" elif isinstance(prop_type, Dictionary): - return f"safeParseClass{expr}(json{fallback}){required_cast}" + return f"safeParseObj{expr}(json,{fallback}){required_cast}" elif isinstance(prop_type, RawArray): - return f"safeParseClass{expr}(json['{self.name}']{fallback}){required_cast}" + return f"safeParseObj{expr}(json['{self.name}'],{fallback}){required_cast}" elif isinstance(prop_type, Url): return f"safeParseUri{expr}(json['{self.name}']){required_cast}" + elif prop_type.is_string_enumeration(): + return f"safeParseStrEnum{expr}(json['{self.name}'], parse: {prop_type_decl}.fromJson,{fallback}){required_cast}" elif prop_type.is_class(): - return f"safeParseClass{expr}({prop_type_decl}.fromJson(json['{self.name}']){fallback}){required_cast}" + return f"safeParseObj{expr}({prop_type_decl}.fromJson(json['{self.name}']),{fallback}){required_cast}" elif prop_type.is_list(): inner_item_type = prop_type.get_list_inner_class() if inner_item_type.is_class(): if inner_item_type.is_string_enumeration(): - return f"safeParseClass{expr}((json['{self.name}'] as List{'?' if self.optional or self.has_default else ''})" \ + return f"safeParseObj{expr}((json['{self.name}'] as List{'?' if self.optional or self.has_default else ''})" \ f"{'?' if self.optional or self.has_default else ''}.map((s) => {prop_type_decl[5:-1]}" \ - f".fromJson(s)!).toList(){fallback}){'' if self.optional or self.has_default else '!'}" + f".fromJson(s)!).toList(),{fallback}){'' if self.optional or self.has_default else '!'}" else: - return f"safeParseClass{expr}((json['{self.name}'] as List{'?' if self.optional or self.has_default else ''})" \ + return f"safeParseObj{expr}((json['{self.name}'] as List{'?' if self.optional or self.has_default else ''})" \ f"{'?' if self.optional or self.has_default else ''}.map((j) => {prop_type_decl[5:-1]}." \ - f"fromJson(j as Map )!{fallback}).toList()){'' if self.optional or self.has_default else '!'}" + f"fromJson(j as Map )!,{fallback}).toList()){'' if self.optional or self.has_default else '!'}" else: - return f"safeParseClass{expr}((json['{self.name}'] as List{'?' if self.optional or self.has_default else ''})" \ + return f"safeParseObj{expr}((json['{self.name}'] as List{'?' if self.optional or self.has_default else ''})" \ f"{'?' if self.optional or self.has_default else ''}.map((v) => (v as {prop_type_decl[5:-1]}))" \ - f".toList(){fallback}){'' if self.optional or self.has_default else '!'}" + f".toList(),{fallback}){'' if self.optional or self.has_default else '!'}" @property def fallback_declaration(self) -> str: diff --git a/api_generator/api_generator/generators/dart/generator.py b/api_generator/api_generator/generators/dart/generator.py index 7bff61421..c8c219a2e 100644 --- a/api_generator/api_generator/generators/dart/generator.py +++ b/api_generator/api_generator/generators/dart/generator.py @@ -128,7 +128,7 @@ class DartGenerator(Generator): result += f' return {full_name}(' for prop in entity.instance_properties: decode_strategy = prop.get_parse_strategy() - result += f" {utils.lower_camel_case(prop.name)}: {prop.add_default_value_to(decode_strategy, in_constructor=False)}," + result += f" {utils.lower_camel_case(prop.name)}: {decode_strategy}," result += ' );' result += ' }' else: diff --git a/api_generator/tests/references/dart/entity_with_array.dart b/api_generator/tests/references/dart/entity_with_array.dart index 77fa1ae7e..f27eec000 100644 --- a/api_generator/tests/references/dart/entity_with_array.dart +++ b/api_generator/tests/references/dart/entity_with_array.dart @@ -24,7 +24,7 @@ class EntityWithArray with EquatableMixin { return null; } return EntityWithArray( - array: safeParseClass((json['array'] as List).map((j) => Entity.fromJson(j as Map )!).toList())!, + array: safeParseObj((json['array'] as List).map((j) => Entity.fromJson(j as Map )!,).toList())!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_array_of_enums.dart b/api_generator/tests/references/dart/entity_with_array_of_enums.dart index bfe5434a3..12ed42ad1 100644 --- a/api_generator/tests/references/dart/entity_with_array_of_enums.dart +++ b/api_generator/tests/references/dart/entity_with_array_of_enums.dart @@ -23,7 +23,7 @@ class EntityWithArrayOfEnums with EquatableMixin { return null; } return EntityWithArrayOfEnums( - items: safeParseClass((json['items'] as List).map((s) => EntityWithArrayOfEnumsItem.fromJson(s)!).toList())!, + items: safeParseObj((json['items'] as List).map((s) => EntityWithArrayOfEnumsItem.fromJson(s)!).toList(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_array_of_expressions.dart b/api_generator/tests/references/dart/entity_with_array_of_expressions.dart index f595002c8..7de9d1935 100644 --- a/api_generator/tests/references/dart/entity_with_array_of_expressions.dart +++ b/api_generator/tests/references/dart/entity_with_array_of_expressions.dart @@ -23,7 +23,7 @@ class EntityWithArrayOfExpressions with EquatableMixin { return null; } return EntityWithArrayOfExpressions( - items: safeParseClassExpr((json['items'] as List).map((v) => (v as String)).toList())!, + items: safeParseObjExpr((json['items'] as List).map((v) => (v as String)).toList(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_array_of_nested_items.dart b/api_generator/tests/references/dart/entity_with_array_of_nested_items.dart index 9fbdabed0..6779a6eec 100644 --- a/api_generator/tests/references/dart/entity_with_array_of_nested_items.dart +++ b/api_generator/tests/references/dart/entity_with_array_of_nested_items.dart @@ -24,7 +24,7 @@ class EntityWithArrayOfNestedItems with EquatableMixin { return null; } return EntityWithArrayOfNestedItems( - items: safeParseClass((json['items'] as List).map((j) => EntityWithArrayOfNestedItemsItem.fromJson(j as Map )!).toList())!, + items: safeParseObj((json['items'] as List).map((j) => EntityWithArrayOfNestedItemsItem.fromJson(j as Map )!,).toList())!, ); } } @@ -51,8 +51,8 @@ class EntityWithArrayOfNestedItemsItem with EquatableMixin { return null; } return EntityWithArrayOfNestedItemsItem( - entity: safeParseClass(Entity.fromJson(json['entity']))!, - property: safeParseStrExpr(json['property']?.toString())!, + entity: safeParseObj(Entity.fromJson(json['entity']),)!, + property: safeParseStrExpr(json['property']?.toString(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_array_with_transform.dart b/api_generator/tests/references/dart/entity_with_array_with_transform.dart index 889f4a8b4..5ed4c3637 100644 --- a/api_generator/tests/references/dart/entity_with_array_with_transform.dart +++ b/api_generator/tests/references/dart/entity_with_array_with_transform.dart @@ -23,7 +23,7 @@ class EntityWithArrayWithTransform with EquatableMixin { return null; } return EntityWithArrayWithTransform( - array: safeParseClassExpr((json['array'] as List).map((v) => (v as int)).toList())!, + array: safeParseObjExpr((json['array'] as List).map((v) => (v as int)).toList(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_complex_property.dart b/api_generator/tests/references/dart/entity_with_complex_property.dart index 5f1bf11de..15c48211e 100644 --- a/api_generator/tests/references/dart/entity_with_complex_property.dart +++ b/api_generator/tests/references/dart/entity_with_complex_property.dart @@ -23,7 +23,7 @@ class EntityWithComplexProperty with EquatableMixin { return null; } return EntityWithComplexProperty( - property: safeParseClass(EntityWithComplexPropertyProperty.fromJson(json['property']))!, + property: safeParseObj(EntityWithComplexPropertyProperty.fromJson(json['property']),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_complex_property_with_default_value.dart b/api_generator/tests/references/dart/entity_with_complex_property_with_default_value.dart index 21dd5df66..a0a68100a 100644 --- a/api_generator/tests/references/dart/entity_with_complex_property_with_default_value.dart +++ b/api_generator/tests/references/dart/entity_with_complex_property_with_default_value.dart @@ -23,7 +23,7 @@ class EntityWithComplexPropertyWithDefaultValue with EquatableMixin { return null; } return EntityWithComplexPropertyWithDefaultValue( - property: safeParseClass(EntityWithComplexPropertyWithDefaultValueProperty.fromJson(json['property'])) ?? const EntityWithComplexPropertyWithDefaultValueProperty(value: Expression.value("Default text"),), + property: safeParseObj(EntityWithComplexPropertyWithDefaultValueProperty.fromJson(json['property']), fallback: const EntityWithComplexPropertyWithDefaultValueProperty(value: Expression.value("Default text"),),)!, ); } } @@ -46,7 +46,7 @@ class EntityWithComplexPropertyWithDefaultValueProperty with EquatableMixin { return null; } return EntityWithComplexPropertyWithDefaultValueProperty( - value: safeParseStrExpr(json['value']?.toString())!, + value: safeParseStrExpr(json['value']?.toString(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_entity_property.dart b/api_generator/tests/references/dart/entity_with_entity_property.dart index eb2379670..c2555b95c 100644 --- a/api_generator/tests/references/dart/entity_with_entity_property.dart +++ b/api_generator/tests/references/dart/entity_with_entity_property.dart @@ -25,7 +25,7 @@ class EntityWithEntityProperty with EquatableMixin { return null; } return EntityWithEntityProperty( - entity: safeParseClass(Entity.fromJson(json['entity'])) ?? const Entity(const EntityWithStringEnumProperty(property: Expression.value(EntityWithStringEnumPropertyProperty.second),)), + entity: safeParseObj(Entity.fromJson(json['entity']), fallback: const Entity(const EntityWithStringEnumProperty(property: Expression.value(EntityWithStringEnumPropertyProperty.second),)),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_json_property.dart b/api_generator/tests/references/dart/entity_with_json_property.dart index ea20d4849..ab8afa22f 100644 --- a/api_generator/tests/references/dart/entity_with_json_property.dart +++ b/api_generator/tests/references/dart/entity_with_json_property.dart @@ -23,7 +23,7 @@ class EntityWithJsonProperty with EquatableMixin { return null; } return EntityWithJsonProperty( - jsonProperty: safeParseClass(json) ?? None, + jsonProperty: safeParseObj(json, fallback: None,)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_optional_complex_property.dart b/api_generator/tests/references/dart/entity_with_optional_complex_property.dart index c55511655..506e21201 100644 --- a/api_generator/tests/references/dart/entity_with_optional_complex_property.dart +++ b/api_generator/tests/references/dart/entity_with_optional_complex_property.dart @@ -23,7 +23,7 @@ class EntityWithOptionalComplexProperty with EquatableMixin { return null; } return EntityWithOptionalComplexProperty( - property: safeParseClass(EntityWithOptionalComplexPropertyProperty.fromJson(json['property'])), + property: safeParseObj(EntityWithOptionalComplexPropertyProperty.fromJson(json['property']),), ); } } diff --git a/api_generator/tests/references/dart/entity_with_optional_property.dart b/api_generator/tests/references/dart/entity_with_optional_property.dart index 7cfc9e784..189d199b7 100644 --- a/api_generator/tests/references/dart/entity_with_optional_property.dart +++ b/api_generator/tests/references/dart/entity_with_optional_property.dart @@ -23,7 +23,7 @@ class EntityWithOptionalProperty with EquatableMixin { return null; } return EntityWithOptionalProperty( - property: safeParseStrExpr(json['property']?.toString()), + property: safeParseStrExpr(json['property']?.toString(),), ); } } diff --git a/api_generator/tests/references/dart/entity_with_optional_string_enum_property.dart b/api_generator/tests/references/dart/entity_with_optional_string_enum_property.dart index cbfce25cd..ecb0f4667 100644 --- a/api_generator/tests/references/dart/entity_with_optional_string_enum_property.dart +++ b/api_generator/tests/references/dart/entity_with_optional_string_enum_property.dart @@ -23,7 +23,7 @@ class EntityWithOptionalStringEnumProperty with EquatableMixin { return null; } return EntityWithOptionalStringEnumProperty( - property: safeParseClassExpr(EntityWithOptionalStringEnumPropertyProperty.fromJson(json['property'])), + property: safeParseStrEnumExpr(json['property'], parse: EntityWithOptionalStringEnumPropertyProperty.fromJson,), ); } } diff --git a/api_generator/tests/references/dart/entity_with_property_with_default_value.dart b/api_generator/tests/references/dart/entity_with_property_with_default_value.dart index 23dac0235..d7228fe94 100644 --- a/api_generator/tests/references/dart/entity_with_property_with_default_value.dart +++ b/api_generator/tests/references/dart/entity_with_property_with_default_value.dart @@ -31,9 +31,9 @@ class EntityWithPropertyWithDefaultValue with EquatableMixin { return null; } return EntityWithPropertyWithDefaultValue( - iNum: safeParseIntExpr(json['iNum'], fallback: 0) ?? const Expression.value(0), - nested: safeParseClass(EntityWithPropertyWithDefaultValueNested.fromJson(json['nested'])), - url: safeParseUriExpr(json['url']) ?? const Expression.value(const Uri.parse("https://yandex.ru")), + iNum: safeParseIntExpr(json['iNum'], fallback: 0,)!, + nested: safeParseObj(EntityWithPropertyWithDefaultValueNested.fromJson(json['nested']),), + url: safeParseUriExpr(json['url'])!, ); } } @@ -64,9 +64,9 @@ class EntityWithPropertyWithDefaultValueNested with EquatableMixin { return null; } return EntityWithPropertyWithDefaultValueNested( - iNum: safeParseIntExpr(json['iNum'], fallback: 0) ?? const Expression.value(0), - nonOptional: safeParseStrExpr(json['non_optional']?.toString())!, - url: safeParseUriExpr(json['url']) ?? const Expression.value(const Uri.parse("https://yandex.ru")), + iNum: safeParseIntExpr(json['iNum'], fallback: 0,)!, + nonOptional: safeParseStrExpr(json['non_optional']?.toString(),)!, + url: safeParseUriExpr(json['url'])!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_raw_array.dart b/api_generator/tests/references/dart/entity_with_raw_array.dart index 1c385f18e..b37a73cd6 100644 --- a/api_generator/tests/references/dart/entity_with_raw_array.dart +++ b/api_generator/tests/references/dart/entity_with_raw_array.dart @@ -23,7 +23,7 @@ class EntityWithRawArray with EquatableMixin { return null; } return EntityWithRawArray( - array: safeParseClassExpr(json['array'])!, + array: safeParseObjExpr(json['array'],)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_required_property.dart b/api_generator/tests/references/dart/entity_with_required_property.dart index 5dfaf8e47..11d8cba00 100644 --- a/api_generator/tests/references/dart/entity_with_required_property.dart +++ b/api_generator/tests/references/dart/entity_with_required_property.dart @@ -23,7 +23,7 @@ class EntityWithRequiredProperty with EquatableMixin { return null; } return EntityWithRequiredProperty( - property: safeParseStrExpr(json['property']?.toString())!, + property: safeParseStrExpr(json['property']?.toString(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_simple_properties.dart b/api_generator/tests/references/dart/entity_with_simple_properties.dart index d5558c3bd..a0a868b3f 100644 --- a/api_generator/tests/references/dart/entity_with_simple_properties.dart +++ b/api_generator/tests/references/dart/entity_with_simple_properties.dart @@ -55,14 +55,14 @@ class EntityWithSimpleProperties with EquatableMixin { return null; } return EntityWithSimpleProperties( - boolean: safeParseBoolExpr(json['boolean']), - booleanInt: safeParseBoolExpr(json['boolean_int']), - color: safeParseColorExpr(json['color']), - dNum: safeParseDoubleExpr(json['dNum']), - id: safeParseInt(json['id']) ?? 0, - integer: safeParseIntExpr(json['integer'], fallback: 0) ?? const Expression.value(0), - positiveInteger: safeParseIntExpr(json['positive_integer']), - string: safeParseStrExpr(json['string']?.toString()), + boolean: safeParseBoolExpr(json['boolean'],), + booleanInt: safeParseBoolExpr(json['boolean_int'],), + color: safeParseColorExpr(json['color'],), + dNum: safeParseDoubleExpr(json['dNum'],), + id: safeParseInt(json['id'], fallback: 0,)!, + integer: safeParseIntExpr(json['integer'], fallback: 0,)!, + positiveInteger: safeParseIntExpr(json['positive_integer'],), + string: safeParseStrExpr(json['string']?.toString(),), url: safeParseUriExpr(json['url']), ); } diff --git a/api_generator/tests/references/dart/entity_with_string_array_property.dart b/api_generator/tests/references/dart/entity_with_string_array_property.dart index daedb0490..6dd4f4124 100644 --- a/api_generator/tests/references/dart/entity_with_string_array_property.dart +++ b/api_generator/tests/references/dart/entity_with_string_array_property.dart @@ -23,7 +23,7 @@ class EntityWithStringArrayProperty with EquatableMixin { return null; } return EntityWithStringArrayProperty( - array: safeParseClassExpr((json['array'] as List).map((v) => (v as String)).toList())!, + array: safeParseObjExpr((json['array'] as List).map((v) => (v as String)).toList(),)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_string_enum_property.dart b/api_generator/tests/references/dart/entity_with_string_enum_property.dart index 9ffa59997..b0fc7cfcf 100644 --- a/api_generator/tests/references/dart/entity_with_string_enum_property.dart +++ b/api_generator/tests/references/dart/entity_with_string_enum_property.dart @@ -23,7 +23,7 @@ class EntityWithStringEnumProperty with EquatableMixin { return null; } return EntityWithStringEnumProperty( - property: safeParseClassExpr(EntityWithStringEnumPropertyProperty.fromJson(json['property']))!, + property: safeParseStrEnumExpr(json['property'], parse: EntityWithStringEnumPropertyProperty.fromJson,)!, ); } } diff --git a/api_generator/tests/references/dart/entity_with_string_enum_property_with_default_value.dart b/api_generator/tests/references/dart/entity_with_string_enum_property_with_default_value.dart index c8e394f11..2fd45af10 100644 --- a/api_generator/tests/references/dart/entity_with_string_enum_property_with_default_value.dart +++ b/api_generator/tests/references/dart/entity_with_string_enum_property_with_default_value.dart @@ -23,7 +23,7 @@ class EntityWithStringEnumPropertyWithDefaultValue with EquatableMixin { return null; } return EntityWithStringEnumPropertyWithDefaultValue( - value: safeParseClassExpr(EntityWithStringEnumPropertyWithDefaultValueValue.fromJson(json['value']), fallback: EntityWithStringEnumPropertyWithDefaultValueValue.second) ?? const Expression.value(EntityWithStringEnumPropertyWithDefaultValueValue.second), + value: safeParseStrEnumExpr(json['value'], parse: EntityWithStringEnumPropertyWithDefaultValueValue.fromJson, fallback: EntityWithStringEnumPropertyWithDefaultValueValue.second,)!, ); } }