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

Lazy initialization of AdditionalProperties property #1552

Merged
merged 1 commit into from
Aug 15, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public async Task When_additionalProperties_schema_is_set_for_object_then_specia
//// Assert
Assert.Contains("[Newtonsoft.Json.JsonExtensionData]", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> AdditionalProperties", code);
Assert.Contains("get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }", code);
}

[Fact]
Expand Down Expand Up @@ -80,6 +81,7 @@ public async Task When_using_SystemTextJson_additionalProperties_schema_is_set_f
//// Assert
Assert.Contains("[System.Text.Json.Serialization.JsonExtensionData]", code);
Assert.Contains("public System.Collections.Generic.IDictionary<string, object> AdditionalProperties", code);
Assert.Contains("get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, object>()); }", code);
}

[Fact]
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@

{%- if GenerateAdditionalPropertiesProperty -%}

private System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}> _additionalProperties = new System.Collections.Generic.Dictionary<string, {{ AdditionalPropertiesType }}>();
private System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}>{% if GenerateNullableReferenceTypes %}?{% endif %} _additionalProperties;

{%- if UseSystemTextJson -%}
[System.Text.Json.Serialization.JsonExtensionData]
Expand All @@ -132,7 +132,7 @@
{%- endif -%}
public System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}> AdditionalProperties
{
get { return _additionalProperties; }
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, {{ AdditionalPropertiesType }}>()); }
{{PropertySetterAccessModifier}}set { _additionalProperties = value; }
}

Expand Down