Skip to content

Commit d8d8ebe

Browse files
RUST-647 Sync spec tests for invalid relaxed UUIDs (#228)
1 parent 503eaa4 commit d8d8ebe

File tree

2 files changed

+17
-26
lines changed

2 files changed

+17
-26
lines changed

src/extjson/models.rs

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -173,42 +173,21 @@ pub(crate) struct Uuid {
173173
}
174174

175175
impl Uuid {
176-
pub(crate) fn parse(mut self) -> extjson::de::Result<crate::Binary> {
177-
if !valid_uuid_format(&self.value) {
178-
return Err(extjson::de::Error::invalid_value(
179-
Unexpected::Str(&self.value),
180-
&"$uuid values does not follow RFC 4122 format regarding length and hyphens",
181-
));
182-
}
183-
184-
self.value.retain(|c| c != '-');
185-
186-
let bytes = hex::decode(&self.value).map_err(|_| {
176+
pub(crate) fn parse(self) -> extjson::de::Result<crate::Binary> {
177+
let uuid = uuid::Uuid::parse_str(&self.value).map_err(|_| {
187178
extjson::de::Error::invalid_value(
188179
Unexpected::Str(&self.value),
189-
&"$uuid does not follow RFC 4122 format regarding hex bytes",
180+
&"$uuid value does not follow RFC 4122 format regarding length and hyphens",
190181
)
191182
})?;
192183

193184
Ok(crate::Binary {
194185
subtype: BinarySubtype::Uuid,
195-
bytes,
186+
bytes: uuid.as_bytes().to_vec(),
196187
})
197188
}
198189
}
199190

200-
fn valid_uuid_format(s: &str) -> bool {
201-
// RFC 4122 defines the hyphens in a UUID as appearing in the 8th, 13th, 18th, and 23rd
202-
// characters.
203-
//
204-
// See https://tools.ietf.org/html/rfc4122#section-3
205-
s.chars().count() == 36
206-
&& s.chars().nth(8) == Some('-')
207-
&& s.chars().nth(13) == Some('-')
208-
&& s.chars().nth(18) == Some('-')
209-
&& s.chars().nth(23) == Some('-')
210-
}
211-
212191
#[derive(Deserialize)]
213192
#[serde(deny_unknown_fields)]
214193
pub(crate) struct JavaScriptCodeWithScope {

src/tests/spec/json/bson-corpus/binary.json

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,20 @@
9494
"string": "{\"x\" : { \"$uuid\" : { \"data\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4\"}}}"
9595
},
9696
{
97-
"description": "$uuid invalid value",
97+
"description": "$uuid invalid value--too short",
9898
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-90e8-e7d1dfc035d4\"}}"
99+
},
100+
{
101+
"description": "$uuid invalid value--too long",
102+
"string": "{\"x\" : { \"$uuid\" : \"73ffd264-44b3-4c69-90e8-e7d1dfc035d4-789e4\"}}"
103+
},
104+
{
105+
"description": "$uuid invalid value--misplaced hyphens",
106+
"string": "{\"x\" : { \"$uuid\" : \"73ff-d26444b-34c6-990e8e-7d1dfc035d4\"}}"
107+
},
108+
{
109+
"description": "$uuid invalid value--too many hyphens",
110+
"string": "{\"x\" : { \"$uuid\" : \"----d264-44b3-4--9-90e8-e7d1dfc0----\"}}"
99111
}
100112
]
101113
}

0 commit comments

Comments
 (0)