Skip to content

Commit

Permalink
handle IpV4 serialization case
Browse files Browse the repository at this point in the history
  • Loading branch information
PSeitz committed Oct 7, 2022
1 parent b2ca83a commit 5c9cbee
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 10 deletions.
4 changes: 1 addition & 3 deletions src/schema/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ impl Document {

/// Adding a facet to the document.
pub fn add_facet<F>(&mut self, field: Field, path: F)
where
Facet: From<F>,
{
where Facet: From<F> {
let facet = Facet::from(path);
let value = Value::Facet(facet);
self.add_field_value(field, value);
Expand Down
35 changes: 35 additions & 0 deletions src/schema/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -619,12 +619,14 @@ mod tests {
schema_builder.add_text_field("title", TEXT);
schema_builder.add_text_field("author", STRING);
schema_builder.add_u64_field("count", count_options);
schema_builder.add_ip_addr_field("ip", FAST | STORED);
schema_builder.add_bool_field("is_read", is_read_options);
let schema = schema_builder.build();
let doc_json = r#"{
"title": "my title",
"author": "fulmicoton",
"count": 4,
"ip": "127.0.0.1",
"is_read": true
}"#;
let doc = schema.parse_document(doc_json).unwrap();
Expand All @@ -633,6 +635,39 @@ mod tests {
assert_eq!(doc, doc_serdeser);
}

#[test]
pub fn test_document_to_ipv4_json() {
let mut schema_builder = Schema::builder();
schema_builder.add_ip_addr_field("ip", FAST | STORED);
let schema = schema_builder.build();

// IpV4 loopback
let doc_json = r#"{
"ip": "127.0.0.1"
}"#;
let doc = schema.parse_document(doc_json).unwrap();
let value: serde_json::Value = serde_json::from_str(&schema.to_json(&doc)).unwrap();
assert_eq!(value["ip"][0], "127.0.0.1");

// Special case IpV6 loopback. We don't want to map that to IPv4
let doc_json = r#"{
"ip": "::1"
}"#;
let doc = schema.parse_document(doc_json).unwrap();

let value: serde_json::Value = serde_json::from_str(&schema.to_json(&doc)).unwrap();
assert_eq!(value["ip"][0], "::1");

// testing ip address of every router in the world
let doc_json = r#"{
"ip": "192.168.0.1"
}"#;
let doc = schema.parse_document(doc_json).unwrap();

let value: serde_json::Value = serde_json::from_str(&schema.to_json(&doc)).unwrap();
assert_eq!(value["ip"][0], "192.168.0.1");
}

#[test]
pub fn test_document_from_nameddoc() {
let mut schema_builder = Schema::builder();
Expand Down
17 changes: 10 additions & 7 deletions src/schema/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ impl Eq for Value {}

impl Serialize for Value {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
where S: Serializer {
match *self {
Value::Str(ref v) => serializer.serialize_str(v),
Value::PreTokStr(ref v) => v.serialize(serializer),
Expand All @@ -55,16 +53,21 @@ impl Serialize for Value {
Value::Facet(ref facet) => facet.serialize(serializer),
Value::Bytes(ref bytes) => serializer.serialize_bytes(bytes),
Value::JsonObject(ref obj) => obj.serialize(serializer),
Value::IpAddr(ref obj) => obj.serialize(serializer),
Value::IpAddr(ref obj) => {
// Ensure IpV4 addresses get serialized as IpV4, but excluding IpV6 loopback.
if let Some(ip_v4) = obj.to_ipv4_mapped() {
ip_v4.serialize(serializer)
} else {
obj.serialize(serializer)
}
}
}
}
}

impl<'de> Deserialize<'de> for Value {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
where D: Deserializer<'de> {
struct ValueVisitor;

impl<'de> Visitor<'de> for ValueVisitor {
Expand Down

0 comments on commit 5c9cbee

Please sign in to comment.