Skip to content

Commit c063b6a

Browse files
✨ add easy accessor for list in object field (#293)
1 parent 9c25c12 commit c063b6a

File tree

10 files changed

+208
-42
lines changed

10 files changed

+208
-42
lines changed

.github/workflows/_build.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
java-version:
14+
- "21"
1415
- "11"
1516
- "8"
1617
distribution:

.github/workflows/_test-integrations.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ jobs:
1212
strategy:
1313
matrix:
1414
java-version:
15+
- "21"
1516
- "11"
1617
- "8"
1718
distribution:

pom.xml

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,20 @@
133133
<target>8</target>
134134
</configuration>
135135
</plugin>
136+
<plugin>
137+
<groupId>org.apache.maven.plugins</groupId>
138+
<artifactId>maven-surefire-plugin</artifactId>
139+
<version>${org.apache.maven.surfire.version}</version>
140+
<configuration>
141+
<includes>
142+
<include>**/*Test.java</include>
143+
</includes>
144+
<argLine>
145+
${surefire.addOpens}
146+
-Djunit.jupiter.extensions.autodetection.enabled=true
147+
</argLine>
148+
</configuration>
149+
</plugin>
136150
<plugin>
137151
<groupId>org.sonatype.central</groupId>
138152
<artifactId>central-publishing-maven-plugin</artifactId>
@@ -148,6 +162,22 @@
148162
</build>
149163

150164
<profiles>
165+
<profile>
166+
<id>jdk-9-plus</id>
167+
<activation>
168+
<jdk>[9,)</jdk>
169+
</activation>
170+
<properties>
171+
<surefire.addOpens>
172+
--add-opens java.base/java.lang=ALL-UNNAMED
173+
--add-opens java.base/java.util=ALL-UNNAMED
174+
--add-opens java.base/java.net=ALL-UNNAMED
175+
--add-opens java.base/sun.net.www.protocol.https=ALL-UNNAMED
176+
--add-opens java.base/java.io=ALL-UNNAMED
177+
--add-opens java.base/java.lang.reflect=ALL-UNNAMED
178+
</surefire.addOpens>
179+
</properties>
180+
</profile>
151181
<profile>
152182
<id>release</id>
153183
<build>
@@ -178,16 +208,6 @@
178208
</execution>
179209
</executions>
180210
</plugin>
181-
<plugin>
182-
<groupId>org.apache.maven.plugins</groupId>
183-
<artifactId>maven-surefire-plugin</artifactId>
184-
<version>${org.apache.maven.surfire.version}</version>
185-
<configuration>
186-
<includes>
187-
<include>**/*Test.java</include>
188-
</includes>
189-
</configuration>
190-
</plugin>
191211
<plugin>
192212
<groupId>org.apache.maven.plugins</groupId>
193213
<artifactId>maven-gpg-plugin</artifactId>
@@ -361,6 +381,18 @@
361381
<scope>test</scope>
362382
<version>${org.mockito.inline.version}</version>
363383
</dependency>
384+
<dependency>
385+
<groupId>net.bytebuddy</groupId>
386+
<artifactId>byte-buddy</artifactId>
387+
<version>1.15.11</version>
388+
<scope>test</scope>
389+
</dependency>
390+
<dependency>
391+
<groupId>net.bytebuddy</groupId>
392+
<artifactId>byte-buddy-agent</artifactId>
393+
<version>1.15.11</version>
394+
<scope>test</scope>
395+
</dependency>
364396
<dependency>
365397
<groupId>com.github.tomakehurst</groupId>
366398
<artifactId>wiremock-jre8</artifactId>
@@ -412,12 +444,13 @@
412444
<org.junit.platform.version>1.8.2</org.junit.platform.version>
413445
<org.hamcrest.version>2.2</org.hamcrest.version>
414446
<org.mapstruct.version>1.5.3.Final</org.mapstruct.version>
415-
<org.mockito.inline.version>4.6.1</org.mockito.inline.version>
416-
<org.mockito.junit.jupiter.version>4.5.1</org.mockito.junit.jupiter.version>
447+
<org.mockito.inline.version>4.11.0</org.mockito.inline.version>
448+
<org.mockito.junit.jupiter.version>4.11.0</org.mockito.junit.jupiter.version>
417449
<org.projectlombok.lombok-mapstruct-binding.version>0.2.0</org.projectlombok.lombok-mapstruct-binding.version>
418450
<org.projectlombok.version>1.18.32</org.projectlombok.version>
419451
<org.slf4j.version>2.0.17</org.slf4j.version>
420452
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
453+
<surefire.addOpens></surefire.addOpens>
421454
<wiremock.version>2.35.2</wiremock.version>
422455
</properties>
423456

src/main/java/com/mindee/parsing/v2/field/ObjectField.java

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,97 @@ public LinkedHashMap<String, SimpleField> getSimpleFields() throws IllegalStateE
4242
return simpleFields;
4343
}
4444

