Skip to content

Don't escape slashes in various JSONEncoders #2023

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

Merged
merged 1 commit into from
Mar 3, 2025
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 @@ -128,7 +128,7 @@ package struct ConfigSchemaGen {
jsonSchema.title = "SourceKit-LSP Configuration"
jsonSchema.comment = "DO NOT EDIT THIS FILE. This file is generated by \(#fileID)."
let encoder = JSONEncoder()
encoder.outputFormatting = [.prettyPrinted, .sortedKeys]
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
return try encoder.encode(jsonSchema)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Diagnose/RequestInfo.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ package struct RequestInfo: Sendable {

package func request(for file: URL) throws -> String {
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
guard var compilerArgs = String(data: try encoder.encode(compilerArgs), encoding: .utf8) else {
throw GenericError("Failed to encode compiler arguments")
}
Expand Down
5 changes: 2 additions & 3 deletions Sources/SKLogging/CustomLogStringConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ extension Optional where Wrapped: CustomLogStringConvertible {
extension Encodable {
package var prettyPrintedJSON: String {
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.prettyPrinted)
encoder.outputFormatting.insert(.sortedKeys)
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
guard let data = try? encoder.encode(self) else {
return "\(self)"
}
Expand Down Expand Up @@ -137,7 +136,7 @@ extension Encodable {
let jsonObject = try? JSONSerialization.jsonObject(with: encoded),
let data = try? JSONSerialization.data(
withJSONObject: redact(subject: jsonObject),
options: [.prettyPrinted, .sortedKeys]
options: [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
),
let string = String(data: data, encoding: .utf8)
else {
Expand Down
9 changes: 3 additions & 6 deletions Sources/SKTestSupport/CheckCoding.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ package func checkCoding<T: Codable & Equatable>(
line: UInt = #line
) {
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.prettyPrinted)
encoder.outputFormatting.insert(.sortedKeys)
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]

let data = try! encoder.encode(WrapFragment(value: value))
let wrappedStr = String(data: data, encoding: .utf8)!
Expand Down Expand Up @@ -64,8 +63,7 @@ package func checkEncoding<T: Encodable & Equatable>(
line: UInt = #line
) {
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.prettyPrinted)
encoder.outputFormatting.insert(.sortedKeys)
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
let data = try! encoder.encode(value)
let str = String(data: data, encoding: .utf8)!
// Remove trailing whitespace to normalize between corelibs and Apple Foundation.
Expand Down Expand Up @@ -101,8 +99,7 @@ package func checkCoding<T: Codable>(
body: (T) -> Void
) {
let encoder = JSONEncoder()
encoder.outputFormatting.insert(.prettyPrinted)
encoder.outputFormatting.insert(.sortedKeys)
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
let data = try! encoder.encode(value)
let str = String(data: data, encoding: .utf8)!
// Remove trailing whitespace to normalize between corelibs and Apple Foundation.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ package struct IndexedSingleSwiftFileTestProject {
]
)
let encoder = JSONEncoder()
encoder.outputFormatting = .prettyPrinted
encoder.outputFormatting = [.prettyPrinted, .sortedKeys, .withoutEscapingSlashes]
try encoder.encode(compilationDatabase).write(
to: testWorkspaceDirectory.appendingPathComponent(JSONCompilationDatabaseBuildSystem.dbName)
)
Expand Down
6 changes: 3 additions & 3 deletions Tests/LanguageServerProtocolJSONRPCTests/CodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class CodingTests: XCTestCase {
},
"locale" : "en-US",
"processId" : 1,
"rootPath" : "\\/foo",
"rootPath" : "/foo",
"trace" : "off"
}
}
Expand Down Expand Up @@ -74,7 +74,7 @@ final class CodingTests: XCTestCase {

},
"processId" : 1,
"rootPath" : "\\/foo",
"rootPath" : "/foo",
"trace" : "off"
}
}
Expand All @@ -86,7 +86,7 @@ final class CodingTests: XCTestCase {
json: """
{
"jsonrpc" : "2.0",
"method" : "$\\/cancelRequest",
"method" : "$/cancelRequest",
"params" : {
"id" : 1
}
Expand Down
27 changes: 13 additions & 14 deletions Tests/LanguageServerProtocolTests/CodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ final class CodingTests: XCTestCase {
func testValueCoding() throws {
let url = URL(fileURLWithPath: "/foo.swift")
let uri = DocumentURI(url)
// The \\/\\/\\/ is escaping file:// + /foo.swift, which is silly but allowed by json.
let urljson = "file:\\/\\/\\/foo.swift"
let urljson = "file:///foo.swift"

let range = Position(line: 5, utf16index: 23)..<Position(line: 6, utf16index: 0)
// Range.lowerBound -> start, Range.upperBound -> end
Expand Down Expand Up @@ -256,7 +255,7 @@ final class CodingTests: XCTestCase {
CodeDescription(href: try DocumentURI(string: "file:///some/path")),
json: """
{
"href" : "file:\\/\\/\\/some\\/path"
"href" : "file:///some/path"
}
"""
)
Expand Down Expand Up @@ -697,7 +696,7 @@ final class CodingTests: XCTestCase {
"line" : 1
}
},
"uri" : "file:\\/\\/\\/foo.swift"
"uri" : "file:///foo.swift"
},
"fromRanges" : [
{
Expand Down Expand Up @@ -862,7 +861,7 @@ final class CodingTests: XCTestCase {
],
"kind" : "full",
"relatedDocuments" : [
"file:\/\/\/some\/path",
"file:///some/path",
{
"kind" : "unchanged",
"resultId" : "myOtherResults"
Expand Down Expand Up @@ -898,7 +897,7 @@ final class CodingTests: XCTestCase {
{
"kind" : "unchanged",
"relatedDocuments" : [
"file:\/\/\/some\/path",
"file:///some/path",
{
"kind" : "unchanged",
"resultId" : "myOtherResults"
Expand Down Expand Up @@ -1039,7 +1038,7 @@ final class CodingTests: XCTestCase {
json: #"""
{
"textDocument" : {
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
}
}
"""#,
Expand All @@ -1056,7 +1055,7 @@ final class CodingTests: XCTestCase {
"line" : 4
},
"textDocument" : {
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
}
}
"""#,
Expand Down Expand Up @@ -1091,7 +1090,7 @@ final class CodingTests: XCTestCase {

],
"kind" : "full",
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
}
"""#
)
Expand All @@ -1107,7 +1106,7 @@ final class CodingTests: XCTestCase {
{
"kind" : "unchanged",
"resultId" : "myResults",
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
}
"""#
)
Expand Down Expand Up @@ -1139,7 +1138,7 @@ final class CodingTests: XCTestCase {
"line" : 3
}
},
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
},
"name" : "mySym"
}
Expand All @@ -1160,7 +1159,7 @@ final class CodingTests: XCTestCase {
{
"kind" : 17,
"location" : {
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
},
"name" : "mySym"
}
Expand All @@ -1173,7 +1172,7 @@ final class CodingTests: XCTestCase {
WorkspaceSymbol.WorkspaceSymbolLocation.uri(.init(uri: try DocumentURI(string: "file:///some/path"))),
json: #"""
{
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
}
"""#
)
Expand All @@ -1197,7 +1196,7 @@ final class CodingTests: XCTestCase {
"line" : 3
}
},
"uri" : "file:\/\/\/some\/path"
"uri" : "file:///some/path"
}
"""#
)
Expand Down
16 changes: 8 additions & 8 deletions config.schema.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$comment" : "DO NOT EDIT THIS FILE. This file is generated by ConfigSchemaGen\/ConfigSchemaGen.swift.",
"$schema" : "http:\/\/json-schema.org\/draft-07\/schema#",
"$comment" : "DO NOT EDIT THIS FILE. This file is generated by ConfigSchemaGen/ConfigSchemaGen.swift.",
"$schema" : "http://json-schema.org/draft-07/schema#",
"properties" : {
"backgroundIndexing" : {
"description" : "Whether background indexing is enabled.",
Expand Down Expand Up @@ -28,8 +28,8 @@
"type" : "integer"
},
"cancelTextDocumentRequestsOnEditAndClose" : {
"description" : "Whether sending a `textDocument\/didChange` or `textDocument\/didClose` notification for a document should cancel all pending requests for that document.",
"markdownDescription" : "Whether sending a `textDocument\/didChange` or `textDocument\/didClose` notification for a document should cancel all pending requests for that document.",
"description" : "Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document.",
"markdownDescription" : "Whether sending a `textDocument/didChange` or `textDocument/didClose` notification for a document should cancel all pending requests for that document.",
"type" : "boolean"
},
"clangdOptions" : {
Expand Down Expand Up @@ -75,10 +75,10 @@
"is-indexing-request"
],
"markdownEnumDescriptions" : [
"Enable support for the `textDocument\/onTypeFormatting` request.",
"Enable support for the `workspace\/_setOptions` request.",
"Enable the `workspace\/_sourceKitOptions` request.",
"Enable the `sourceKit\/_isIndexing` request."
"Enable support for the `textDocument/onTypeFormatting` request.",
"Enable support for the `workspace/_setOptions` request.",
"Enable the `workspace/_sourceKitOptions` request.",
"Enable the `sourceKit/_isIndexing` request."
],
"type" : "string"
},
Expand Down