diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs index a0c98bf78..f33422088 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/NullableReferenceTypesTests.cs @@ -245,5 +245,48 @@ public async Task When_generating_from_json_schema_string_property_with_date_or_ Assert.Contains("public string Required { get; set; } = default!;", code); Assert.Contains("public string? Optional { get; set; } = default!;", code); } + + [Fact] + public async Task + When_generating_from_json_schema_string_property_with_reference_is_optional_and_GenerateNullableOptionalProperties_is_set_then_CSharp_property() + { + //// Arrange + var schemaJson = @" + { + ""type"": ""object"", + ""required"": [ + ""required"" + ], + ""properties"": { + ""required"": { ""$ref"": ""#/$defs/requiredString"" }, + ""optional"": { ""$ref"": ""#/$defs/optionalString"" } + }, + ""$defs"": { + ""requiredString"": { ""type"": ""string"" }, + ""optionalString"": { ""type"": ""string"" } + } + } + "; + + var schema = await JsonSchema.FromJsonAsync(schemaJson); + + //// Act + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings + { + ClassStyle = CSharpClassStyle.Poco, + SchemaType = SchemaType.OpenApi3, + DateType = "string", + DateTimeType = "string", + TimeType = "string", + TimeSpanType = "string", + GenerateNullableReferenceTypes = true, + GenerateOptionalPropertiesAsNullable = true + }); + var code = generator.GenerateFile("MyClass"); + + //// Assert + Assert.Contains("public string Required { get; set; } = default!;", code); + Assert.Contains("public string? Optional { get; set; } = default!;", code); + } } } \ No newline at end of file diff --git a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs index 13ddb5ef5..b0a25c809 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs @@ -60,13 +60,6 @@ public string Resolve(JsonSchema schema, bool isNullable, string? typeNameHint, throw new ArgumentNullException(nameof(schema)); } - schema = GetResolvableSchema(schema); - - if (schema == ExceptionSchema) - { - return "System.Exception"; - } - // Primitive schemas (no new type) if (Settings.GenerateOptionalPropertiesAsNullable && schema is JsonSchemaProperty property && @@ -75,6 +68,13 @@ schema is JsonSchemaProperty property && isNullable = true; } + schema = GetResolvableSchema(schema); + + if (schema == ExceptionSchema) + { + return "System.Exception"; + } + var markAsNullableType = Settings.GenerateNullableReferenceTypes && isNullable; if (schema.ActualTypeSchema.IsAnyType &&