Skip to content

Commit

Permalink
Merge pull request #788 from appwrite/fix-dotnet-property-collisions
Browse files Browse the repository at this point in the history
Fix dotnet model collisions
  • Loading branch information
TorstenDittmann authored Mar 8, 2024
2 parents e61118f + 83c0071 commit ee68012
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
15 changes: 15 additions & 0 deletions src/SDK/Language/DotNet.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,15 @@ public function getIdentifierOverrides(): array
];
}

public function getPropertyOverrides(): array
{
return [
'provider' => [
'Provider' => 'MessagingProvider',
],
];
}

/**
* @param array $parameter
* @return string
Expand Down Expand Up @@ -428,6 +437,12 @@ public function getFilters(): array
new TwigFilter('caseEnumKey', function (string $value) {
return $this->toPascalCase($value);
}),
new TwigFilter('overrideProperty', function (string $property, string $class) {
if (isset($this->getPropertyOverrides()[$class][$property])) {
return $this->getPropertyOverrides()[$class][$property];
}
return $property;
}),
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ namespace {{ spec.title | caseUcfirst }}.Converters
{
public class ValueClassConverter : JsonConverter {

public override bool CanConvert(Type objectType)
public override bool CanConvert(System.Type objectType)
{
return typeof(IEnum).IsAssignableFrom(objectType);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer)
{
var value = (string)reader.Value;
var constructor = objectType.GetConstructor(new[] { typeof(string) });
Expand Down
8 changes: 4 additions & 4 deletions templates/dotnet/src/Appwrite/Models/Model.cs.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% macro sub_schema(property) %}{% if property.sub_schema %}{% if property.type == 'array' %}List<{{property.sub_schema | caseUcfirst | overrideIdentifier}}>{% else %}{{property.sub_schema | caseUcfirst | overrideIdentifier}}{% endif %}{% else %}{{property | typeName}}{% endif %}{% if not property.required %}?{% endif %}{% endmacro %}
{% macro property_name(definition, property) %}{{ property.name | caseUcfirst | removeDollarSign | escapeKeyword}}{% endmacro %}
{% macro property_name(definition, property) %}{{ property.name | caseUcfirst | removeDollarSign | escapeKeyword }}{% endmacro %}

using System;
using System.Linq;
Expand All @@ -14,7 +14,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
{
{%~ for property in definition.properties %}
[JsonProperty("{{ property.name }}")]
public {{ _self.sub_schema(property) }} {{ _self.property_name(definition, property) }} { get; private set; }
public {{ _self.sub_schema(property) }} {{ _self.property_name(definition, property) | overrideProperty(definition.name) }} { get; private set; }

{%~ endfor %}
{%~ if definition.additionalProperties %}
Expand All @@ -31,7 +31,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
{%~ endif %}
) {
{%~ for property in definition.properties %}
{{ _self.property_name(definition, property) }} = {{ property.name | caseCamel | escapeKeyword }};
{{ _self.property_name(definition, property) | overrideProperty(definition.name) }} = {{ property.name | caseCamel | escapeKeyword }};
{%~ endfor %}
{%~ if definition.additionalProperties %}
Data = data;
Expand All @@ -51,7 +51,7 @@ namespace {{ spec.title | caseUcfirst }}.Models
public Dictionary<string, object?> ToMap() => new Dictionary<string, object?>()
{
{%~ for property in definition.properties %}
{ "{{ property.name }}", {% if property.sub_schema %}{% if property.type == 'array' %}{{ _self.property_name(definition, property) }}.Select(it => it.ToMap()){% else %}{{ _self.property_name(definition, property) }}.ToMap(){% endif %}{% else %}{{ _self.property_name(definition, property) }}{% endif %}{{ ' }' }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}
{ "{{ property.name }}", {% if property.sub_schema %}{% if property.type == 'array' %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.Select(it => it.ToMap()){% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}.ToMap(){% endif %}{% else %}{{ _self.property_name(definition, property) | overrideProperty(definition.name) }}{% endif %}{{ ' }' }}{% if not loop.last or (loop.last and definition.additionalProperties) %},{% endif %}

{%~ endfor %}
{%~ if definition.additionalProperties %}
Expand Down

0 comments on commit ee68012

Please sign in to comment.