Skip to content

Commit de1cdc2

Browse files
Relax rules for decoding JSON for asides with custom styles (#829)
* RenderBlockContent#init(from decoder:) overwrites aside style with name (if present) for all aside styles not just for notes. * Update Tests/SwiftDocCTests/Model/SemaToRenderNodeTests.swift Co-authored-by: David Rönnqvist <[email protected]>
1 parent c1bede3 commit de1cdc2

File tree

2 files changed

+34
-2
lines changed

2 files changed

+34
-2
lines changed

Sources/SwiftDocC/Model/Rendering/Content/RenderBlockContent.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -711,7 +711,7 @@ extension RenderBlockContent: Codable {
711711
self = try .paragraph(.init(inlineContent: container.decode([RenderInlineContent].self, forKey: .inlineContent)))
712712
case .aside:
713713
var style = try container.decode(AsideStyle.self, forKey: .style)
714-
if style.renderKind == "note", let displayName = try container.decodeIfPresent(String.self, forKey: .name) {
714+
if let displayName = try container.decodeIfPresent(String.self, forKey: .name) {
715715
style = AsideStyle(displayName: displayName)
716716
}
717717
self = try .aside(.init(style: style, content: container.decode([RenderBlockContent].self, forKey: .content)))

Tests/SwiftDocCTests/Model/SemaToRenderNodeTests.swift

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2956,9 +2956,41 @@ Document
29562956
{"type":"aside", "style":"note", "name":"See Also",
29572957
"content": [{"type":"paragraph", "inlineContent":[{"type":"text", "text":"And this other thing."}]}]},
29582958
{"type":"aside", "style":"note", "name":"Throws",
2959-
"content": [{"type":"paragraph", "inlineContent":[{"type":"text", "text":"A serious error."}]}]}
2959+
"content": [{"type":"paragraph", "inlineContent":[{"type":"text", "text":"A serious error."}]}]},
29602960
]
29612961
""")
2962+
2963+
// While decoding, overwrite the style with the name, if both are specified. We expect the style's raw value
2964+
// to be "Custom Title", not "important" in this example.
2965+
try assertJSONRepresentation(
2966+
RenderBlockContent.aside(
2967+
.init(
2968+
style: .init(rawValue: "Custom Title"),
2969+
content: [.paragraph(.init(inlineContent: [.text("This is a custom title...")]))]
2970+
)
2971+
),
2972+
"""
2973+
{
2974+
"type": "aside",
2975+
"content": [
2976+
{
2977+
"type": "paragraph",
2978+
"inlineContent": [
2979+
{
2980+
"type": "text",
2981+
"text": "This is a custom title..."
2982+
}
2983+
]
2984+
}
2985+
],
2986+
"style": "important",
2987+
"name": "Custom Title"
2988+
}
2989+
""")
2990+
2991+
for style in Aside.Kind.allCases.map({ RenderBlockContent.AsideStyle(asideKind: $0) }) + [.init(displayName: "Custom Title")] {
2992+
try assertRoundTripCoding(RenderBlockContent.aside(.init(style: style, content: [.paragraph(.init(inlineContent: [.text("This is a custom title...")]))])))
2993+
}
29622994
}
29632995

29642996
/// Tests links to symbols that have deprecation summary in markdown appear deprecated.

0 commit comments

Comments
 (0)