Skip to content

Commit ec1709c

Browse files
authored
Fix encoding & decoding inconsistencies for link destination summary variant values (#451)
* Update OpenAPI spec to consistently use identifiers for language values rdar://103916556 * Fix inconsistent encoding/decoding for link summary variant language * Fix incorrect decoding of link summary variant usr
1 parent e06e798 commit ec1709c

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

Sources/SwiftDocC/LinkTargets/LinkDestinationSummary.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ extension LinkDestinationSummary.Variant {
474474
try container.encodeIfPresent(relativePresentationURL, forKey: .relativePresentationURL)
475475
try container.encodeIfPresent(title, forKey: .title)
476476
try container.encodeIfPresent(abstract, forKey: .abstract)
477-
try container.encodeIfPresent(language, forKey: .language)
477+
try container.encodeIfPresent(language?.id, forKey: .language)
478478
try container.encodeIfPresent(usr, forKey: .usr)
479479
try container.encodeIfPresent(declarationFragments, forKey: .declarationFragments)
480480
try container.encodeIfPresent(taskGroups, forKey: .taskGroups)
@@ -513,7 +513,7 @@ extension LinkDestinationSummary.Variant {
513513
relativePresentationURL = try container.decodeIfPresent(URL.self, forKey: .relativePresentationURL)
514514
title = try container.decodeIfPresent(String?.self, forKey: .title)
515515
abstract = try container.decodeIfPresent(LinkDestinationSummary.Abstract?.self, forKey: .abstract)
516-
usr = try container.decodeIfPresent(String?.self, forKey: .title)
516+
usr = try container.decodeIfPresent(String?.self, forKey: .usr)
517517
declarationFragments = try container.decodeIfPresent(LinkDestinationSummary.DeclarationFragments?.self, forKey: .declarationFragments)
518518
taskGroups = try container.decodeIfPresent([LinkDestinationSummary.TaskGroup]?.self, forKey: .taskGroups)
519519
}

Sources/SwiftDocC/SwiftDocC.docc/Resources/LinkableEntities.json

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -88,30 +88,6 @@
8888
}
8989
}
9090
},
91-
"SourceLanguage": {
92-
"type": "object",
93-
"required": [
94-
"name",
95-
"id"
96-
],
97-
"properties": {
98-
"name": {
99-
"type": "string"
100-
},
101-
"id": {
102-
"type": "string"
103-
},
104-
"idAliases": {
105-
"type": "array",
106-
"items": {
107-
"type": "string"
108-
}
109-
},
110-
"linkDisambiguationID": {
111-
"type": "string"
112-
}
113-
}
114-
},
11591
"RenderNodeVariantTrait": {
11692
"oneOf": [
11793
{
@@ -148,7 +124,7 @@
148124
"nullable": true
149125
},
150126
"language": {
151-
"$ref": "#/components/schemas/SourceLanguage",
127+
"type": "string",
152128
"nullable": true
153129
},
154130
"path": {

Tests/SwiftDocCTests/LinkTargets/LinkDestinationSummaryTests.swift

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ class ExternalLinkableTests: XCTestCase {
191191
.init(text: " ", kind: .text, identifier: nil),
192192
.init(text: "MyClass", kind: .identifier, identifier: nil),
193193
])
194+
195+
let encoded = try JSONEncoder().encode(summary)
196+
let decoded = try JSONDecoder().decode(LinkDestinationSummary.self, from: encoded)
197+
XCTAssertEqual(decoded, summary)
194198
}
195199

196200
do {
@@ -226,6 +230,10 @@ class ExternalLinkableTests: XCTestCase {
226230
.init(text: " : ", kind: .text, identifier: nil),
227231
.init(text: "Hashable", kind: .typeIdentifier, identifier: nil, preciseIdentifier: "p:hPP"),
228232
])
233+
234+
let encoded = try JSONEncoder().encode(summary)
235+
let decoded = try JSONDecoder().decode(LinkDestinationSummary.self, from: encoded)
236+
XCTAssertEqual(decoded, summary)
229237
}
230238

231239
do {
@@ -245,6 +253,10 @@ class ExternalLinkableTests: XCTestCase {
245253
XCTAssertEqual(summary.platforms, renderNode.metadata.platforms)
246254
XCTAssertEqual(summary.usr, "s:5MyKit0A5ClassC10myFunctionyyF")
247255
XCTAssertEqual(summary.declarationFragments, nil) // This symbol doesn't have a `subHeading` in the symbol graph
256+
257+
let encoded = try JSONEncoder().encode(summary)
258+
let decoded = try JSONDecoder().decode(LinkDestinationSummary.self, from: encoded)
259+
XCTAssertEqual(decoded, summary)
248260
}
249261

250262
do {
@@ -276,6 +288,10 @@ class ExternalLinkableTests: XCTestCase {
276288
.init(text: ")", kind: .text, identifier: nil),
277289
.init(text: "\n", kind: .text, identifier: nil),
278290
])
291+
292+
let encoded = try JSONEncoder().encode(summary)
293+
let decoded = try JSONDecoder().decode(LinkDestinationSummary.self, from: encoded)
294+
XCTAssertEqual(decoded, summary)
279295
}
280296
}
281297

@@ -333,6 +349,10 @@ class ExternalLinkableTests: XCTestCase {
333349
XCTAssertEqual(variant.usr, nil)
334350
XCTAssertEqual(variant.kind, nil)
335351
XCTAssertEqual(variant.taskGroups, nil)
352+
353+
let encoded = try JSONEncoder().encode(summary)
354+
let decoded = try JSONDecoder().decode(LinkDestinationSummary.self, from: encoded)
355+
XCTAssertEqual(decoded, summary)
336356
}
337357

338358
// Check the Swift version of a symbol that's represented differently in different languages
@@ -412,6 +432,10 @@ class ExternalLinkableTests: XCTestCase {
412432
)
413433
]
414434
)
435+
436+
let encoded = try JSONEncoder().encode(summary)
437+
let decoded = try JSONDecoder().decode(LinkDestinationSummary.self, from: encoded)
438+
XCTAssertEqual(decoded, summary)
415439
}
416440
}
417441

0 commit comments

Comments
 (0)