diff --git a/CHANGELOG.md b/CHANGELOG.md index 7d01791..ed6337b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @germ-network/atprototypes +## 0.5.2 + +### Patch Changes + +- Backport of [#75](https://github.com/germ-network/AtprotoTypes/pull/75) (`8d809ff`, released in 0.10.0) for consumers still on 0.5.x. `PutRecordOutput.commit`, `PutRecordOutput.validationStatus` and `DeleteRecordOutput.commit` are now optional, so a PDS no-op (identical record, or deleting a missing record) decodes. `PutRecordOutput.ValidationStatus` is now an open `RawRepresentable` struct with `.valid` and `.unknown`, so unrecognized values decode instead of throwing. + ## 0.5.1 ### Patch Changes diff --git a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/DeleteRecord.swift b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/DeleteRecord.swift index 027d66a..c4b0494 100644 --- a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/DeleteRecord.swift +++ b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/DeleteRecord.swift @@ -61,9 +61,10 @@ extension Lexicon.Com.Atproto.Repo { } public struct DeleteRecordOutput: Codable, Sendable { - public let commit: Defs.CommitMeta + ///Absent when the record did not exist. + public let commit: Defs.CommitMeta? - public init(commit: Defs.CommitMeta) { + public init(commit: Defs.CommitMeta?) { self.commit = commit } } diff --git a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/PutRecord.swift b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/PutRecord.swift index ca1857f..00b2624 100644 --- a/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/PutRecord.swift +++ b/Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/PutRecord.swift @@ -70,19 +70,27 @@ extension Lexicon.Com.Atproto.Repo { public struct PutRecordOutput: Codable, Sendable { public let uri: String public let cid: String - public let commit: Defs.CommitMeta - public let validationStatus: ValidationStatus + ///Absent when the write was a no-op: the record was identical to the live one. + public let commit: Defs.CommitMeta? + public let validationStatus: ValidationStatus? - public enum ValidationStatus: String, Sendable, Codable { - case valid - case unknown + ///Lexicon knownValues: an open set, so an unrecognized value still decodes. + public struct ValidationStatus: RawRepresentable, Codable, Hashable, Sendable { + public let rawValue: String + + public init(rawValue: String) { + self.rawValue = rawValue + } + + public static let valid = Self(rawValue: "valid") + public static let unknown = Self(rawValue: "unknown") } public init( uri: String, cid: String, - commit: Defs.CommitMeta, - validationStatus: ValidationStatus + commit: Defs.CommitMeta?, + validationStatus: ValidationStatus? ) { self.uri = uri self.cid = cid diff --git a/Tests/AtprotoTypesTests/RepoWriteOutputDecodingTests.swift b/Tests/AtprotoTypesTests/RepoWriteOutputDecodingTests.swift new file mode 100644 index 0000000..472fd82 --- /dev/null +++ b/Tests/AtprotoTypesTests/RepoWriteOutputDecodingTests.swift @@ -0,0 +1,119 @@ +// +// RepoWriteOutputDecodingTests.swift +// AtprotoTypes +// + +import AtprotoTypes +import Foundation +import GermConvenience +import HTTPTypes +import Testing + +//The lexicons require only `uri` and `cid` on putRecord/createRecord output and +//nothing on deleteRecord output. A PDS omits `commit` when the write is a no-op +//(an identical record, or deleting a record that doesn't exist), and +//`validationStatus` is an open knownValues string. +struct RepoWriteOutputDecodingTests { + struct Thing: Atproto.Record { + struct Collection: Atproto.RecordType { + static var nsid: Atproto.NSID { .init(string: "example.thing") } + init() {} + init(from decoder: any Decoder) throws { self.init() } + func encode(to encoder: any Encoder) throws { + var container = encoder.singleValueContainer() + try container.encode(Self.nsid.rawValue) + } + } + typealias Key = Atproto.LiteralSelfRecordKey + var nsid: Collection { .init() } + var name = "x" + private enum CodingKeys: String, CodingKey { case name } + } + + typealias PutRecord = Lexicon.Com.Atproto.Repo.PutRecord + typealias DeleteRecord = Lexicon.Com.Atproto.Repo.DeleteRecord + + static let uri = "at://did:plc:abcdefghijklmnopqrstuvwx/example.thing/self" + static let cid = "bafyreibmcnkzbxtciyydktbatjlpgh5hollpov4zvpwtaaq353vyzfbxwu" + static let rev = "3lbwqrstuvwxy" + + static func ok(_ json: String) -> HTTPDataResponse { + .init(data: Data(json.utf8), response: .init(status: .ok)) + } + + static func parsed( + _: P.Type, _ json: String + ) throws -> P.Output { + let parsed = try P.parse(fullResponse: ok(json)) + guard case .ok(let output) = parsed else { + Issue.record("expected .ok, got \(parsed)") + throw ParseFailed() + } + return output + } + struct ParseFailed: Error {} + + @Test func putRecordNoOpWithoutCommitParses() throws { + let output = try Self.parsed( + PutRecord.self, + #"{"uri":"\#(Self.uri)","cid":"\#(Self.cid)","validationStatus":"valid"}"#) + #expect(output.uri == Self.uri) + #expect(output.cid == Self.cid) + #expect(output.commit == nil) + #expect(output.validationStatus == .valid) + } + + //rsky (Blacksky's PDS) returns only these two fields on every write + @Test func putRecordUriAndCidOnlyParses() throws { + let output = try Self.parsed( + PutRecord.self, #"{"cid":"\#(Self.cid)","uri":"\#(Self.uri)"}"#) + #expect(output.uri == Self.uri) + #expect(output.commit == nil) + #expect(output.validationStatus == nil) + } + + @Test func putRecordWithoutValidationStatusParses() throws { + let output = try Self.parsed( + PutRecord.self, + #"{"uri":"\#(Self.uri)","cid":"\#(Self.cid)","commit":{"cid":"\#(Self.cid)","rev":"\#(Self.rev)"}}"# + ) + #expect(output.cid == Self.cid) + #expect(output.validationStatus == nil) + } + + @Test func putRecordUnrecognizedValidationStatusParses() throws { + let output = try Self.parsed( + PutRecord.self, + #"{"uri":"\#(Self.uri)","cid":"\#(Self.cid)","commit":{"cid":"\#(Self.cid)","rev":"\#(Self.rev)"},"validationStatus":"deferred"}"# + ) + #expect(output.validationStatus?.rawValue == "deferred") + } + + @Test func putRecordWriteKeepsCommit() throws { + let output = try Self.parsed( + PutRecord.self, + #"{"uri":"\#(Self.uri)","cid":"\#(Self.cid)","commit":{"cid":"\#(Self.cid)","rev":"\#(Self.rev)"},"validationStatus":"valid"}"# + ) + #expect(output.commit?.rev.rawValue == Self.rev) + #expect(output.validationStatus == .valid) + } + + @Test func deleteRecordNoOpEmptyBodyParses() throws { + let output = try Self.parsed(DeleteRecord.self, "{}") + #expect(output.commit == nil) + } + + @Test func validationStatusEncodesAsBareString() throws { + let data = try JSONEncoder().encode( + Lexicon.Com.Atproto.Repo.PutRecordOutput.ValidationStatus.valid) + #expect(String(decoding: data, as: UTF8.self) == #""valid""#) + } + + @Test func absentCommitIsOmittedWhenEncoding() throws { + let output = Lexicon.Com.Atproto.Repo.PutRecordOutput( + uri: Self.uri, cid: Self.cid, commit: nil, validationStatus: nil) + let json = String(decoding: try JSONEncoder().encode(output), as: UTF8.self) + #expect(!json.contains("commit")) + #expect(!json.contains("validationStatus")) + } +} diff --git a/package.json b/package.json index 0afbcf2..1be00f5 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@germ-network/atprototypes", "private": true, - "version": "0.5.1", + "version": "0.5.2", "engines": { "node": ">=24" },