Skip to content
Closed
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
7 changes: 6 additions & 1 deletion lib/openapi_kit/codec/date_time.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,13 @@ def self.from_wire(value)
end
end

# Rails renders a Time through as_json at three decimal places
# (ActiveSupport::JSON::Encoding.time_precision), so an application moving onto
# openapi_kit keeps emitting the timestamps its clients already parse.
PRECISION = 3

sig { override.params(value: ::Time).returns(OpenAPIKit::Wire) }
def self.to_wire(value) = value.utc.iso8601
def self.to_wire(value) = value.utc.iso8601(PRECISION)
end
end
end
8 changes: 7 additions & 1 deletion spec/openapi_kit/codec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,13 @@
describe "round trips" do
it "normalises a date-time to UTC" do
value = described_class::DateTime.from_wire("2026-09-02T12:00:00+02:00")
expect(described_class::DateTime.to_wire(value)).to eq("2026-09-02T10:00:00Z")
expect(described_class::DateTime.to_wire(value)).to eq("2026-09-02T10:00:00.000Z")
end

it "keeps the sub-second precision Rails renders, so a migrating client sees no change" do
value = described_class::DateTime.from_wire("2026-09-02T12:00:00.250Z")

expect(described_class::DateTime.to_wire(value)).to eq("2026-09-02T12:00:00.250Z")
end

it "keeps decimal precision as a string" do
Expand Down
2 changes: 1 addition & 1 deletion spec/scripts/round_trip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def check(label)
check("differing values are unequal") { KitchenSink::Types::Mod::Codec.from_wire(WIRE.merge("id" => 8)) != mod }

wire = KitchenSink::Types::Mod::Codec.to_wire(mod)
check("dumped date-time") { wire["updatedAt"] == "2026-09-02T10:00:00Z" }
check("dumped date-time") { wire["updatedAt"] == "2026-09-02T10:00:00.000Z" }
check("dumped enum") { wire["status"] == "under-moderation" }
check("required nullable stays null") { wire.key?("deletedAt") && wire["deletedAt"].nil? }
check("absent optional omitted") { !wire.key?("summary") }
Expand Down