Skip to content

Commit

Permalink
Tolerate schema properties named AdditionalProperties
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Nov 21, 2023
1 parent 63ee615 commit c6e01d7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using NJsonSchema.Annotations;
using NJsonSchema.Generation;
using NJsonSchema.NewtonsoftJson.Generation;
using Xunit;

Expand Down Expand Up @@ -1890,6 +1889,35 @@ public async Task When_schema_has_AdditionProperties_schema_then_JsonExtensionDa

//// Assert
Assert.Contains("JsonExtensionData", output);
Assert.Contains("IDictionary<string, object> AdditionalProperties\n", output);
}


public class ClassWithAdditionalProperties
{
public string AdditionalProperties { get; set; }

[JsonExtensionData]
public IDictionary<string, object> ExtensionData { get; set; }
}

[Fact]
public async Task When_schema_has_AdditionProperties_schema_and_type_has_member_with_same_name()
{
//// Arrange
var schema = NewtonsoftJsonSchemaGenerator.FromType<ClassWithAdditionalProperties>(new NewtonsoftJsonSchemaGeneratorSettings { SchemaType = SchemaType.OpenApi3 });
var json = schema.ToJson();

var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Poco
});

//// Act
var output = generator.GenerateFile("PersonAddress");

//// Assert
Assert.Contains("IDictionary<string, object> AdditionalProperties2\n", output);
}

[Fact]
Expand Down
25 changes: 20 additions & 5 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class ClassTemplateModel : ClassTemplateModelBase
private readonly CSharpTypeResolver _resolver;
private readonly JsonSchema _schema;
private readonly CSharpGeneratorSettings _settings;
private readonly List<PropertyModel> _properties;

/// <summary>Initializes a new instance of the <see cref="ClassTemplateModel"/> class.</summary>
/// <param name="typeName">Name of the type.</param>
Expand All @@ -34,10 +35,21 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
_settings = settings;

ClassName = typeName;
Properties = _schema.ActualProperties.Values
.Where(p => !p.IsInheritanceDiscriminator)
.Select(property => new PropertyModel(this, property, _resolver, _settings))
.ToArray();

AdditionalPropertiesPropertyName = "AdditionalProperties";
var actualProperties = _schema.ActualProperties;
_properties = new List<PropertyModel>(actualProperties.Count);
foreach (var property in actualProperties.Values)
{
if (!property.IsInheritanceDiscriminator)
{
_properties.Add(new PropertyModel(this, property, _resolver, _settings));
if (property.Name == AdditionalPropertiesPropertyName)
{
AdditionalPropertiesPropertyName += "2";
}
}
}

if (schema.InheritedSchema != null)
{
Expand Down Expand Up @@ -85,8 +97,11 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
// _schema.AdditionalPropertiesSchema.IsNullable(_settings.SchemaType),
// string.Empty) : null;

/// <summary>Gets property name for the additional properties.</summary>
public string? AdditionalPropertiesPropertyName { get; private set; }

/// <summary>Gets the property models.</summary>
public IEnumerable<PropertyModel> Properties { get; }
public IEnumerable<PropertyModel> Properties => _properties;

/// <summary>Gets the property models with inherited properties.</summary>
public IEnumerable<PropertyModel> AllProperties { get; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
{%- else -%}
[Newtonsoft.Json.JsonExtensionData]
{%- endif -%}
public System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}> AdditionalProperties
public System.Collections.Generic.IDictionary<string, {{ AdditionalPropertiesType }}> {{ AdditionalPropertiesPropertyName }}
{
get { return _additionalProperties ?? (_additionalProperties = new System.Collections.Generic.Dictionary<string, {{ AdditionalPropertiesType }}>()); }
{{PropertySetterAccessModifier}}set { _additionalProperties = value; }
Expand Down

0 comments on commit c6e01d7

Please sign in to comment.