Skip to content

Commit 90a6c55

Browse files
✨ add missing accessors & fix broken accessor (#368)
1 parent 2201478 commit 90a6c55

8 files changed

Lines changed: 108 additions & 29 deletions

File tree

src/Mindee/Parsing/V2/Field/DynamicFieldJsonConverter.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public override DynamicField Read(ref Utf8JsonReader reader, Type typeToConvert,
3636
var listField = new ListField(confidence);
3737
foreach (var item in itemsArray)
3838
{
39-
listField.Items.Add(item.Deserialize<DynamicField>());
39+
listField.Items.Add(item.Deserialize<DynamicField>(options));
4040
}
4141

4242
field = new DynamicField(
@@ -49,14 +49,14 @@ public override DynamicField Read(ref Utf8JsonReader reader, Type typeToConvert,
4949
nestedFieldsNode is JsonObject)
5050
{
5151
field = new DynamicField(FieldType.ObjectField,
52-
objectField: jsonObject.Deserialize<ObjectField>());
52+
objectField: jsonObject.Deserialize<ObjectField>(options));
5353
}
5454
// -------- SIMPLE OBJECT --------
5555
else if (jsonObject != null && jsonObject.ContainsKey("value"))
5656
{
5757
field = new DynamicField(
5858
FieldType.SimpleField,
59-
jsonObject.Deserialize<SimpleField>());
59+
jsonObject.Deserialize<SimpleField>(options));
6060
}
6161
else
6262
{

src/Mindee/Parsing/V2/Field/ObjectField.cs

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ namespace Mindee.Parsing.V2.Field
99
public class ObjectField : BaseField
1010
{
1111
private Dictionary<string, SimpleField> _simpleFields;
12+
private Dictionary<string, ListField> _listFields;
13+
private Dictionary<string, ObjectField> _objectFields;
1214

1315
/// <summary>
1416
/// Object field.
@@ -34,21 +36,92 @@ public Dictionary<string, SimpleField> SimpleFields
3436
{
3537
get
3638
{
37-
if (_simpleFields != null)
39+
if (this._simpleFields != null) return this._simpleFields;
40+
41+
this._simpleFields = new Dictionary<string, SimpleField>();
42+
foreach (var currentField in Fields)
3843
{
39-
return _simpleFields;
44+
// Strict Check: Only add if the Type says it's a SimpleField
45+
if (currentField.Value.Type == FieldType.SimpleField && currentField.Value.SimpleField != null)
46+
{
47+
this._simpleFields.Add(currentField.Key, currentField.Value.SimpleField);
48+
}
4049
}
50+
return this._simpleFields;
51+
}
52+
}
4153

42-
_simpleFields = new Dictionary<string, SimpleField>();
54+
/// <summary>
55+
/// List sub-fields of the field.
56+
/// </summary>
57+
public Dictionary<string, ListField> ListFields
58+
{
59+
get
60+
{
61+
if (this._listFields != null) return this._listFields;
62+
63+
this._listFields = new Dictionary<string, ListField>();
4364
foreach (var currentField in Fields)
4465
{
45-
_simpleFields.Add(currentField.Key, currentField.Value.SimpleField);
66+
if (currentField.Value.Type == FieldType.ListField && currentField.Value.ListField != null)
67+
{
68+
this._listFields.Add(currentField.Key, currentField.Value.ListField);
69+
}
4670
}
71+
return this._listFields;
72+
}
73+
}
4774

48-
return _simpleFields;
75+
/// <summary>
76+
/// Object sub-fields of the field.
77+
/// </summary>
78+
public Dictionary<string, ObjectField> ObjectFields
79+
{
80+
get
81+
{
82+
if (this._objectFields != null) return this._objectFields;
83+
84+
this._objectFields = new Dictionary<string, ObjectField>();
85+
foreach (var currentField in Fields)
86+
{
87+
if (currentField.Value.Type == FieldType.ObjectField && currentField.Value.ObjectField != null)
88+
{
89+
this._objectFields.Add(currentField.Key, currentField.Value.ObjectField);
90+
}
91+
}
92+
return this._objectFields;
4993
}
5094
}
5195

96+
/// <summary>
97+
/// Retrieves a sub-field from the fields object as a SimpleField.
98+
/// </summary>
99+
/// <param name="fieldName">Name of the field to retrieve.</param>
100+
/// <returns>A SimpleField instance, if found.</returns>
101+
/// <exception cref="KeyNotFoundException"></exception>
102+
public SimpleField GetSimpleField(string fieldName) => SimpleFields.TryGetValue(fieldName, out var value)
103+
? value
104+
: throw new KeyNotFoundException();
105+
106+
/// <summary>
107+
/// Retrieves a sub-field from the fields object as a ListField.
108+
/// </summary>
109+
/// <param name="fieldName">Name of the field to retrieve.</param>
110+
/// <returns>A ListField instance, if found.</returns>
111+
/// <exception cref="KeyNotFoundException"></exception>
112+
public ListField GetListField(string fieldName) => ListFields.TryGetValue(fieldName, out var value)
113+
? value
114+
: throw new KeyNotFoundException();
115+
116+
/// <summary>
117+
/// Retrieves a sub-field from the fields object as an ObjectField.
118+
/// </summary>
119+
/// <param name="fieldName">Name of the field to retrieve.</param>
120+
/// <returns>An ObjectField instance, if found.</returns>
121+
/// <exception cref="KeyNotFoundException"></exception>
122+
public ObjectField GetObjectField(string fieldName) => ObjectFields.TryGetValue(fieldName, out var value)
123+
? value
124+
: throw new KeyNotFoundException();
52125

53126
/// <summary>
54127
/// Print the value for all fields.

tests/Mindee.IntegrationTests/V2/MindeeClientV2Test.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ public async Task DataSchemaOverride_OverridesTheDataSchemaSuccessfully()
250250
var inputSource = new LocalInputSource(
251251
Constants.RootDir + "file_types/pdf/blank_1.pdf");
252252
var dataSchemaContents =
253-
File.ReadAllText(Constants.V2RootDir + "inference/data_schema_replace_param.json");
253+
File.ReadAllText(Constants.V2RootDir + "products/extraction/data_schema_replace_param.json");
254254
var inferenceParams = new InferenceParameters(
255255
_findocModelId,
256256
dataSchema: dataSchemaContents);

tests/Mindee.UnitTests/V2/Input/InferenceParameterTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Mindee.UnitTests.V2.Input
88
[Trait("Category", "V2")]
99
public class InferenceParameterTest
1010
{
11-
private const string ReplacePath = Constants.V2RootDir + "inference/data_schema_replace_param.json";
11+
private const string ReplacePath = Constants.V2RootDir + "products/extraction/data_schema_replace_param.json";
1212
private readonly Dictionary<string, object> DataSchemaDict;
1313
private readonly DataSchema DataSchemaInstance;
1414
private readonly string DataSchemaString;

tests/Mindee.UnitTests/V2/Input/LocalResponseTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ namespace Mindee.UnitTests.V2.Input
88
public class LocalResponseTest
99
{
1010
// File which the signature applies to.
11-
private const string FilePath = Constants.V2RootDir + "inference/standard_field_types.json";
11+
private const string FilePath = Constants.V2RootDir + "products/extraction/standard_field_types.json";
1212

1313
private static void AssertLocalResponse(LocalResponse localResponse)
1414
{
1515
// Fake secret key.
1616
const string secretKey = "ogNjY44MhvKPGTtVsI8zG82JqWQa68woYQH";
1717

1818
// Real signature using fake secret key.
19-
const string signature = "1df388c992d87897fe61dfc56c444c58fc3c7369c31e2b5fd20d867695e93e85";
19+
const string signature = "e51bdf80f1a08ed44ee161100fc30a25cb35b4ede671b0a575dc9064a3f5dbf1";
2020
Assert.False(localResponse.IsValidHmacSignature(
2121
secretKey, "invalid signature is invalid"));
2222
Assert.Equal(signature, localResponse.GetHmacSignature(secretKey));

tests/Mindee.UnitTests/V2/MindeeClientTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public async Task Document_GetJob_Async()
7575
public void Inference_LoadsLocally()
7676
{
7777
var localResponse = new LocalResponse(
78-
new FileInfo(Constants.V2RootDir + "products/financial_document/complete.json"));
78+
new FileInfo(Constants.V2RootDir + "products/extraction/financial_document/complete.json"));
7979
var locallyLoadedResponse = localResponse.DeserializeResponse<InferenceResponse>();
8080
Assert.NotNull(locallyLoadedResponse);
8181
Assert.Equal("12345678-1234-1234-1234-123456789abc", locallyLoadedResponse.Inference.Model.Id);

tests/Mindee.UnitTests/V2/Parsing/InferenceTest.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class InferenceTest
1212
[Fact]
1313
public void FinancialDocument_WhenEmpty_MustHaveValidProperties()
1414
{
15-
var response = GetInference("products/financial_document/blank.json");
15+
var response = GetInference("products/extraction/financial_document/blank.json");
1616
AssertInferenceResponse(response);
1717

1818
var fields = response.Inference.Result.Fields;
@@ -51,7 +51,7 @@ public void FinancialDocument_WhenEmpty_MustHaveValidProperties()
5151
[Fact]
5252
public void FinancialDocument_WhenComplete_MustHaveValidProperties()
5353
{
54-
var response = GetInference("products/financial_document/complete.json");
54+
var response = GetInference("products/extraction/financial_document/complete.json");
5555
AssertInferenceResponse(response);
5656

5757
var activeOptions = response.Inference.ActiveOptions;
@@ -87,7 +87,7 @@ public void FinancialDocument_WhenComplete_MustHaveValidProperties()
8787
[Fact(DisplayName = "deep_nested_fields.json – all nested structures must be typed correctly")]
8888
public void DeepNestedFields_mustExposeCorrectTypes()
8989
{
90-
var response = GetInference("inference/deep_nested_fields.json");
90+
var response = GetInference("products/extraction/deep_nested_fields.json");
9191
AssertInferenceResponse(response);
9292

9393
var inference = response.Inference;
@@ -97,6 +97,12 @@ public void DeepNestedFields_mustExposeCorrectTypes()
9797
Assert.NotNull(fields["field_object"].ObjectField);
9898

9999
var fieldObject = fields["field_object"].ObjectField!;
100+
Assert.Single(fieldObject.SimpleFields);
101+
Assert.Single(fieldObject.ListFields);
102+
Assert.Single(fieldObject.ObjectFields);
103+
Assert.Equal("value_2", fieldObject.GetSimpleField("sub_object_simple").Value);
104+
Assert.Equal(2, fieldObject.GetListField("sub_object_list").Items.Count);
105+
Assert.NotEmpty(fieldObject.GetObjectField("sub_object_object").Fields);
100106
var lvl1 = fieldObject.Fields;
101107
Assert.NotNull(lvl1["sub_object_list"].ListField);
102108
Assert.NotNull(lvl1["sub_object_object"].ObjectField);
@@ -118,7 +124,7 @@ public void DeepNestedFields_mustExposeCorrectTypes()
118124
[Fact(DisplayName = "standard_field_types.json – file metadata must be recognised")]
119125
public void StandardFieldTypes_mustExposeFileValues()
120126
{
121-
var response = GetInference("inference/standard_field_types.json");
127+
var response = GetInference("products/extraction/standard_field_types.json");
122128
AssertInferenceResponse(response);
123129

124130
var inference = response.Inference;
@@ -140,7 +146,7 @@ public void StandardFieldTypes_mustExposeFileValues()
140146
[Fact(DisplayName = "standard_field_types.json – simple fields must be recognised")]
141147
public void StandardFieldTypes_mustExposeSimpleFieldValues()
142148
{
143-
var response = GetInference("inference/standard_field_types.json");
149+
var response = GetInference("products/extraction/standard_field_types.json");
144150
AssertInferenceResponse(response);
145151

146152
var inference = response.Inference;
@@ -181,7 +187,7 @@ public void StandardFieldTypes_mustExposeSimpleFieldValues()
181187
[Fact(DisplayName = "standard_field_types.json – simple list fields must be recognised")]
182188
public void StandardFieldTypes_mustExposeSimpleListFieldValues()
183189
{
184-
var response = GetInference("inference/standard_field_types.json");
190+
var response = GetInference("products/extraction/standard_field_types.json");
185191
AssertInferenceResponse(response);
186192

187193
var inference = response.Inference;
@@ -203,7 +209,7 @@ public void StandardFieldTypes_mustExposeSimpleListFieldValues()
203209
[Fact(DisplayName = "standard_field_types.json – object fields must be recognised")]
204210
public void StandardFieldTypes_mustExposeObjectFieldValues()
205211
{
206-
var response = GetInference("inference/standard_field_types.json");
212+
var response = GetInference("products/extraction/standard_field_types.json");
207213
AssertInferenceResponse(response);
208214

209215
var inference = response.Inference;
@@ -228,7 +234,7 @@ public void StandardFieldTypes_mustExposeObjectFieldValues()
228234
[Fact(DisplayName = "standard_field_types.json – simple list fields must be recognised")]
229235
public void StandardFieldTypes_mustExposeObjectListFieldValues()
230236
{
231-
var response = GetInference("inference/standard_field_types.json");
237+
var response = GetInference("products/extraction/standard_field_types.json");
232238
AssertInferenceResponse(response);
233239

234240
var inference = response.Inference;
@@ -262,7 +268,7 @@ public void StandardFieldTypes_mustExposeObjectListFieldValues()
262268
[Fact(DisplayName = "standard_field_types.json - locations must be recognised")]
263269
public void StandardFieldTypes_mustHaveLocations()
264270
{
265-
var response = GetInference("inference/standard_field_types.json");
271+
var response = GetInference("products/extraction/standard_field_types.json");
266272
AssertInferenceResponse(response);
267273

268274
var inference = response.Inference;
@@ -285,9 +291,9 @@ public void StandardFieldTypes_mustHaveLocations()
285291
public void RstDisplay_mustBeAccessible()
286292
{
287293
// Arrange
288-
var resp = GetInference("inference/standard_field_types.json");
294+
var resp = GetInference("products/extraction/standard_field_types.json");
289295
var rstReference = File.ReadAllText(
290-
Constants.V2RootDir + "inference/standard_field_types.rst");
296+
Constants.V2RootDir + "products/extraction/standard_field_types.rst");
291297

292298
var inf = resp.Inference;
293299

@@ -302,7 +308,7 @@ public void RstDisplay_mustBeAccessible()
302308
[Fact]
303309
public void RawText_whenActivated_mustExposeProperties()
304310
{
305-
var response = GetInference("inference/raw_texts.json");
311+
var response = GetInference("products/extraction/raw_texts.json");
306312
AssertInferenceResponse(response);
307313

308314
var activeOptions = response.Inference.ActiveOptions;
@@ -324,14 +330,14 @@ public void RawText_whenActivated_mustExposeProperties()
324330

325331
Assert.Equal("This is the raw text of the first page...", rawText.Pages[0].Content);
326332
Assert.Equal(
327-
File.ReadAllText(Constants.V2RootDir + "inference/raw_texts.txt"),
333+
File.ReadAllText(Constants.V2RootDir + "products/extraction/raw_texts.txt"),
328334
rawText.ToString());
329335
}
330336

331337
[Fact]
332338
public void Rag_whenMatched_mustExposeProperties()
333339
{
334-
var response = GetInference("inference/rag_matched.json");
340+
var response = GetInference("products/extraction/rag_matched.json");
335341
AssertInferenceResponse(response);
336342

337343
var activeOptions = response.Inference.ActiveOptions;
@@ -349,7 +355,7 @@ public void Rag_whenMatched_mustExposeProperties()
349355
[Fact]
350356
public void Rag_whenNotMatched_mustExposeProperties()
351357
{
352-
var response = GetInference("inference/rag_not_matched.json");
358+
var response = GetInference("products/extraction/rag_not_matched.json");
353359
AssertInferenceResponse(response);
354360

355361
var activeOptions = response.Inference.ActiveOptions;

0 commit comments

Comments
 (0)