pierre3 / PlantUmlClassDiagramGenerator

This is a generator to create a class-diagram of PlantUML from the C# source code.
MIT License
680 stars 132 forks source link

Only nullable properties are outputted #83

Closed wkthomasvdb closed 9 months ago

wkthomasvdb commented 1 year ago

Having the following C# code:

public class Test
{
    public LinkedClass AdditionalData { get; set; }
    public LinkedClass? AdditionalData2 { get; set; }
}

public class LinkedClass
{
    public string Name { get; set; }
}

and running the following command: dotnet puml-gen ./ ./UML -dir -public -createAssociation

I get the following output:

@startuml
class Test {
    + AdditionalData2 : LinkedClass? <<get>> <<set>>
}
class LinkedClass {
    + Name : string <<get>> <<set>>
}
Test --> "AdditionalData" LinkedClass
@enduml

Only the nullable property is outputted. Using version 1.3.4

wkthomasvdb commented 1 year ago

I think that an additional check needs to be added in the ClassDiagramGenerator: https://github.com/pierre3/PlantUmlClassDiagramGenerator/blob/5f38665aa54ef998ad4cc98559d6f4fbd7da96d4/src/PlantUmlClassDiagramGenerator.Library/ClassDiagramGenerator.cs#L278

I think I should also check for: || type is IdentifierNameSyntax

nikDobrovolskiy commented 11 months ago

I'd like to help with this issue. I'm new at working on an open source project, but I have some experience in C#. Can I help and make changes to this project?

nikDobrovolskiy commented 11 months ago

I've written the test around the issue, and it is failed. The bug is present. Please, check if the expected result (expected puml) is correct.

The test:

[TestMethod]
public void NullableTestNullableAndNotNullableCustomTypes()
{
    var code = File.ReadAllText(Path.Combine("testData", "NullableAndNotNullableCustomType.cs"));
    var tree = CSharpSyntaxTree.ParseText(code);
    var root = tree.GetRoot();

    var output = new StringBuilder();
    using (var writer = new StringWriter(output))
    {
        var gen = new ClassDiagramGenerator(writer, "    ", Accessibilities.Private | Accessibilities.Internal
                                                                                    | Accessibilities.Protected | Accessibilities.ProtectedInternal);
        gen.Generate(root);
    }

    var expected = ConvertNewLineCode(File.ReadAllText(Path.Combine("uml", "nullableAndNotNullableCustomType.puml")), Environment.NewLine);
    var actual = output.ToString();
    Console.Write(actual);
    Assert.AreEqual(expected, actual);
}

Input test class: (testData/NullableAndNotNullableCustomType.cs)

public class NullableAndNotNullableCustomType
{
    public CustomType NotNullableCustomType { get; set; }
    public CustomType? NullableCustomType { get; set; }
}

public class CustomType
{
    public string Name { get; set; }
}

Expected puml: (uml/nullableAndNotNullableCustomType.puml)

@startuml
class NullableAndNotNullableCustomType {
    + NotNullableCustomType : CustomType  <<get>> <<set>>
    + NullableCustomType : CustomType?  <<get>> <<set>>
}
class CustomType {
    + Name : string <<get>> <<set>>
}
NullableAndNotNullableCustomType --> "NotNullableCustomType" CustomType
NullableAndNotNullableCustomType --> "NullableCustomType" CustomType
@enduml

Actual:

@startuml
class NullableAndNotNullableCustomType {
    + NullableCustomType : CustomType? <<get>> <<set>>
}
class CustomType {
    + Name : string <<get>> <<set>>
}
NullableAndNotNullableCustomType --> "NotNullableCustomType" CustomType
@enduml
PendingChanges commented 11 months ago

Hello, fixed it on my fork. But i didn't outputed the properties as they are now in the associations. PR incoming.

pierre3 commented 10 months ago

This issue has been resolved in version 1.4.0.

https://www.nuget.org/packages/PlantUmlClassDiagramGenerator/1.4.0 https://marketplace.visualstudio.com/items?itemName=pierre3.csharp-to-plantuml