Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve validation in uuid unmarshaller #2569

Merged
merged 1 commit into from
Jul 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class UnmarshallingSpec extends FreeSpec with Matchers with BeforeAndAfterAll wi
val uuid = UUID.randomUUID()
Unmarshal(uuid.toString).to[UUID] should evaluateTo(uuid)
}
"uuidUnmarshaller should unmarshal nil uuid" in {
Unmarshal("00000000-0000-0000-0000-000000000000").to[UUID] should evaluateTo(UUID.fromString("00000000-0000-0000-0000-000000000000"))
}
}

"The GenericUnmarshallers" - {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,17 @@ trait PredefinedFromStringUnmarshallers {
}
}

implicit val uuidFromStringUnmarshaller: Unmarshaller[String, UUID] =
Unmarshaller.strict { string =>
try UUID.fromString(string)
catch {
case e: IllegalArgumentException =>
throw new IllegalArgumentException(s"'$string' is not a valid UUID value", e)
}
implicit val uuidFromStringUnmarshaller: Unmarshaller[String, UUID] = {
val validUuidPattern =
"[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[1-5][0-9a-fA-F]{3}-[89abAB][0-9a-fA-F]{3}-[0-9a-fA-F]{12}|00000000-0000-0000-0000-000000000000".r.pattern

Unmarshaller.strict[String, UUID] { string =>
if (validUuidPattern.matcher(string).matches)
UUID.fromString(string)
else
throw new IllegalArgumentException(s"'$string' is not a valid UUID value")
}
}

implicit def CsvSeq[T](implicit unmarshaller: Unmarshaller[String, T]): Unmarshaller[String, immutable.Seq[T]] =
Unmarshaller.strict[String, immutable.Seq[String]] { string =>
Expand Down