Skip to content

Commit 1252bc1

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

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.Test;
16+
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
19+
class ReadJsonAsBinaryTest extends AbstractClientTest {
20+
21+
@Test
22+
void test() {
23+
try (DatabaseClient client = Common.newClient()) {
24+
final String uri = "/a.json";
25+
writeJsonAsBinary(uri, client);
26+
27+
GenericDocumentManager docManager = client.newDocumentManager();
28+
29+
// A multipart request returns the correct document format.
30+
try (DocumentPage page = docManager.read(uri)) {
31+
DocumentRecord record = page.next();
32+
assertEquals(uri, record.getUri());
33+
assertEquals("application/json", record.getMimetype());
34+
assertEquals(Format.BINARY, record.getFormat());
35+
}
36+
37+
// But a request for a single URI does not!
38+
try (InputStreamHandle handle = docManager.read(uri, new InputStreamHandle())) {
39+
assertEquals("application/json", handle.getMimetype());
40+
assertEquals(Format.JSON, handle.getFormat(),
41+
"This is not correct! This is identifying a bug in the server where a GET with a 'uri' query " +
42+
"parameter and a 'category' parameter is not returning the correct document type. This is " +
43+
"evident in the response header which is 'vnd.marklogic.document-format: json' instead of " +
44+
"'vnd.marklogic.document-format: binary'. Will update this soon with a Jira ID for the " +
45+
"server bug.");
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)