Skip to content

Commit f6dde97

Browse files
authored
Merge pull request #17 from IvanMurzak/fix/output-schema
Enhance JSON schema generation with $defs support and 'result' property
2 parents e7fde7f + 67fac97 commit f6dde97

38 files changed

Lines changed: 2398 additions & 492 deletions

.vscode/settings.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"cSpell.words": [
33
"unstringified",
4-
"Unstringify"
4+
"Unstringify",
5+
"Xunit"
56
]
67
}

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ jsonSerializer.AddConverter(new MyCustomJsonConverter());
372372

373373
// Access to JSON schema generation
374374
var jsonSchema = reflector.JsonSchema;
375-
var schema = jsonSchema.GetSchema<MyClass>(reflector, justRef: false);
375+
var schema = jsonSchema.GetSchema<MyClass>(reflector);
376376
```
377377

378378
#### Converter System Architecture
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// Complex class: Address with multiple properties
2+
namespace com.IvanMurzak.ReflectorNet.OuterAssembly.Model
3+
{
4+
public class Address
5+
{
6+
public string Street { get; set; } = string.Empty;
7+
public string City { get; set; } = string.Empty;
8+
public string? Zip { get; set; }
9+
public string Country { get; set; } = string.Empty;
10+
}
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Complex class: Company with nested collections and references
2+
using System.Collections.Generic;
3+
4+
namespace com.IvanMurzak.ReflectorNet.OuterAssembly.Model
5+
{
6+
public class Company
7+
{
8+
public string Name { get; set; } = string.Empty;
9+
public Address? Headquarters { get; set; }
10+
public List<Person> Employees { get; } = new List<Person>();
11+
public Dictionary<string, List<Person>> Teams { get; } = new Dictionary<string, List<Person>>();
12+
public Dictionary<string, Dictionary<string, Person>> Directory { get; } = new();
13+
}
14+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Complex class: Person with mixed fields/properties and nested types
2+
using System;
3+
using System.Collections.Generic;
4+
5+
namespace com.IvanMurzak.ReflectorNet.OuterAssembly.Model
6+
{
7+
public class Person
8+
{
9+
// Fields
10+
public string FirstName = string.Empty;
11+
public string LastName = string.Empty;
12+
private DateTime _birthDate;
13+
14+
// Properties
15+
public int Age { get; set; }
16+
public Address? Address { get; set; }
17+
public List<string> Tags { get; } = new List<string>();
18+
public Dictionary<string, int> Scores { get; } = new Dictionary<string, int>();
19+
20+
// Arrays
21+
public int[] Numbers { get; set; } = Array.Empty<int>();
22+
public string[][] JaggedAliases { get; set; } = Array.Empty<string[]>();
23+
public int[,] Matrix2x2 { get; set; } = new int[2, 2];
24+
25+
// Methods using primitives and collections
26+
public void SetBirthDate(DateTime date) => _birthDate = date;
27+
public DateTime GetBirthDate() => _birthDate;
28+
29+
public void AddTag(string tag) => Tags.Add(tag);
30+
public void SetScore(string key, int value) => Scores[key] = value;
31+
}
32+
}

ReflectorNet.Tests.OuterAssembly/Model/NestedClass.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,24 @@ public class WrapperClass<T>
4545
[JsonInclude]
4646
public T? ValueField;
4747
public T? ValueProperty { get; set; }
48+
49+
public WrapperClass() { }
50+
public WrapperClass(T? valueField, T? valueProperty)
51+
{
52+
ValueField = valueField;
53+
ValueProperty = valueProperty;
54+
}
55+
56+
/// <summary>
57+
/// Echo method that returns the provided value unchanged.
58+
/// Used for testing return type schemas with various generic types.
59+
/// </summary>
60+
public T Echo(T value) => value;
61+
62+
/// <summary>
63+
/// Echo method that returns a nullable version of the provided value.
64+
/// Used for testing return type schemas with nullable generic types.
65+
/// </summary>
66+
public T? EchoNullable(T? value) => value;
4867
}
4968
}

ReflectorNet.Tests/JsonConverterTests/JsonConverterTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using com.IvanMurzak.ReflectorNet.Tests.Model;
1+
using com.IvanMurzak.ReflectorNet.OuterAssembly.Model;
2+
using com.IvanMurzak.ReflectorNet.Tests.Model;
23
using Xunit.Abstractions;
34

45
namespace com.IvanMurzak.ReflectorNet.Tests.Utils

ReflectorNet.Tests/Model/JsonConvertors/ObjectRefConverter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,15 @@
11
using System;
2+
using System.Collections.Generic;
23
using System.Text.Json;
34
using System.Text.Json.Nodes;
4-
using System.Text.Json.Serialization;
55
using com.IvanMurzak.ReflectorNet.Json;
66
using com.IvanMurzak.ReflectorNet.Utils;
77

88
namespace com.IvanMurzak.ReflectorNet.Tests.Model
99
{
10-
public class ObjectRefConverter : JsonConverter<ObjectRef>, IJsonSchemaConverter
10+
public class ObjectRefConverter : JsonSchemaConverter<ObjectRef>, IJsonSchemaConverter
1111
{
12-
public string Id => typeof(ObjectRef).GetTypeId();
13-
public JsonNode GetScheme() => new JsonObject
12+
public override JsonNode GetSchema() => new JsonObject
1413
{
1514
[JsonSchema.Type] = JsonSchema.Object,
1615
[JsonSchema.Properties] = new JsonObject
@@ -21,7 +20,7 @@ public class ObjectRefConverter : JsonConverter<ObjectRef>, IJsonSchemaConverter
2120
},
2221
[JsonSchema.Required] = new JsonArray { nameof(ObjectRef.instanceID) }
2322
};
24-
public JsonNode GetSchemeRef() => new JsonObject
23+
public override JsonNode GetSchemaRef() => new JsonObject
2524
{
2625
[JsonSchema.Ref] = JsonSchema.RefValue + Id
2726
};

ReflectorNet.Tests/Model/JsonConvertors/SerializedMemberCustomDescriptionConverter.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
using System;
22
using System.Text.Json;
33
using System.Text.Json.Nodes;
4-
using System.Text.Json.Serialization;
54
using com.IvanMurzak.ReflectorNet.Json;
65
using com.IvanMurzak.ReflectorNet.Model;
76
using com.IvanMurzak.ReflectorNet.Utils;
87

98
namespace com.IvanMurzak.ReflectorNet.Tests.Model
109
{
11-
public class SerializedMemberCustomDescriptionConverter : JsonConverter<SerializedMember>, IJsonSchemaConverter
10+
public class SerializedMemberCustomDescriptionConverter : JsonSchemaConverter<SerializedMember>, IJsonSchemaConverter
1211
{
1312
public const string CustomDescription = "Custom description, used for testing purposes.";
1413

15-
public static string StaticId => TypeUtils.GetTypeId<SerializedMember>();
1614
public static JsonNode Schema => new JsonObject
1715
{
1816
[JsonSchema.Type] = JsonSchema.Object,
@@ -64,15 +62,13 @@ public class SerializedMemberCustomDescriptionConverter : JsonConverter<Serializ
6462

6563
readonly Reflector _reflector;
6664

67-
public string Id => StaticId;
68-
6965
public SerializedMemberCustomDescriptionConverter(Reflector reflector)
7066
{
7167
_reflector = reflector ?? throw new ArgumentNullException(nameof(reflector));
7268
}
7369

74-
public JsonNode GetSchemeRef() => SchemaRef;
75-
public JsonNode GetScheme() => Schema;
70+
public override JsonNode GetSchemaRef() => SchemaRef;
71+
public override JsonNode GetSchema() => Schema;
7672

7773
public override SerializedMember? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
7874
{

ReflectorNet.Tests/Model/NestedClass.cs

Lines changed: 0 additions & 56 deletions
This file was deleted.

0 commit comments

Comments
 (0)