Skip to content

Commit

Permalink
Check ExtensionData for nullable property (#1584)
Browse files Browse the repository at this point in the history
  • Loading branch information
levimatheri authored Sep 26, 2023
1 parent 80146f7 commit b438a0c
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 0 deletions.
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
8 changes: 8 additions & 0 deletions src/NJsonSchema/JsonSchema.cs
Original file line number Diff line number Diff line change
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 b438a0c

Please sign in to comment.