Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Ignore nullability of the property to generate the association or field (Issue #83) #91

Merged
merged 2 commits into from
Jan 5, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,8 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
var isTypeParameterProp = parentClass?.TypeParameterList?.Parameters
.Any(t => t.Identifier.Text == type.ToString()) ?? false;

var typeIgnoringNullable = type is NullableTypeSyntax nullableTypeSyntax ? nullableTypeSyntax.ElementType : type;

var associationAttrSyntax = node.AttributeLists.GetAssociationAttributeSyntax();
if (associationAttrSyntax is not null)
{
Expand All @@ -263,8 +265,7 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
}
else if (!createAssociation
|| node.AttributeLists.HasIgnoreAssociationAttribute()
|| type.GetType() == typeof(PredefinedTypeSyntax)
|| type.GetType() == typeof(NullableTypeSyntax)
|| typeIgnoringNullable is PredefinedTypeSyntax
|| isTypeParameterProp)
{
var modifiers = GetMemberModifiersText(node.Modifiers,
Expand Down Expand Up @@ -293,7 +294,7 @@ public override void VisitPropertyDeclaration(PropertyDeclarationSyntax node)
{
additionalTypeDeclarationNodes.Add(type);
}
relationships.AddAssociationFrom(node);
relationships.AddAssociationFrom(node, typeIgnoringNullable);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ public void AddAssociationFrom(FieldDeclarationSyntax node, VariableDeclaratorSy
AddRelationship(leafName, rootName, symbol, fieldIdentifier);
}

public void AddAssociationFrom(PropertyDeclarationSyntax node)
public void AddAssociationFrom(PropertyDeclarationSyntax node, TypeSyntax typeIgnoringNullable)
{
if (node.Type is not SimpleNameSyntax leafNode
if (typeIgnoringNullable is not SimpleNameSyntax leafNode
|| node.Parent is not BaseTypeDeclarationSyntax rootNode) return;

var symbol = node.Initializer == null ? "-->" : "o->";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public ClassDiagramGeneratorTest(ITestOutputHelper outputHelper)
[InlineData("AttributeRequired.cs", "NotAttributeRequired.puml", true, false, false, Accessibilities.None)]
[InlineData("InputClasses.cs", "withoutStartEndUml.puml", true, false, true, Accessibilities.Private)]
[InlineData("DefaultModifierType.cs", "DefaultModifierType.puml", true, false, false, Accessibilities.None)]
[InlineData("NullableRelationship.cs", "NullableRelationship.puml", true, false, false, Accessibilities.None)]
public void Generate(string inputClassFile, string outpulPumlFile, bool createAssociations, bool attributeRequired, bool excludeUmlBeginEndTags, Accessibilities accessibilities)
{
var code = File.ReadAllText(Path.Combine("testData", inputClassFile));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,8 @@
</Content>
</ItemGroup>

<ItemGroup>
<None Remove="uml\NullableRelationship.puml" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace PlantUmlClassDiagramGeneratorTest.testData
{
public class Test
{
public LinkedClass AdditionalData { get; set; }
public LinkedClass? AdditionalData2 { get; set; }
}

public class LinkedClass
{
public string Name { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@startuml
class Test {
}
class LinkedClass {
+ Name : string <<get>> <<set>>
}
Test --> "AdditionalData" LinkedClass
Test --> "AdditionalData2" LinkedClass
@enduml
Loading