Permalink
Browse files

fix: json should be returned as PGObject, not as String for backward …

…compatibility reasons (#640)

fixes #639
  • Loading branch information...
vlsi committed Sep 17, 2016
1 parent 56c04d0 commit beaec3a0ec5b556cc1abfd50b3bf9017d5c09ac4
@@ -35,15 +35,19 @@ script:
before_cache:
# No sense in caching current build artifacts
rm -rf $HOME/.m2/repository/org/postgresql
- rm -rf $HOME/.m2/repository/org/postgresql
- find $HOME/.ivy2 -name "ivydata-*.properties" -delete
- find $HOME/.sbt -name "*.lock" -delete
# Skip default "mvn install" issued by Travis
# Root project cannot be compiled with older JDKs, so it makes sense to just skip the step
install: true
cache:
directories:
- '$HOME/.m2/repository'
- $HOME/.m2/repository
- $HOME/.ivy2/cache
- $HOME/.sbt/boot/
matrix:
fast_finish: true
@@ -107,6 +111,12 @@ matrix:
env:
- PG_VERSION=9.4
- MCENTRAL=Y
- jdk: oraclejdk8
addons:
postgresql: "9.4"
env:
- PG_VERSION=9.4
- TEST_CLIENTS=Y
- jdk: oraclejdk8 # this will be overwritten by before_install above
addons:
postgresql: "9.4"
@@ -123,6 +133,7 @@ matrix:
- PG_VERSION=9.4
- QUERY_MODE=simple
- COVERAGE=Y
- ANORM_SBT=Y
- jdk: oraclejdk8
addons:
postgresql: "9.4"
@@ -33,7 +33,10 @@ then
MVN_ARGS="$MVN_ARGS -Dcurrent.jdk=1.9 -Djavac.target=1.9"
fi
if [[ "$JDOC" == *"Y"* ]];
if [[ "$TEST_CLIENTS" ]];
then
mvn clean install -B -V -DskipTests $MVN_CUSTOM_ARGS
elif [[ "$JDOC" == *"Y"* ]];
then
# Build javadocs for Java 8 only
mvn ${MVN_ARGS} -P ${MVN_PROFILES},release-artifacts
@@ -51,6 +54,28 @@ else
mvn ${MVN_ARGS} -P ${MVN_PROFILES}
fi
# Run Scala-based and Clojure-based tests
if [[ "${TEST_CLIENTS}" == *"Y" ]];
then
mvn -DskipTests install
mkdir -p $HOME/.sbt/launchers/0.13.12
curl -L -o $HOME/.sbt/launchers/0.13.12/sbt-launch.jar http://dl.bintray.com/typesafe/ivy-releases/org.scala-sbt/sbt-launch/0.13.12/sbt-launch.jar
PROJECT_VERSION=$(mvn -B -N org.apache.maven.plugins:maven-help-plugin:2.1.1:evaluate -Dexpression=project.version | grep -v '\[')
cd test-anorm-sbt
sed -i "s/\"org.postgresql\" % \"postgresql\" % \"[^\"]*\"/\"org.postgresql\" % \"postgresql\" % \"${PROJECT_VERSION}\"/" build.sbt
sbt test
cd ..
# Uncomment when https://github.com/clojure/java.jdbc/pull/44 is merged in
#git clone --depth=10 https://github.com/clojure/java.jdbc.git
#cd java.jdbc
#TEST_DBS=postgres TEST_POSTGRES_USER=test TEST_POSTGRES_DBNAME=test mvn test -Djava.jdbc.test.pgjdbc.version=$PROJECT_VERSION
fi
if [[ "${COVERAGE}" == "Y" ]];
then
pip install --user codecov
@@ -93,7 +93,7 @@
//#if mvn.project.property.postgresql.jdbc.spec >= "JDBC4.2"
{"refcursor", Oid.REF_CURSOR, Types.REF_CURSOR, "java.sql.ResultSet", Oid.REF_CURSOR_ARRAY},
//#endif
{"json", Oid.JSON, Types.VARCHAR, "java.lang.String", Oid.JSON_ARRAY},
{"json", Oid.JSON, Types.OTHER, "org.postgresql.util.PGobject", Oid.JSON_ARRAY},
{"point", Oid.POINT, Types.OTHER, "org.postgresql.geometric.PGpoint", Oid.POINT_ARRAY}
};
@@ -136,10 +136,10 @@ public void testCreateArrayOfMultiJson() throws SQLException {
Array arr = rs.getArray(1);
ResultSet arrRs = arr.getResultSet();
Assert.assertTrue(arrRs.next());
Assert.assertEquals(in[0].getValue(), arrRs.getObject(2));
Assert.assertEquals(in[0], arrRs.getObject(2));
Assert.assertTrue(arrRs.next());
Assert.assertEquals(in[1].getValue(), arrRs.getObject(2));
Assert.assertEquals(in[1], arrRs.getObject(2));
}
@Test
@@ -0,0 +1,7 @@
.idea/
target/
logs/
.ivy2
*.swp
*.swo
.DS_STORE
@@ -0,0 +1,20 @@
# Test case for https://github.com/pgjdbc/pgjdbc/issues/639
## Dependencies
```
http://www.scala-sbt.org/download.html
```
## Database setup
```
createdb test
createuser -s test
```
## Running tests
```
sbt test
```
@@ -0,0 +1,22 @@
import play.PlayImport.PlayKeys._
name := "pgbug"
organization := "io.flow"
scalaVersion in ThisBuild := "2.11.8"
resolvers := (Resolver.mavenLocal +: resolvers.value)
lazy val root = project
.in(file("."))
.enablePlugins(PlayScala)
.settings(
libraryDependencies ++= Seq(
jdbc,
"com.typesafe.play" %% "anorm" % "2.5.2",
"org.postgresql" % "postgresql" % "9.4.1210",
"org.scalatestplus" %% "play" % "1.4.0" % "test"
)
)
@@ -0,0 +1,7 @@
db.default.url="jdbc:postgresql://localhost/test"
db.default.username=test
db.default.driver=org.postgresql.Driver
db.default.logStatements=true
db.default.hikaricp.minimumIdle=1
db.default.hikaricp.maximumPoolSize=1
evolutionplugin=disabled
@@ -0,0 +1 @@
sbt.version=0.13.12
@@ -0,0 +1,8 @@
// Comment to get more information during initialization
logLevel := Level.Warn
// The Typesafe repository
resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/"
// Use the Play sbt plugin for Play projects
addSbtPlugin("com.typesafe.play" % "sbt-plugin" % "2.4.3")
@@ -0,0 +1,74 @@
import anorm._
import org.scalatest._
import org.scalatestplus.play._
import play.api.db._
import play.api.libs.json._
import play.api.test._
import play.api.test.Helpers._
import scala.util.{Failure, Success, Try}
case class Example(
data: JsValue
)
class ExampleSpec extends PlaySpec with OneAppPerSuite {
import scala.concurrent.ExecutionContext.Implicits.global
"example" in {
DB.withConnection { implicit c =>
SQL("drop table if exists pgbugkeystore").execute()
SQL("create table pgbugkeystore(data json)").execute()
SQL("insert into pgbugkeystore (data) values ({data}::json)").on(
'data -> "{}"
).executeUpdate()
SQL("select data from pgbugkeystore").as(
SqlParser.get[JsObject]("data").*
)
}
}
implicit val columnToJsObject: Column[play.api.libs.json.JsObject] = Util.parser { _.as[play.api.libs.json.JsObject] }
/**
* Conversions to collections of objects using JSON.
*/
object Util {
def parseJson[T](f: play.api.libs.json.JsValue => T, columnName: String, value: String) = {
Try {
f(
play.api.libs.json.Json.parse(value)
)
} match {
case Success(result) => Right(result)
case Failure(ex) => Left(
TypeDoesNotMatch(
s"Column[$columnName] error parsing json $value: $ex"
)
)
}
}
def parser[T](
f: play.api.libs.json.JsValue => T
) = anorm.Column.nonNull { (value, meta) =>
val MetaDataItem(columnName, nullable, clazz) = meta
value match {
case json: org.postgresql.util.PGobject => parseJson(f, columnName.qualified, json.getValue)
case _=> {
Left(
TypeDoesNotMatch(
s"Column[${columnName.qualified}] error converting $value to Json. Expected class to be[org.postgresql.util.PGobject] and not[${value.asInstanceOf[AnyRef].getClass}"
)
)
}
}
}
}
}

0 comments on commit beaec3a

Please sign in to comment.