Skip to content

Commit

Permalink
fix(XkcdResponse): fix link field deserialization
Browse files Browse the repository at this point in the history
Change the type of the `link` field from `Url` to `String`. This is necessary
because `Url` does not deserialize "" properly. Replacing `link` with the type
`Option<Url>` was also not feasible as that does not fix the issue.

Added test for deserializing `XkcdResponse` strings with a blank `link` field.

BREAKING CHANGE: `link` field type was changed from `Url` to `String`.
  • Loading branch information
indiv0 committed Jun 25, 2016
1 parent 4b98224 commit 825017b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/model.in.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub struct XkcdResponse {
pub num: u32,
/// A URL to use for in the anchor tag of the hyperlink surrounding the
/// image, if the image is a hyperlinked one.
pub link: Url,
pub link: String,
/// The year the comic was published in.
pub year: u32,
/// News or updates regarding the comic.
Expand Down
41 changes: 36 additions & 5 deletions src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
//! For more information, see [XKCD's API
//! documentation](https://xkcd.com/json.html).

use url::Url;

#[cfg(feature = "nightly")]
include!("model.in.rs");

Expand All @@ -23,12 +21,12 @@ include!(concat!(env!("OUT_DIR"), "/model.rs"));
#[cfg(test)]
mod tests {
use serde_json::from_str;
use url::Url;

use super::XkcdResponse;
use super::super::parse_xkcd_response;

#[test]
fn test_xkcd_response_deserialize() {
fn test_parse_xkcd_response() {
let result = r#"
{
"month": "9",
Expand All @@ -48,7 +46,7 @@ mod tests {
assert_eq!(response, XkcdResponse {
month: 9,
num: 1572,
link: "http://goo.gl/forms/pj0OhX6wfO".to_owned().parse::<Url>().unwrap(),
link: "http://goo.gl/forms/pj0OhX6wfO".to_owned(),
year: 2015,
news: "".to_owned(),
safe_title: "xkcd Survey".to_owned(),
Expand All @@ -59,4 +57,37 @@ mod tests {
day: 1,
});
}

#[test]
fn test_parse_xkcd_response_no_link() {
let result = r#"
{
"month": "6",
"num": 1698,
"link": "",
"year": "2016",
"news": "",
"safe_title": "Theft Quadrants",
"transcript": "",
"alt": "TinyURL was the most popular link shortener for long enough that it made it into a lot of printed publications. I wonder what year the domain will finally lapse and get picked up by a porn site.",
"img": "http:\/\/imgs.xkcd.com\/comics\/theft_quadrants.png",
"title": "Theft Quadrants",
"day": "24"
}
"#;
let response = parse_xkcd_response::<XkcdResponse>(&result).unwrap();
assert_eq!(response, XkcdResponse {
month: 6,
num: 1698,
link: "".to_owned(),
year: 2016,
news: "".to_owned(),
safe_title: "Theft Quadrants".to_owned(),
transcript: "".to_owned(),
alt: "TinyURL was the most popular link shortener for long enough that it made it into a lot of printed publications. I wonder what year the domain will finally lapse and get picked up by a porn site.".to_owned(),
img: "http://imgs.xkcd.com/comics/theft_quadrants.png".to_owned(),
title: "Theft Quadrants".to_owned(),
day: 24,
});
}
}

0 comments on commit 825017b

Please sign in to comment.