Skip to content
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
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/CodeCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace NJsonSchema.CodeGeneration.CSharp.Tests;

public class CodeCompiler
{
public static void AssertCompile(string source)
public static void AssertCompile(string source, CSharpParseOptions options = null)
{
var syntaxTree = CSharpSyntaxTree.ParseText(source);
var syntaxTree = CSharpSyntaxTree.ParseText(source, options);

var metadataReferences = AppDomain.CurrentDomain.GetAssemblies()
.Where(x => !x.IsDynamic && !string.IsNullOrWhiteSpace(x.Location))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1695,6 +1695,50 @@ public async Task When_native_record_no_setter_in_class_and_constructor_provided
CodeCompiler.AssertCompile(output);
}

#if NETCOREAPP3_1_OR_GREATER || NET5_0_OR_GREATER
[Fact]
public async Task When_csharp_record_no_setter_in_record_and_constructor_provided()
{
// Arrange
var schema = JsonSchema.FromType<Address>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record,
GenerateNativeRecords = true,
SortConstructorParameters = false
});

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

// Assert
await VerifyHelper.Verify(output);
CodeCompiler.AssertCompile(output, new Microsoft.CodeAnalysis.CSharp.CSharpParseOptions(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9));
}

[Fact]
public async Task When_csharp_record_init_in_record_and_constructor_provided()
{
// Arrange
var schema = JsonSchema.FromType<Address>();
var data = schema.ToJson();
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record,
SortConstructorParameters = false,
GenerateNativeRecords = true,
});

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

// Assert
await VerifyHelper.Verify(output);
CodeCompiler.AssertCompile(output, new Microsoft.CodeAnalysis.CSharp.CSharpParseOptions(Microsoft.CodeAnalysis.CSharp.LanguageVersion.CSharp9));
}
#endif

public abstract class AbstractAddress
{
[JsonProperty("city")]
Expand Down
22 changes: 22 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/InheritanceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,28 @@ public async Task When_definitions_inherit_from_root_schema_and_STJ_polymorphism
CodeCompiler.AssertCompile(code);
}

[Fact]
public async Task When_definition_inherits_parameters_from_base_come_first()
{
// Arrange
var path = GetTestDirectory() + "/References/Car.json";

// Act
var schema = await JsonSchema.FromFileAsync(path);
var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings
{
ClassStyle = CSharpClassStyle.Record,
SortConstructorParameters = false,
});

// Act
var code = generator.GenerateFile();

// Assert
await VerifyHelper.Verify(code);
CodeCompiler.AssertCompile(code);
}

private string GetTestDirectory()
{
#pragma warning disable SYSLIB0012
Expand Down
16 changes: 16 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/Car.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"title": "Car",
"type": "object",
"description": "This is a car",
"allOf": [
{
"$ref": "Vehicle.json"
}
],
"properties": {
"wheels": {
"type": "integer",
"description": "The number of wheels on the car"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"title": "Vehicle",
"type": "object",
"x-abstract": true,
"description": "This is a vehicle",
"properties": {
"id": {
"type": "string",
"description": "This is the ID of Vehicle"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ namespace MyNamespace

public abstract partial class AbstractClass
{

[Newtonsoft.Json.JsonProperty("Foo", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Foo { get; set; }


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ namespace ns
public partial class Page
{


private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand All @@ -25,14 +24,13 @@ namespace ns

public partial class Library
{

[Newtonsoft.Json.JsonProperty("Index", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.Generic.IDictionary<string, string> Index { get; set; }

[Newtonsoft.Json.JsonProperty("Page", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Page Page { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ namespace ns

public partial class Library
{

[Newtonsoft.Json.JsonProperty("Index", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public System.Collections.Generic.IDictionary<string, string> Index { get; set; }

[Newtonsoft.Json.JsonProperty("Page", Required = Newtonsoft.Json.Required.Default, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public object Page { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace MyNamespace

public partial class Person
{

[Newtonsoft.Json.JsonProperty("Pet", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public Pet Pet { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand All @@ -28,14 +27,13 @@ namespace MyNamespace

public partial class Pet
{

[Newtonsoft.Json.JsonProperty("id", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public long Id { get; set; }

[Newtonsoft.Json.JsonProperty("category", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Category { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("Pet")]
public Pet Pet { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
Expand All @@ -33,12 +31,9 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("id")]
public long Id { get; set; }


[System.Text.Json.Serialization.JsonPropertyName("category")]
public string Category { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,9 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("dog")]
public Dog Dog { get; set; }


[System.Text.Json.Serialization.JsonPropertyName("cat")]
public Cat Cat { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
Expand Down Expand Up @@ -50,12 +47,9 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("id")]
public long Id { get; set; }


[System.Text.Json.Serialization.JsonPropertyName("category")]
public string Category { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("whiskers")]
public string Whiskers { get; set; }


}

public partial class Pet : Animal
Expand All @@ -23,7 +22,6 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("id")]
public long Id { get; set; }


}

public partial class Animal
Expand All @@ -32,8 +30,6 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("category")]
public string Category { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
Expand All @@ -51,8 +47,6 @@ namespace MyNamespace
[System.Text.Json.Serialization.JsonPropertyName("Name")]
public string Name { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[System.Text.Json.Serialization.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace MyNamespace

public partial class B
{

[Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Foo { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand All @@ -29,6 +28,7 @@ namespace MyNamespace
[Newtonsoft.Json.JsonConverter(typeof(JsonInheritanceConverter), "type")]
public partial class A : B
{

[Newtonsoft.Json.JsonProperty("prop", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.StringLength(30, MinimumLength = 1)]
Expand All @@ -37,7 +37,6 @@ namespace MyNamespace
[Newtonsoft.Json.JsonProperty("prop2", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop2 { get; set; }


}

[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = true)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace MyNamespace

public partial class B
{

[Newtonsoft.Json.JsonProperty("foo", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Foo { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand All @@ -28,6 +27,7 @@ namespace MyNamespace

public partial class A : B
{

[Newtonsoft.Json.JsonProperty("prop", Required = Newtonsoft.Json.Required.Always)]
[System.ComponentModel.DataAnnotations.Required]
[System.ComponentModel.DataAnnotations.StringLength(30, MinimumLength = 1)]
Expand All @@ -36,6 +36,5 @@ namespace MyNamespace
[Newtonsoft.Json.JsonProperty("prop2", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop2 { get; set; }


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@ namespace MyNamespace

public partial class Foo : Anonymous
{

[Newtonsoft.Json.JsonProperty("prop2", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public double Prop2 { get; set; }


}

public partial class Anonymous
{

[Newtonsoft.Json.JsonProperty("prop1", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop1 { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ namespace MyNamespace

public partial class Bar
{

[Newtonsoft.Json.JsonProperty("prop2", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop2 { get; set; }



private System.Collections.Generic.IDictionary<string, object> _additionalProperties;

[Newtonsoft.Json.JsonExtensionData]
Expand All @@ -28,9 +27,9 @@ namespace MyNamespace

public partial class Foo : Bar
{

[Newtonsoft.Json.JsonProperty("prop1", Required = Newtonsoft.Json.Required.DisallowNull, NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore)]
public string Prop1 { get; set; }


}
}
Loading