Skip to content

Commit

Permalink
Enforce backward-compatibility when encoding NominalDiffTime.
Browse files Browse the repository at this point in the history
  Keep encoding as integer when the value is a whole number of seconds. Otherwise, this may break clients down the line.
  • Loading branch information
KtorZ committed Aug 11, 2022
1 parent 0ade3d0 commit 8121673
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 10 deletions.
2 changes: 1 addition & 1 deletion clients/TypeScript/packages/schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export type RelativeTime = number;
*/
export type QueryUnavailableInCurrentEra = "QueryUnavailableInCurrentEra";
/**
* A slot length, in seconds.
* A slot length, in seconds. Starting from v5.5.4, this can be a floating number. Before v5.5.4, the floating value would be rounded to the nearest second.
*/
export type SlotLength = number;
/**
Expand Down
10 changes: 4 additions & 6 deletions docs/static/ogmios.wsp.json
Original file line number Diff line number Diff line change
Expand Up @@ -3641,10 +3641,9 @@
}

, "RelativeTime":
{ "type": "integer"
, "description": "A time in seconds relative to another one (typically, system start or era start)."
{ "type": "number"
, "description": "A time in seconds relative to another one (typically, system start or era start). Starting from v5.5.4, this can be a floating number. Before v5.5.4, the floating value would be rounded to the nearest second."
, "minimum": 0
, "maximum": 18446744073709552999
}

, "Relay":
Expand Down Expand Up @@ -4169,9 +4168,8 @@
}

, "SlotLength":
{ "type": "integer"
, "description": "A slot length, in seconds."
, "maximum": 18446744073709552000
{ "type": "number"
, "description": "A slot length, in seconds. Starting from v5.5.4, this can be a floating number. Before v5.5.4, the floating value would be rounded to the nearest second."
}

, "SoftwareVersion":
Expand Down
11 changes: 9 additions & 2 deletions server/src/Ogmios/Data/Json/Prelude.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ import Data.ByteString.Bech32
( HumanReadablePart, encodeBech32 )
import Data.IP
( IPv4, IPv6 )
import Data.Ratio
( (%) )
import Data.Scientific
( Scientific )
import Data.Sequence.Strict
Expand Down Expand Up @@ -295,8 +297,13 @@ encodeNatural =
{-# INLINABLE encodeNatural #-}

encodeNominalDiffTime :: NominalDiffTime -> Json
encodeNominalDiffTime =
Json.double . fromRational . toRational
encodeNominalDiffTime t =
-- TODO / NOTE: Backward-compatibility prior to v5.5.4. Should encode only
-- as Double in v6+
if i % 1 == r then Json.integer i else Json.double (fromRational r)
where
r = toRational t
i = round t
{-# INLINABLE encodeNominalDiffTime #-}

encodeNonNegativeInterval :: NonNegativeInterval -> Json
Expand Down
2 changes: 1 addition & 1 deletion server/test/unit/Ogmios/Data/JsonSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ spec = do
\7A80D87A801A002625A0D87983D879801A000F4240D879811A000FA92E"

context "SlotLength" $ do
let matrix = [ ( mkSlotLength 1, Json.double 1 )
let matrix = [ ( mkSlotLength 1, Json.integer 1 )
, ( mkSlotLength 0.1, Json.double 0.1 )
]
forM_ matrix $ \(slotLength, json) ->
Expand Down

0 comments on commit 8121673

Please sign in to comment.