Skip to content

Commit 9d324b5

Browse files
committed
fix HUGE bug with datatype "string"
- I forgot to support values of type string in the input api... this is now fixed
1 parent 2662460 commit 9d324b5

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

other/mysql/test-data.sql

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ INSERT INTO `objects` (`id`, `name`, `description`, `ugrp_id`, `unit_symbol`, `c
5151
(2, 'volts box 2', NULL, 2, 'V', '2019-01-01'),
5252
(3, 'tmp box 1', NULL, 2, 'h', '2019-01-01'),
5353
(4, 'tmp box 2', NULL, 2, 'Here', DEFAULT),
54-
(5, 'caching', NULL, 2, 'V', DEFAULT),
54+
(5, 'some free text', NULL, 2, 'char', DEFAULT),
5555
-- one object owned by the "other" usergroup
5656
(6, 'owned by other', NULL, 3, 'V', DEFAULT),
5757
-- objects that have values in cassandra
@@ -92,6 +92,7 @@ INSERT INTO `tokens` (`id`, `token`, `object_id`, `description`) VALUES
9292
(1, '012345678901234567890123456789a1', 1, 'test'),
9393
(2, '012345678901234567890123456789a2', 2, 'test'),
9494
(3, '012345678901234567890123456789a3', 3, 'test'),
95-
(4, '012345678901234567890123456789a4', 4, 'test');
95+
(4, '012345678901234567890123456789a4', 4, 'test'),
96+
(5, '012345678901234567890123456789a5', 5, 'test');
9697

9798
COMMIT;

src/main/kotlin/ch/derlin/bbdata/output/api/types/BaseType.kt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@ package ch.derlin.bbdata.output.api.types
1212
*/
1313

1414

15+
import ch.derlin.bbdata.common.truncate
1516
import com.fasterxml.jackson.annotation.JsonIdentityInfo
1617
import com.fasterxml.jackson.annotation.JsonIdentityReference
1718
import com.fasterxml.jackson.annotation.ObjectIdGenerators
1819
import org.hibernate.validator.constraints.Length
20+
import org.slf4j.Logger
21+
import org.slf4j.LoggerFactory
1922
import javax.persistence.*
2023
import javax.validation.constraints.NotEmpty
2124

@@ -40,9 +43,12 @@ data class BaseType(
4043
companion object {
4144
const val TYPE_MAX = 45
4245

46+
private val log: Logger = LoggerFactory.getLogger(BaseType::class.java)
47+
4348
fun parseType(value: String, type: String): Any? {
4449
try {
4550
when (type) {
51+
"string" -> return value
4652
"float" -> return value.toFloat()
4753
"int" -> return value.toInt()
4854
"bool" -> {
@@ -52,8 +58,10 @@ data class BaseType(
5258
return false
5359
return null
5460
}
61+
else -> log.error("got an unknown unit type '$type' for value '$value'")
5562
}
5663
} catch (e: Exception) {
64+
log.debug("exception while parsing value ${value.truncate()} of type $type: ${e.javaClass.simpleName} ${e.message}")
5765
}
5866
return null
5967
}

src/test/kotlin/ch/derlin/bbdata/input/InputApiTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,13 @@ class InputApiTest {
203203
assertEquals(HttpStatus.OK, resp.statusCode, "good bool false: $v. ${resp.body}")
204204
assertEquals("false", JsonPath.parse(resp.body).read<String>("$[0].value"))
205205
}
206+
207+
// test good string
208+
for (v in listOf("False", "12.0", """{\"json\": \"data\", \"works\": 1}""", " ", true)) {
209+
resp = restTemplate.postWithBody(URL, getMeasureBody(objectId = 5, value = v))
210+
assertEquals(HttpStatus.OK, resp.statusCode, "good string: $v. ${resp.body}")
211+
assertEquals(v.toString().replace("\\\"", "\""), JsonPath.parse(resp.body).read<String>("$[0].value"))
212+
}
206213
}
207214

208215
@Test

0 commit comments

Comments
 (0)