Skip to content
This repository was archived by the owner on Apr 4, 2023. It is now read-only.

Commit 4e4d8df

Browse files
bors[bot]irevoire
andauthored
Merge #772
772: Throw an error on unknown fields specified in the _geo field r=irevoire a=irevoire Fix parts of meilisearch/meilisearch#3414 Co-authored-by: Tamo <tamo@meilisearch.com>
2 parents 1c4b1b3 + de3c4f1 commit 4e4d8df

File tree

4 files changed

+41
-0
lines changed

4 files changed

+41
-0
lines changed

milli/src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,8 @@ only composed of alphanumeric characters (a-z A-Z 0-9), hyphens (-) and undersco
154154
pub enum GeoError {
155155
#[error("The `_geo` field in the document with the id: `{document_id}` is not an object. Was expecting an object with the `_geo.lat` and `_geo.lng` fields but instead got `{value}`.")]
156156
NotAnObject { document_id: Value, value: Value },
157+
#[error("The `_geo` field in the document with the id: `{document_id}` contains the following unexpected fields: `{value}`.")]
158+
UnexpectedExtraFields { document_id: Value, value: Value },
157159
#[error("Could not find latitude nor longitude in the document with the id: `{document_id}`. Was expecting `_geo.lat` and `_geo.lng` fields.")]
158160
MissingLatitudeAndLongitude { document_id: Value },
159161
#[error("Could not find latitude in the document with the id: `{document_id}`. Was expecting a `_geo.lat` field.")]

milli/src/index.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,4 +2326,35 @@ pub(crate) mod tests {
23262326

23272327
db_snap!(index, geo_faceted_documents_ids); // ensure that no more document was inserted
23282328
}
2329+
2330+
#[test]
2331+
fn unexpected_extra_fields_in_geo_field() {
2332+
let index = TempIndex::new();
2333+
2334+
index
2335+
.update_settings(|settings| {
2336+
settings.set_primary_key("id".to_string());
2337+
settings.set_filterable_fields(HashSet::from(["_geo".to_string()]));
2338+
})
2339+
.unwrap();
2340+
2341+
let err = index
2342+
.add_documents(
2343+
documents!({ "id" : "doggo", "_geo": { "lat": 1, "lng": 2, "doggo": "are the best" }}),
2344+
)
2345+
.unwrap_err();
2346+
insta::assert_display_snapshot!(err, @r###"The `_geo` field in the document with the id: `"\"doggo\""` contains the following unexpected fields: `{"doggo":"are the best"}`."###);
2347+
2348+
db_snap!(index, geo_faceted_documents_ids); // ensure that no documents were inserted
2349+
2350+
// multiple fields and complex values
2351+
let err = index
2352+
.add_documents(
2353+
documents!({ "id" : "doggo", "_geo": { "lat": 1, "lng": 2, "doggo": "are the best", "and": { "all": ["cats", { "are": "beautiful" } ] } } }),
2354+
)
2355+
.unwrap_err();
2356+
insta::assert_display_snapshot!(err, @r###"The `_geo` field in the document with the id: `"\"doggo\""` contains the following unexpected fields: `{"and":{"all":["cats",{"are":"beautiful"}]},"doggo":"are the best"}`."###);
2357+
2358+
db_snap!(index, geo_faceted_documents_ids); // ensure that no documents were inserted
2359+
}
23292360
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
source: milli/src/index.rs
3+
---
4+
[]

milli/src/update/index_documents/enrich.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,10 @@ pub fn validate_geo_from_json(id: &DocumentId, bytes: &[u8]) -> Result<StdResult
379379
Value::Object(mut object) => match (object.remove("lat"), object.remove("lng")) {
380380
(Some(lat), Some(lng)) => {
381381
match (extract_finite_float_from_value(lat), extract_finite_float_from_value(lng)) {
382+
(Ok(_), Ok(_)) if !object.is_empty() => Ok(Err(UnexpectedExtraFields {
383+
document_id: debug_id(),
384+
value: object.into(),
385+
})),
382386
(Ok(_), Ok(_)) => Ok(Ok(())),
383387
(Err(value), Ok(_)) => Ok(Err(BadLatitude { document_id: debug_id(), value })),
384388
(Ok(_), Err(value)) => Ok(Err(BadLongitude { document_id: debug_id(), value })),

0 commit comments

Comments
 (0)