Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
Rico Suter committed Sep 26, 2023
2 parents 8e65d2d + b438a0c commit 2d9cf9f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/F.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"type": "object",
"properties": {
"name": {
"$ref": "./G.json",
"description": "This is the type of G"
}
}
}
4 changes: 4 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/References/G.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "string",
"nullable": true
}
21 changes: 21 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/ReferencesTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ public async Task When_ref_is_file_no_types_are_duplicated()
Assert.DoesNotContain("public enum C2", code);
}

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

//// Act
var schema = await JsonSchema.FromFileAsync(path);
var generatorSettings = new CSharpGeneratorSettings
{
GenerateNullableReferenceTypes = true
};
var generator = new CSharpGenerator(schema, generatorSettings);

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

//// Assert
Assert.Contains("public string? Name", code);
}

private string GetTestDirectory()
{
var codeBase = Assembly.GetExecutingAssembly().CodeBase;
Expand Down
10 changes: 9 additions & 1 deletion src/NJsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ public JsonSchema InheritedTypeSchema
{
get
{
if (ActualTypeSchema.IsDictionary || ActualTypeSchema.IsArray || ActualTypeSchema.IsTuple)
if (InheritedSchema == null && (ActualTypeSchema.IsDictionary || ActualTypeSchema.IsArray || ActualTypeSchema.IsTuple))
{
return ActualTypeSchema;
}
Expand Down Expand Up @@ -875,6 +875,14 @@ public virtual bool IsNullable(SchemaType schemaType)
return true;
}

if (ExtensionData != null && ExtensionData.TryGetValue("nullable", out var value))
{
if (bool.TryParse(value.ToString(), out var boolValue))
{
return boolValue;
}
}

return false;
}

Expand Down

0 comments on commit 2d9cf9f

Please sign in to comment.