Skip to content

Commit

Permalink
Fix a bug leading to the truncation of the data returned by std.gzip …
Browse files Browse the repository at this point in the history
…and std.xz (#221)

The output of std.gzip and std.xz was truncated because the output
stream was closed after being read and encoded into base64.

This regression was introduced in
#210
  • Loading branch information
stephenamar-db authored Dec 2, 2024
1 parent ab5bb62 commit 2047a32
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
val sjsonnetVersion = "0.4.12"
val sjsonnetVersion = "0.4.12.1"

scalaVersion in Global := "2.13.4"

Expand Down
2 changes: 1 addition & 1 deletion build.sc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import mill._, scalalib._, publish._, scalajslib._, scalanativelib._, scalanativelib.api._
val sjsonnetVersion = "0.4.12"
val sjsonnetVersion = "0.4.12.1"

object sjsonnet extends Cross[SjsonnetModule]("2.12.13", "2.13.4")
class SjsonnetModule(val crossScalaVersion: String) extends Module {
Expand Down
15 changes: 9 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Sjsonnet can be used from Java:
<dependency>
<groupId>com.databricks</groupId>
<artifactId>sjsonnet_2.13</artifactId>
<version>0.4.12</version>
<version>0.4.12.1</version>
</dependency>
```

Expand All @@ -30,8 +30,8 @@ sjsonnet.SjsonnetMain.main0(
From Scala:

```scala
"com.databricks" %% "sjsonnet" % "0.4.12" // SBT
ivy"com.databricks::sjsonnet:0.4.12" // Mill
"com.databricks" %% "sjsonnet" % "0.4.12.1" // SBT
ivy"com.databricks::sjsonnet:0.4.12.1" // Mill
```

```scala
Expand All @@ -48,10 +48,10 @@ sjsonnet.SjsonnetMain.main0(

As a standalone executable assembly:

- <https://github.com/databricks/sjsonnet/releases/download/0.4.12/sjsonnet.jar>
- <https://github.com/databricks/sjsonnet/releases/download/0.4.12.1/sjsonnet.jar>

```bash
$ curl -L https://github.com/databricks/sjsonnet/releases/download/0.4.12/sjsonnet-0.4.12.jar > sjsonnet.jar
$ curl -L https://github.com/databricks/sjsonnet/releases/download/0.4.12.1/sjsonnet-0.4.12.1.jar > sjsonnet.jar

$ chmod +x sjsonnet.jar

Expand All @@ -71,7 +71,7 @@ $ ./sjsonnet.jar foo.jsonnet
Or from Javascript:

```javascript
$ curl -L https://github.com/databricks/sjsonnet/releases/download/0.4.12/sjsonnet-0.4.12.js > sjsonnet.js
$ curl -L https://github.com/databricks/sjsonnet/releases/download/0.4.12/sjsonnet-0.4.12.1.js > sjsonnet.js

$ node

Expand Down Expand Up @@ -280,6 +280,9 @@ to ensure the output bytecode remains compatible with users on older JVMs.

## Changelog

### 0.4.12.1
- Fix a bug leading to the truncation of the data returned by `std.gzip` and `std.xz` [#221](https://github.com/databricks/sjsonnet/pull/221).

### 0.4.12
- Fix a bug introduced with 0.4.11 with synthetic paths [#215](https://github.com/databricks/sjsonnet/pull/215)
- Fix thread-safety bug in Obj.getAllKeys [#217](https://github.com/databricks/sjsonnet/pull/217)
Expand Down
5 changes: 3 additions & 2 deletions sjsonnet/src-jvm/sjsonnet/Platform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,13 @@ object Platform {
val gzip: GZIPOutputStream = new GZIPOutputStream(outputStream)
try {
gzip.write(b)
Base64.getEncoder.encodeToString(outputStream.toByteArray)
} finally {
gzip.close()
outputStream.close()
}
Base64.getEncoder.encodeToString(outputStream.toByteArray)
}

def gzipString(s: String): String = {
gzipBytes(s.getBytes())
}
Expand All @@ -37,11 +38,11 @@ object Platform {
val xz: XZOutputStream = new XZOutputStream(outputStream, new LZMA2Options(level))
try {
xz.write(b)
Base64.getEncoder.encodeToString(outputStream.toByteArray)
} finally {
xz.close()
outputStream.close()
}
Base64.getEncoder.encodeToString(outputStream.toByteArray)
}

def xzString(s: String, compressionLevel: Option[Int]): String = {
Expand Down
14 changes: 14 additions & 0 deletions sjsonnet/test/src-jvm/sjsonnet/StdGzipTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package sjsonnet

import sjsonnet.TestUtils.eval
import utest._

object StdGzipTests extends TestSuite {
val tests = Tests {
test("gzip"){
eval("""std.gzip([1, 2])""") ==> ujson.Str("H4sIAAAAAAAAAGNkAgCSQsy2AgAAAA==")
eval("""std.gzip("hi")""") ==> ujson.Str("H4sIAAAAAAAAAMvIBACsKpPYAgAAAA==")
}
}
}

8 changes: 4 additions & 4 deletions sjsonnet/test/src-jvm/sjsonnet/StdXzTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import TestUtils.eval
object StdXzTests extends TestSuite {
val tests = Tests {
test("xz"){
eval("""std.xz([1, 2])""")
eval("""std.xz("hi")""")
eval("""std.xz([1, 2], compressionLevel = 0)""")
eval("""std.xz("hi", compressionLevel = 1)""")
eval("""std.xz([1, 2])""") ==> ujson.Str("/Td6WFoAAATm1rRGAgAhARYAAAB0L+WjAQABAQIAAADRC9qlUgJ94gABGgLcLqV+H7bzfQEAAAAABFla")
eval("""std.xz("hi")""") ==> ujson.Str("/Td6WFoAAATm1rRGAgAhARYAAAB0L+WjAQABaGkAAAD+qTgRvMqlSAABGgLcLqV+H7bzfQEAAAAABFla")
eval("""std.xz([1, 2], compressionLevel = 0)""") ==> ujson.Str("/Td6WFoAAATm1rRGAgAhAQwAAACPmEGcAQABAQIAAADRC9qlUgJ94gABGgLcLqV+H7bzfQEAAAAABFla")
eval("""std.xz("hi", compressionLevel = 1)""") ==> ujson.Str("/Td6WFoAAATm1rRGAgAhARAAAACocI6GAQABaGkAAAD+qTgRvMqlSAABGgLcLqV+H7bzfQEAAAAABFla")
val ex = intercept[Exception] {
// Compression level 10 is invalid
eval("""std.xz("hi", 10)""")
Expand Down

0 comments on commit 2047a32

Please sign in to comment.