Skip to content
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
22 changes: 15 additions & 7 deletions Sources/AtprotoTypes/Lexicon/Com/Atproto/Repo/PutRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
119 changes: 119 additions & 0 deletions Tests/AtprotoTypesTests/RepoWriteOutputDecodingTests.swift
Original file line number Diff line number Diff line change
@@ -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<Thing>
typealias DeleteRecord = Lexicon.Com.Atproto.Repo.DeleteRecord<Thing>

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: Atproto.XRPC.ResponseParsing>(
_: 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"))
}
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@germ-network/atprototypes",
"private": true,
"version": "0.5.1",
"version": "0.5.2",
"engines": {
"node": ">=24"
},
Expand Down
Loading