Skip to content

fix: private attributes not being accessible in dart #1115

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

Merged
merged 1 commit into from
Jul 21, 2025
Merged
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
22 changes: 21 additions & 1 deletion templates/dart/lib/src/models/model.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,30 @@ class {{ definition.name | caseUcfirst | overrideIdentifier }}<T> implements Mod
{{ definition.name | caseUcfirst | overrideIdentifier}}({% if definition.properties | length or definition.additionalProperties %}{{ '{' }}{% endif %}

{% for property in definition.properties %}
{% if property.name starts with '_' %}
{% if property.required %}required {% endif %}{{_self.sub_schema(property)}}{% if not property.required %}?{% endif %} {{ property.name | slice(1) | escapeKeyword }},
{% else %}
{% if property.required %}required {% endif %}this.{{ property.name | escapeKeyword }},
{% endif %}
{% endfor %}
{% if definition.additionalProperties %}
required this.data,
{% endif %}
{% if definition.properties | length or definition.additionalProperties %}{{ '}' }}{% endif %});
{% if definition.properties | length or definition.additionalProperties %}{{ '}' }}{% endif %}){% set hasUnderscoreProps = false %}{% for property in definition.properties %}{% if property.name starts with '_' %}{% set hasUnderscoreProps = true %}{% endif %}{% endfor %}{% if hasUnderscoreProps %} :
{% for property in definition.properties %}
{% if property.name starts with '_' %}
{{ property.name | escapeKeyword }} = {{ property.name | slice(1) | escapeKeyword }}{% if not loop.last %},{% endif %}

{% endif %}
{% endfor %}{% endif %};

factory {{ definition.name | caseUcfirst | overrideIdentifier}}.fromMap(Map<String, dynamic> map, [T Function(Map<String, dynamic>)? fromJson]) {
return {{ definition.name | caseUcfirst | overrideIdentifier }}(
{% for property in definition.properties %}
{% if property.name starts with '_' %}
{{ property.name | slice(1) | escapeKeyword }}:{{' '}}{% else %}
{{ property.name | escapeKeyword }}:{{' '}}
{% endif %}
{%- if property.sub_schema -%}
{%- if property.type == 'array' -%}
List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}<T>>.from(map['{{property.name | escapeDollarSign }}'].map((p) => {{property.sub_schema | caseUcfirst | overrideIdentifier}}.fromMap(p, fromJson)))
Expand Down Expand Up @@ -76,5 +89,12 @@ class {{ definition.name | caseUcfirst | overrideIdentifier }}<T> implements Mod
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}

// Public getters for private underscore fields
{% for property in definition.properties %}
{% if property.name starts with '_' %}
{{_self.sub_schema(property)}}{% if not property.required %}?{% endif %} get {{ property.name | slice(1) | escapeKeyword }} => {{ property.name | escapeKeyword }};
{% endif %}
{% endfor %}
}
8 changes: 8 additions & 0 deletions templates/dart/test/src/models/model_test.dart.twig
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,11 @@ void main() {
test('model', () {
final model = {{ definition.name | caseUcfirst | overrideIdentifier }}(
{% for property in definition.properties | filter(p => p.required) %}
{% if property.name starts with '_' %}
{{ property.name | slice(1) | escapeKeyword }}: {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}Preferences(data: {}){% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %},
{% else %}
{{ property.name | escapeKeyword }}: {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}Preferences(data: {}){% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %},
{% endif %}
{% endfor %}
{% if definition.additionalProperties %}
data: {},
Expand All @@ -23,7 +27,11 @@ void main() {
final result = {{ definition.name | caseUcfirst | overrideIdentifier }}.fromMap(map);

{% for property in definition.properties | filter(p => p.required) %}
{% if property.name starts with '_' %}
expect(result.{{ property.name | slice(1) | escapeKeyword }}{% if property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}.data{% endif %}, {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}{"data": {}}{% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %});
{% else %}
expect(result.{{ property.name | escapeKeyword }}{% if property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}.data{% endif %}, {% if property.type == 'array' %}[]{% elseif property.type == 'object' and (property.sub_schema == 'prefs' or property.sub_schema == 'preferences') %}{"data": {}}{% elseif property.type == 'object' %}{}{% elseif property.type == 'string' %}'{{property['x-example'] | escapeDollarSign}}'{% elseif property.type == 'boolean' %}true{% else %}{{property['x-example']}}{% endif %});
{% endif %}
{% endfor %}
});
});
Expand Down