Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/NJsonSchema.CodeGeneration.CSharp.Tests/ObsoleteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
public string JsonConvertersArrayCode => CSharpJsonSerializerGenerator.GenerateJsonConvertersArrayCode(_settings, null);

/// <summary>Gets a value indicating whether the class is deprecated.</summary>
public bool IsDeprecated => _schema.IsDeprecated;
public bool IsDeprecated => _schema.IsDeprecated || _schema.AllOf.Any(s => s.IsDeprecated);

/// <summary>Gets a value indicating whether the class has a deprecated message.</summary>
public bool HasDeprecatedMessage => !string.IsNullOrEmpty(_schema.DeprecatedMessage);
public bool HasDeprecatedMessage => !string.IsNullOrEmpty(DeprecatedMessage);

/// <summary>Gets the deprecated message.</summary>
public string? DeprecatedMessage => _schema.DeprecatedMessage;
public string? DeprecatedMessage => _schema.DeprecatedMessage ??
_schema.AllOf.Select(s => s.DeprecatedMessage).FirstOrDefault(m => !string.IsNullOrEmpty(m));
}
}