diff --git a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs index 472331833..9ebdd79ea 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs @@ -93,5 +93,88 @@ public async Task When_class_is_obsolete_with_a_message_then_obsolete_attribute_ await VerifyHelper.Verify(code); CSharpCompiler.AssertCompile(code); } + + [Fact] + public async Task When_class_with_base_class_has_deprecated_in_allOf_then_obsolete_attribute_is_rendered() + { + // Arrange - simulates the schema structure where deprecated is in an allOf sub-schema + var json = @"{ + ""type"": ""object"", + ""definitions"": { + ""BaseDto"": { + ""type"": ""object"", + ""properties"": { + ""id"": { ""type"": ""string"" } + } + }, + ""DerivedDto"": { + ""allOf"": [ + { ""$ref"": ""#/definitions/BaseDto"" }, + { + ""type"": ""object"", + ""x-deprecated"": true, + ""x-deprecatedMessage"": ""Has been replaced by flows."", + ""properties"": { + ""url"": { ""type"": ""string"" } + } + } + ] + } + }, + ""properties"": { + ""item"": { ""$ref"": ""#/definitions/DerivedDto"" } + } + }"; + var schema = await JsonSchema.FromJsonAsync(json); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()); + + // Act + var code = generator.GenerateFile(); + + // Assert + Assert.Contains("[System.Obsolete(\"Has been replaced by flows.\")]", code); + CSharpCompiler.AssertCompile(code); + } + + [Fact] + public async Task When_class_with_base_class_has_deprecated_without_message_in_allOf_then_obsolete_attribute_is_rendered() + { + // Arrange + var json = @"{ + ""type"": ""object"", + ""definitions"": { + ""BaseDto"": { + ""type"": ""object"", + ""properties"": { + ""id"": { ""type"": ""string"" } + } + }, + ""DeprecatedDto"": { + ""allOf"": [ + { ""$ref"": ""#/definitions/BaseDto"" }, + { + ""type"": ""object"", + ""x-deprecated"": true, + ""properties"": { + ""name"": { ""type"": ""string"" } + } + } + ] + } + }, + ""properties"": { + ""item"": { ""$ref"": ""#/definitions/DeprecatedDto"" } + } + }"; + var schema = await JsonSchema.FromJsonAsync(json); + var generator = new CSharpGenerator(schema, new CSharpGeneratorSettings()); + + // Act + var code = generator.GenerateFile(); + + // Assert + Assert.Contains("[System.Obsolete]", code); + CSharpCompiler.AssertCompile(code); + } } } diff --git a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs index 2071b0312..144083e92 100644 --- a/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs +++ b/src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs @@ -184,12 +184,13 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings, public string JsonConvertersArrayCode => CSharpJsonSerializerGenerator.GenerateJsonConvertersArrayCode(_settings, null); /// Gets a value indicating whether the class is deprecated. - public bool IsDeprecated => _schema.IsDeprecated; + public bool IsDeprecated => _schema.IsDeprecated || _schema.AllOf.Any(s => s.IsDeprecated); /// Gets a value indicating whether the class has a deprecated message. - public bool HasDeprecatedMessage => !string.IsNullOrEmpty(_schema.DeprecatedMessage); + public bool HasDeprecatedMessage => !string.IsNullOrEmpty(DeprecatedMessage); /// Gets the deprecated message. - public string? DeprecatedMessage => _schema.DeprecatedMessage; + public string? DeprecatedMessage => _schema.DeprecatedMessage ?? + _schema.AllOf.Select(s => s.DeprecatedMessage).FirstOrDefault(m => !string.IsNullOrEmpty(m)); } } \ No newline at end of file