Skip to content

Commit

Permalink
only serialize parent property if it has members
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidVollmers committed Jun 18, 2024
1 parent 5adf620 commit 4bb6f06
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/Doki.Abstractions/DocumentationObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@ public abstract record DocumentationObject
/// <summary>
/// Gets the parent of the documentation object.
/// </summary>
[JsonIgnore]
[JsonConverter(typeof(DocumentationObjectConverter))]
public DocumentationObject? Parent { get; internal init; }

/// <summary>
/// Gets the content type of the documentation object.
/// </summary>
public DocumentationContentType ContentType { get; protected init; }

internal bool HasMembers { get; init; }

protected DocumentationObject()
{
Expand Down
26 changes: 26 additions & 0 deletions src/Doki.Abstractions/DocumentationObjectConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Doki;

internal class DocumentationObjectConverter : JsonConverter<DocumentationObject>
{
public override DocumentationObject? Read(ref Utf8JsonReader reader, Type typeToConvert,
JsonSerializerOptions options)
{
return (DocumentationObject?)JsonSerializer.Deserialize(ref reader, typeToConvert, options);
}

public override void Write(Utf8JsonWriter writer, DocumentationObject value, JsonSerializerOptions options)
{
if (!value.HasMembers)
{
writer.WriteNullValue();
return;
}

Console.WriteLine("Writing: {0}, {1}", value.Id, writer.CurrentDepth);

JsonSerializer.Serialize(writer, value, options);
}
}
5 changes: 3 additions & 2 deletions src/Doki.Abstractions/TypeDocumentationReference.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Doki;
// ReSharper disable InconsistentNaming
namespace Doki;

/// <summary>
/// Represents a type documentation reference in the documentation.
Expand All @@ -21,7 +22,7 @@ public record TypeDocumentationReference : MemberDocumentation
public bool IsMicrosoft { get; init; }

internal TypeDocumentationReference? InternalBaseType;

/// <summary>
/// Gets the base type of the type.
/// </summary>
Expand Down
6 changes: 5 additions & 1 deletion src/Doki.Output.Json/JsonOutput.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Doki.Output.Json;

public sealed class JsonOutput(OutputOptions<JsonOutput> options) : IOutput
{
private static readonly JsonSerializerOptions JsonSerializerOptions = new();
private static readonly JsonSerializerOptions JsonSerializerOptions = new()
{
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault
};

public Task BeginAsync(CancellationToken cancellationToken = default)
{
Expand Down
8 changes: 5 additions & 3 deletions src/Doki/DocumentationGenerator.Content.cs
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ private DocumentationObject BuildCRefDocumentation(string cref, DocumentationObj
Text = memberName
};

var typeDocumentationReference = BuildTypeDocumentationReference(type, parent);
var typeDocumentationReference = BuildTypeDocumentationReference(type, parent, true);

switch (memberType)
{
Expand Down Expand Up @@ -467,7 +467,8 @@ private DocumentationObject BuildCRefDocumentation(string cref, DocumentationObj
}
}

private TypeDocumentationReference BuildTypeDocumentationReference(Type type, DocumentationObject parent)
private TypeDocumentationReference BuildTypeDocumentationReference(Type type, DocumentationObject parent,
bool hasMembers = false)
{
var typeId = type.GetXmlDocumentationId();

Expand All @@ -484,7 +485,8 @@ private TypeDocumentationReference BuildTypeDocumentationReference(Type type, Do
IsGeneric = type.IsGenericType,
IsDocumented = IsTypeDocumented(type),
IsMicrosoft = IsAssemblyFromMicrosoft(assembly),
Parent = parent
Parent = parent,
HasMembers = hasMembers
};

if (type.BaseType != null)
Expand Down
1 change: 1 addition & 0 deletions src/Doki/DocumentationGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ private async Task<TypeDocumentation> GenerateTypeDocumentationAsync(GeneratorCo
Assembly = context.Current.Assembly.GetName().Name,
IsDocumented = true,
IsGeneric = context.Current.IsGenericType,
HasMembers = true
};

if (summary != null) typeDocumentation.Summary = BuildXmlDocumentation(summary, typeDocumentation);
Expand Down

0 comments on commit 4bb6f06

Please sign in to comment.