Skip to content

Commit 19d9af7

Browse files
committed
Fix duration deserialisation
Signed-off-by: Paolo Di Tommaso <[email protected]>
1 parent e5d0e56 commit 19d9af7

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/main/groovy/io/seqera/wave/encoder/DateTimeAdapter.groovy

+7-2
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,12 @@ class DateTimeAdapter {
3232
}
3333

3434
@FromJson
35-
Duration deserializeDuration(Long value) {
36-
return value != null ? Duration.ofNanos(value) : null
35+
Duration deserializeDuration(String value) {
36+
if( value==null )
37+
return null
38+
// for backward compatibility duration may be encoded as float value
39+
// instead of long (number of nanoseconds) as expected
40+
final val0 = value.contains('.') ? Math.round(value.toDouble() * 1_000_000_000) : value.toLong()
41+
return value != null ? Duration.ofNanos(val0) : null
3742
}
3843
}

src/test/groovy/io/seqera/wave/encoder/MoshiEncodingStrategyTest.groovy

+17
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package io.seqera.wave.encoder
22

33
import spock.lang.Specification
44

5+
import java.time.Duration
56
import java.time.Instant
67

78
import io.seqera.wave.api.ContainerConfig
@@ -36,6 +37,22 @@ class MoshiEncodingStrategyTest extends Specification {
3637
copy == build
3738
}
3839

40+
def 'should decode old format build result' () {
41+
given:
42+
def encoder = new MoshiEncodeStrategy<BuildResult>() { }
43+
and:
44+
def json = '{"id":"100","exitStatus":1,"logs":"logs","startTime":"2022-12-03T22:27:04.079724Z","duration":60.000000000}'
45+
46+
when:
47+
def build = encoder.decode(json)
48+
then:
49+
build.id == '100'
50+
build.exitStatus == 1
51+
build.logs == 'logs'
52+
build.startTime == Instant.parse('2022-12-03T22:27:04.079724Z')
53+
build.duration == Duration.ofSeconds(60)
54+
}
55+
3956
def 'should encode and decode ContainerRequestData' () {
4057
given:
4158
def encoder = new MoshiEncodeStrategy<ContainerRequestData>() { }

0 commit comments

Comments
 (0)