Skip to content
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

Fix dotnet model collisions #788

Merged
merged 1 commit into from
Mar 8, 2024
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
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