45+
/**
46+
* Retrieves all subfields from the {@code fields} map as a {@link LinkedHashMap} of
47+
* {@code ListField} objects, keyed by their field names.
48+
*
49+
* @return a {@link LinkedHashMap} containing the field names as keys and their corresponding
50+
* {@code ListField} instances as values (only includes fields that are list fields)
51+
*/
52+
public LinkedHashMap<String, ListField> getListFields() {
53+
LinkedHashMap<String, ListField> listFields = new LinkedHashMap<>();
54+
if (fields != null) {
55+
for (String fieldName : fields.keySet()) {
56+
DynamicField field = fields.get(fieldName);
57+
if (field != null && field.getType() == DynamicField.FieldType.LIST_FIELD) {
58+
listFields.put(fieldName, field.getListField());
59+
}
60+
}
61+
}
62+
return listFields;
63+
}
64+
65+
/**
66+
* Retrieves all subfields from the {@code fields} map as a {@link LinkedHashMap} of
67+
* {@code ObjectField} objects, keyed by their field names.
68+
*
69+
* @return a {@link LinkedHashMap} containing the field names as keys and their corresponding
70+
* {@code ObjectField} objects as values
71+
*/
72+
public LinkedHashMap<String, ObjectField> getObjectFields() {
73+
LinkedHashMap<String, ObjectField> objectFields = new LinkedHashMap<>();
74+
if (fields != null) {
75+
for (String fieldName : fields.keySet()) {
76+
DynamicField field = fields.get(fieldName);
77+
if (field != null && field.getType() == DynamicField.FieldType.OBJECT_FIELD) {
78+
objectFields.put(fieldName, field.getObjectField());
79+
}
80+
}
81+
}
82+
return objectFields;
83+
}
84+
85+
/**
86+
* Retrieves a single sub-field from the {@code fields} map as a {@link SimpleField}.
87+
*
88+
* @param fieldKey the name/key of the field to retrieve
89+
* @return the corresponding {@link SimpleField}, or {@code null} if {@code fields} is
90+
* {@code null}
91+
* or if no field exists for {@code fieldKey} (depending on {@code fields}' behavior)
92+
* @throws IllegalStateException if the referenced field exists but is not of type
93+
* {@code SIMPLE_FIELD}
94+
*/
95+
public SimpleField getSimpleField(String fieldKey) throws IllegalStateException {
96+
if (fields == null) {
97+
return null;
98+
}
99+
return fields.getSimpleField(fieldKey);
100+
}
101+
102+
/**
103+
* Retrieves a list sub-field from the {@code fields} map as a {@link ListField}.
104+
*
105+
* @param fieldKey the name/key of the field to retrieve
106+
* @return the corresponding {@link ListField}, or {@code null} if {@code fields} is
107+
* {@code null}
108+
* or if no field exists for {@code fieldKey} (depending on {@code fields}' behavior)
109+
* @throws IllegalStateException if the referenced field exists but is not of type
110+
* {@code LIST_FIELD}
111+
*/
112+
public ListField getListField(String fieldKey) {
113+
if (fields == null) {
114+
return null;
115+
}
116+
return fields.getListField(fieldKey);
117+
}
118+
119+
/**
120+
* Retrieves an object sub-field from the {@code fields} map as a {@link ObjectField}.
121+
*
122+
* @param fieldKey the name/key of the field to retrieve
123+
* @return the corresponding {@link ObjectField}, or {@code null} if {@code fields} is
124+
* {@code null}
125+
* or if no field exists for {@code fieldKey} (depending on {@code fields}' behavior)
126+
* @throws IllegalStateException if the referenced field exists but is not of type
127+
* {@code OBJECT_FIELD}
128+
*/
129+
public ObjectField getObjectField(String fieldKey) {
130+
if (fields == null) {
131+
return null;
132+
}
133+
return fields.getObjectField(fieldKey);
134+
}
135+
45136
@Override
46137
public String toString() {
47138
return "\n" + (fields != null ? fields.toString(1) : "");

src/test/java/com/mindee/MindeeClientV2IT.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ void parseFile_emptyMultiPage_mustSucceed() throws IOException, InterruptedExcep
8787
@DisplayName("Filled, single-page image – enqueue & parse must succeed")
8888
void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedException {
8989
LocalInputSource source = new LocalInputSource(
90-
getV2ResourcePath("products/financial_document/default_sample.jpg")
90+
getV2ResourcePath("products/extraction/financial_document/default_sample.jpg")
9191
);
9292

9393
InferenceParameters params = InferenceParameters
@@ -135,14 +135,16 @@ void parseFile_filledSinglePage_mustSucceed() throws IOException, InterruptedExc
135135
@DisplayName("Data Schema Replace – enqueue & parse must succeed")
136136
void parseFile_dataSchemaReplace_mustSucceed() throws IOException, InterruptedException {
137137
LocalInputSource source = new LocalInputSource(
138-
getV2ResourcePath("products/financial_document/default_sample.jpg")
138+
getV2ResourcePath("products/extraction/financial_document/default_sample.jpg")
139139
);
140140

141141
InferenceParameters params = InferenceParameters
142142
.builder(modelId)
143143
.rag(false)
144144
.alias("java-integration-test_data-schema-replace")
145-
.dataSchema(readFileAsString(getV2ResourcePath("inference/data_schema_replace_param.json")))
145+
.dataSchema(
146+
readFileAsString(getV2ResourcePath("products/extraction/data_schema_replace_param.json"))
147+
)
146148
.build();
147149

148150
InferenceResponse response = mindeeClient.enqueueAndGetInference(source, params);

src/test/java/com/mindee/MindeeClientV2Test.java

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
package com.mindee;
22

33
import static com.mindee.TestingUtilities.getResourcePath;
4-
import static org.junit.jupiter.api.Assertions.*;
5-
import static org.mockito.ArgumentMatchers.*;
6-
import static org.mockito.Mockito.*;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import static org.mockito.ArgumentMatchers.any;
7+
import static org.mockito.ArgumentMatchers.anyString;
8+
import static org.mockito.Mockito.atMostOnce;
9+
import static org.mockito.Mockito.verify;
10+
import static org.mockito.Mockito.when;
711

812
import com.fasterxml.jackson.core.JsonProcessingException;
913
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -87,7 +91,9 @@ void document_getInference_async() throws IOException {
8791
MindeeApiV2 predictable = Mockito.mock(MindeeApiV2.class);
8892

8993
String json = FileUtils
90-
.readFileToString(getResourcePath("v2/products/financial_document/complete.json").toFile());
94+
.readFileToString(
95+
getResourcePath("v2/products/extraction/financial_document/complete.json").toFile()
96+
);
9197

9298
ObjectMapper mapper = new ObjectMapper();
9399
mapper.findAndRegisterModules();
@@ -129,7 +135,7 @@ class DeserializeResponse {
129135
@DisplayName("parses local JSON and exposes correct field values")
130136
void inference_loadsLocally() throws IOException {
131137
LocalResponse localResponse = new LocalResponse(
132-
getResourcePath("v2/products/financial_document/complete.json")
138+
getResourcePath("v2/products/extraction/financial_document/complete.json")
133139
);
134140
InferenceResponse loaded = localResponse.deserializeResponse(InferenceResponse.class);
135141

src/test/java/com/mindee/MindeeSettingsTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class MindeeSettingsTest {
1111
@SetEnvironmentVariable(key = "MINDEE_API_URL", value = "https://example.com")
1212
void setEnvironmentVariablesAndEmptyParams() {
1313
MindeeSettings settings = new MindeeSettings("", "");
14-
Assertions.assertEquals(settings.getApiKey().orElse(""), "abcd");
15-
Assertions.assertEquals(settings.getBaseUrl(), "https://example.com");
14+
Assertions.assertEquals("abcd", settings.getApiKey().orElse(""));
15+
Assertions.assertEquals("https://example.com", settings.getBaseUrl());
1616
}
1717
}

src/test/java/com/mindee/input/LocalResponseV2Test.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ public class LocalResponseV2Test {
1919
/**
2020
* Real signature using fake secret key.
2121
*/
22-
String signature = "1df388c992d87897fe61dfc56c444c58fc3c7369c31e2b5fd20d867695e93e85";
22+
String signature = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1";
2323

2424
/**
2525
* File which the signature applies to.
2626
*/
27-
Path filePath = getV2ResourcePath("inference/standard_field_types.json");
27+
Path filePath = getV2ResourcePath("products/extraction/standard_field_types.json");
2828

2929
protected void assertLocalResponse(LocalResponse localResponse) {
3030
Assertions.assertNotNull(localResponse.getFile());

0 commit comments

Comments
 (0)