Skip to content

Commit 5e00335

Browse files
committed
MLE-27078 Added incorrect test to capture doc format error
1 parent b721b71 commit 5e00335

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

.copyrightconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ startyear: 2010
1111
# - Dotfiles already skipped automatically
1212
# Enable by removing the leading '# ' from the next line and editing values.
1313
# filesexcluded: third_party/*, docs/generated/*.md, assets/*.png, scripts/temp_*.py, vendor/lib.js
14-
filesexcluded: .github/*, README.md, Jenkinsfile, gradle/*, docker-compose.yaml, docker-compose.yml, *.gradle, gradle.properties, gradlew, gradlew.bat, **/test/resources/**, *.md, pom.xml, *.properties, *.json, *.xml, CODEOWNERS, *.txt
14+
filesexcluded: .github/*, README.md, Jenkinsfile, gradle/*, docker-compose.yaml, docker-compose.yml, *.gradle, gradle.properties, gradlew, gradlew.bat, **/test/resources/**, *.md, pom.xml, *.properties, *.json, *.xml, CODEOWNERS, *.txt, *.xqy
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright (c) 2010-2026 Progress Software Corporation and/or its subsidiaries or affiliates. All Rights Reserved.
3+
*/
4+
package com.marklogic.client.test.document;
5+
6+
import com.fasterxml.jackson.databind.ObjectMapper;
7+
import com.marklogic.client.DatabaseClient;
8+
import com.marklogic.client.document.*;
9+
import com.marklogic.client.io.DocumentMetadataHandle;
10+
import com.marklogic.client.io.Format;
11+
import com.marklogic.client.io.InputStreamHandle;
12+
import com.marklogic.client.io.JacksonHandle;
13+
import com.marklogic.client.test.AbstractClientTest;
14+
import com.marklogic.client.test.Common;
15+
import org.junit.jupiter.api.Disabled;
16+
import org.junit.jupiter.api.Test;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
class ReadJsonAsBinaryTest extends AbstractClientTest {
21+
22+
@Test
23+
@Disabled("See MLE-27191 for description of the server bug")
24+
void test() {
25+
try (DatabaseClient client = Common.newClient()) {
26+
final String uri = "/a.json";
27+
writeJsonAsBinary(uri, client);
28+
29+
GenericDocumentManager docManager = client.newDocumentManager();
30+
31+
// A multipart request returns the correct document format.
32+
try (DocumentPage page = docManager.read(uri)) {
33+
DocumentRecord record = page.next();
34+
assertEquals(uri, record.getUri());
35+
assertEquals("application/json", record.getMimetype());
36+
assertEquals(Format.BINARY, record.getFormat());
37+
}
38+
39+
// But a request for a single URI does not!
40+
try (InputStreamHandle handle = docManager.read(uri, new InputStreamHandle())) {
41+
assertEquals("application/json", handle.getMimetype());
42+
assertEquals(Format.BINARY, handle.getFormat(), "Unfortunately due to MLE-27191, the " +
43+
"format is JSON instead of binary. So this assertion will fail until that bug is fixed. " +
44+
"For a Java Client user, the workaround is to use the read method above and get a DocumentPage " +
45+
"back, as a multipart response seems to identify the correct headers.");
46+
}
47+
48+
}
49+
}
50+
51+
private void writeJsonAsBinary(String uri, DatabaseClient client) {
52+
JSONDocumentManager jsonDocManager = client.newJSONDocumentManager();
53+
DocumentMetadataHandle metadata = new DocumentMetadataHandle()
54+
.withPermission("rest-reader", DocumentMetadataHandle.Capability.READ, DocumentMetadataHandle.Capability.UPDATE);
55+
56+
jsonDocManager.setWriteTransform(new ServerTransform("toBinary"));
57+
jsonDocManager.write(uri, metadata, new JacksonHandle(new ObjectMapper().createObjectNode().put("a", 1)));
58+
}
59+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
xquery version "1.0-ml";
2+
3+
module namespace transform = "http://marklogic.com/rest-api/transform/toBinary";
4+
5+
(:
6+
Demonstrates how a transform can be used to store JSON or XML as a binary document. This is useful for a use case of
7+
a document with a URI that ends in e.g. ".json" or ".xml" but the user wants to treat the content as binary data so
8+
it is not indexed.
9+
:)
10+
declare function transform($context as map:map, $params as map:map, $content as document-node()) as document-node()
11+
{
12+
let $node := $content/node()
13+
let $enc := xdmp:base64-encode(xdmp:quote($node))
14+
let $bin := xs:hexBinary(xs:base64Binary($enc))
15+
return document { binary { $bin } }
16+
};

0 commit comments

Comments
 (0)