From 960892050e4725e1abd6c83b3eb0452396bf5ae9 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Wed, 10 Jul 2024 20:29:42 -0400 Subject: refactor!: use `format` field instead of names for special parsing --- generator/src/main.rs | 1 + generator/src/methods.rs | 1 + generator/src/structs.rs | 66 ++++++++++++++++++++++----------- src/generated/structs.rs | 23 +++++++----- swagger.v1.json | 96 +++++++++++++++++++++++++++++++++++++++++++++--- 5 files changed, 150 insertions(+), 37 deletions(-) diff --git a/generator/src/main.rs b/generator/src/main.rs index f69df2d..b0570d2 100644 --- a/generator/src/main.rs +++ b/generator/src/main.rs @@ -75,6 +75,7 @@ fn schema_type_name( Primitive::String if schema._enum.is_none() => match schema.format.as_deref() { Some("date") => "time::Date", Some("date-time") => "time::OffsetDateTime", + Some("url" | "ssh-url") => "url::Url", _ => "String", } .to_string(), diff --git a/generator/src/methods.rs b/generator/src/methods.rs index 7245247..141fc7d 100644 --- a/generator/src/methods.rs +++ b/generator/src/methods.rs @@ -209,6 +209,7 @@ pub fn param_type_inner( ParameterType::String => match format.as_deref() { Some("date") => "time::Date", Some("date-time") => "time::OffsetDateTime", + Some("url" | "ssh-url") => "url::Url", _ => { if owned { "String" diff --git a/generator/src/structs.rs b/generator/src/structs.rs index 4543f5c..428bb1d 100644 --- a/generator/src/structs.rs +++ b/generator/src/structs.rs @@ -70,33 +70,50 @@ pub fn create_struct_for_definition( crate::schema_subtype_name(spec, name, prop_name, value, &mut field_ty)?; crate::schema_subtypes(spec, name, prop_name, value, &mut subtypes)?; } - if field_name.ends_with("url") && field_ty == "String" { - field_ty = "url::Url".into() + let is_required = required.contains(prop_name); + if let Some(format) = &prop_schema.deref(spec)?.format { + match &**format { + "url" => { + if !is_required { + fields.push_str( + "#[serde(deserialize_with = \"crate::none_if_blank_url\")]\n", + ); + } + } + "ssh-url" => { + if is_required { + fields.push_str( + "#[serde(deserialize_with = \"crate::deserialize_ssh_url\")]\n", + ); + } else { + fields.push_str( + "#[serde(deserialize_with = \"crate::deserialize_optional_ssh_url\")]\n", + ); + } + } + "date-time" => { + if is_required { + fields.push_str("#[serde(with = \"time::serde::rfc3339\")]\n"); + } else { + fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); + } + } + "date" => { + if is_required { + fields.push_str("#[serde(with = \"time::serde::rfc3339\")]\n"); + } else { + fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); + } + } + _ => (), + } } if field_ty == name { field_ty = format!("Box<{field_ty}>") } - if !required.contains(prop_name) { + if !is_required { field_ty = format!("Option<{field_ty}>") } - if field_ty == "Option" { - if field_name == "ssh_url" { - fields.push_str( - "#[serde(deserialize_with = \"crate::deserialize_optional_ssh_url\")]\n", - ); - } else { - fields.push_str("#[serde(deserialize_with = \"crate::none_if_blank_url\")]\n"); - } - } - if field_ty == "url::Url" && field_name == "ssh_url" { - fields.push_str("#[serde(deserialize_with = \"crate::deserialize_ssh_url\")]\n"); - } - if field_ty == "time::OffsetDateTime" { - fields.push_str("#[serde(with = \"time::serde::rfc3339\")]\n"); - } - if field_ty == "Option" { - fields.push_str("#[serde(with = \"time::serde::rfc3339::option\")]\n"); - } if let Some(parse_method) = parse_with.get(prop_name) { fields.push_str("#[serde(deserialize_with = \"crate::"); fields.push_str(parse_method); @@ -539,7 +556,12 @@ fn create_header_struct( ) .unwrap(); match &header._type { - ParameterType::String => imp.push_str("Ok(s.to_string())"), + ParameterType::String => match header.format.as_deref() { + Some("url" | "ssh-url") => imp.push_str( + "s.parse::().map_err(|_| StructureError::HeaderParseFailed)", + ), + _ => imp.push_str("Ok(s.to_string())"), + }, ParameterType::Number => match header.format.as_deref() { Some("float") => { imp.push_str("s.parse::().map_err(|_| StructureError::HeaderParseFailed)") diff --git a/src/generated/structs.rs b/src/generated/structs.rs index 7399273..0b62017 100644 --- a/src/generated/structs.rs +++ b/src/generated/structs.rs @@ -413,8 +413,7 @@ pub struct CommitStatus { pub description: Option, pub id: Option, pub status: Option, - #[serde(deserialize_with = "crate::none_if_blank_url")] - pub target_url: Option, + pub target_url: Option, #[serde(with = "time::serde::rfc3339::option")] pub updated_at: Option, #[serde(deserialize_with = "crate::none_if_blank_url")] @@ -3009,7 +3008,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for RegistrationTokenHeaders { pub struct ErrorHeaders { pub message: Option, - pub url: Option, + pub url: Option, } impl TryFrom<&reqwest::header::HeaderMap> for ErrorHeaders { @@ -3027,7 +3026,8 @@ impl TryFrom<&reqwest::header::HeaderMap> for ErrorHeaders { .get("url") .map(|s| -> Result<_, _> { let s = s.to_str().map_err(|_| StructureError::HeaderNotAscii)?; - Ok(s.to_string()) + s.parse::() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) @@ -3036,7 +3036,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for ErrorHeaders { pub struct ForbiddenHeaders { pub message: Option, - pub url: Option, + pub url: Option, } impl TryFrom<&reqwest::header::HeaderMap> for ForbiddenHeaders { @@ -3054,7 +3054,8 @@ impl TryFrom<&reqwest::header::HeaderMap> for ForbiddenHeaders { .get("url") .map(|s| -> Result<_, _> { let s = s.to_str().map_err(|_| StructureError::HeaderNotAscii)?; - Ok(s.to_string()) + s.parse::() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) @@ -3093,7 +3094,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for InvalidTopicsErrorHeaders { pub struct RepoArchivedErrorHeaders { pub message: Option, - pub url: Option, + pub url: Option, } impl TryFrom<&reqwest::header::HeaderMap> for RepoArchivedErrorHeaders { @@ -3111,7 +3112,8 @@ impl TryFrom<&reqwest::header::HeaderMap> for RepoArchivedErrorHeaders { .get("url") .map(|s| -> Result<_, _> { let s = s.to_str().map_err(|_| StructureError::HeaderNotAscii)?; - Ok(s.to_string()) + s.parse::() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) @@ -3120,7 +3122,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for RepoArchivedErrorHeaders { pub struct ValidationErrorHeaders { pub message: Option, - pub url: Option, + pub url: Option, } impl TryFrom<&reqwest::header::HeaderMap> for ValidationErrorHeaders { @@ -3138,7 +3140,8 @@ impl TryFrom<&reqwest::header::HeaderMap> for ValidationErrorHeaders { .get("url") .map(|s| -> Result<_, _> { let s = s.to_str().map_err(|_| StructureError::HeaderNotAscii)?; - Ok(s.to_string()) + s.parse::() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) diff --git a/swagger.v1.json b/swagger.v1.json index a32491a..18c780a 100644 --- a/swagger.v1.json +++ b/swagger.v1.json @@ -16930,6 +16930,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17242,6 +17243,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "verification": { @@ -17264,6 +17266,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17275,6 +17278,7 @@ "properties": { "browser_download_url": { "type": "string", + "format": "url", "x-go-name": "DownloadURL" }, "created_at": { @@ -17606,6 +17610,7 @@ }, "contents_url": { "type": "string", + "format": "url", "x-go-name": "ContentsURL" }, "deletions": { @@ -17619,6 +17624,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "previous_filename": { @@ -17627,6 +17633,7 @@ }, "raw_url": { "type": "string", + "format": "url", "x-go-name": "RawURL" }, "status": { @@ -17642,6 +17649,7 @@ "properties": { "commit_url": { "type": "string", + "format": "url", "x-go-name": "CommitURL" }, "repository": { @@ -17668,6 +17676,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17695,6 +17704,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -17704,6 +17714,7 @@ }, "issue_url": { "type": "string", + "format": "url", "x-go-name": "IssueURL" }, "original_author": { @@ -17717,6 +17728,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "PRURL" }, "updated_at": { @@ -17757,6 +17769,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "parents": { @@ -17775,6 +17788,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17827,6 +17841,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17893,6 +17908,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17956,6 +17972,7 @@ }, "download_url": { "type": "string", + "format": "url", "x-go-name": "DownloadURL" }, "encoding": { @@ -17965,10 +17982,12 @@ }, "git_url": { "type": "string", + "format": "url", "x-go-name": "GitURL" }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "last_commit_sha": { @@ -17995,6 +18014,7 @@ "submodule_git_url": { "description": "`submodule_git_url` is populated when `type` is `submodule`, otherwise null", "type": "string", + "format": "url", "x-go-name": "SubmoduleGitURL" }, "target": { @@ -18009,6 +18029,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -18341,7 +18362,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } }, "x-go-package": "code.gitea.io/gitea/modules/structs" @@ -18839,6 +18861,7 @@ }, "target_url": { "type": "string", + "format": "url", "x-go-name": "TargetURL" } }, @@ -19179,6 +19202,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20067,6 +20091,7 @@ "external_tracker_url": { "description": "URL of external issue tracker.", "type": "string", + "format": "url", "x-go-name": "ExternalTrackerURL" } }, @@ -20079,6 +20104,7 @@ "external_wiki_url": { "description": "URL of external wiki.", "type": "string", + "format": "url", "x-go-name": "ExternalWikiURL" } }, @@ -20101,6 +20127,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "message": { @@ -20123,6 +20150,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20491,6 +20519,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20523,6 +20552,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20561,6 +20591,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20597,6 +20628,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20676,6 +20708,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20766,6 +20799,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -20830,6 +20864,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "user": { @@ -20868,6 +20903,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21060,6 +21096,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21112,6 +21149,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21131,6 +21169,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21553,14 +21592,17 @@ "properties": { "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "latest_comment_html_url": { "type": "string", + "format": "url", "x-go-name": "LatestCommentHTMLURL" }, "latest_comment_url": { "type": "string", + "format": "url", "x-go-name": "LatestCommentURL" }, "state": { @@ -21575,6 +21617,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21610,6 +21653,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21671,6 +21715,7 @@ "properties": { "avatar_url": { "type": "string", + "format": "url", "x-go-name": "AvatarURL" }, "description": { @@ -21786,6 +21831,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -21897,6 +21943,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "verification": { @@ -22007,6 +22054,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "user": { @@ -22072,6 +22120,7 @@ }, "diff_url": { "type": "string", + "format": "url", "x-go-name": "DiffURL" }, "draft": { @@ -22087,6 +22136,7 @@ "$ref": "#/definitions/PRBranchInfo" }, "html_url": { + "format": "url", "type": "string", "x-go-name": "HTMLURL" }, @@ -22140,6 +22190,7 @@ }, "patch_url": { "type": "string", + "format": "url", "x-go-name": "PatchURL" }, "pin_order": { @@ -22174,6 +22225,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "user": { @@ -22232,6 +22284,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22245,6 +22298,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "HTMLPullURL" }, "stale": { @@ -22296,6 +22350,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22328,6 +22383,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "HTMLPullURL" }, "resolver": { @@ -22438,6 +22494,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -22479,6 +22536,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22505,6 +22563,7 @@ }, "tarball_url": { "type": "string", + "format": "url", "x-go-name": "TarURL" }, "target_commitish": { @@ -22513,13 +22572,16 @@ }, "upload_url": { "type": "string", + "format": "url", "x-go-name": "UploadURL" }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "zipball_url": { + "format": "url", "type": "string", "x-go-name": "ZipURL" } @@ -22591,6 +22653,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "verification": { @@ -22673,10 +22736,12 @@ }, "avatar_url": { "type": "string", + "format": "url", "x-go-name": "AvatarURL" }, "clone_url": { "type": "string", + "format": "url", "x-go-name": "CloneURL" }, "created_at": { @@ -22760,6 +22825,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22784,6 +22850,7 @@ }, "languages_url": { "type": "string", + "format": "url", "x-go-name": "LanguagesURL" }, "link": { @@ -22822,6 +22889,7 @@ }, "original_url": { "type": "string", + "format": "url", "x-go-name": "OriginalURL" }, "owner": { @@ -22852,6 +22920,7 @@ }, "ssh_url": { "type": "string", + "format": "ssh-url", "x-go-name": "SSHURL" }, "stars_count": { @@ -22877,6 +22946,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "watchers_count": { @@ -23055,10 +23125,12 @@ }, "tarball_url": { "type": "string", + "format": "url", "x-go-name": "TarballURL" }, "zipball_url": { "type": "string", + "format": "url", "x-go-name": "ZipballURL" } }, @@ -23221,6 +23293,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -23230,6 +23303,7 @@ }, "issue_url": { "type": "string", + "format": "url", "x-go-name": "IssueURL" }, "label": { @@ -23269,6 +23343,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "PRURL" }, "ref_action": { @@ -23531,6 +23606,7 @@ "avatar_url": { "description": "URL to the user's avatar", "type": "string", + "format": "url", "x-go-name": "AvatarURL" }, "created": { @@ -23782,6 +23858,7 @@ }, "repository_url": { "type": "string", + "format": "url", "x-go-name": "RepositoryURL" }, "subscribed": { @@ -23790,6 +23867,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -23855,6 +23933,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "last_commit": { @@ -23866,6 +23945,7 @@ }, "sub_url": { "type": "string", + "format": "url", "x-go-name": "SubURL" }, "title": { @@ -23881,6 +23961,7 @@ "properties": { "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "last_commit": { @@ -23888,6 +23969,7 @@ }, "sub_url": { "type": "string", + "format": "url", "x-go-name": "SubURL" }, "title": { @@ -24876,7 +24958,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } }, @@ -24887,7 +24970,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } }, @@ -24924,7 +25008,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } }, @@ -24941,7 +25026,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } } -- cgit v1.2.3 From d707f98ec4167e462ca749942e0c5afdd039326d Mon Sep 17 00:00:00 2001 From: Cyborus Date: Sun, 4 Aug 2024 11:49:38 -0400 Subject: fix: `WikiPage{MetaData}`'s `sub_url` field isn't a url --- swagger.v1.json | 2 -- 1 file changed, 2 deletions(-) diff --git a/swagger.v1.json b/swagger.v1.json index 18c780a..0ad38a8 100644 --- a/swagger.v1.json +++ b/swagger.v1.json @@ -23945,7 +23945,6 @@ }, "sub_url": { "type": "string", - "format": "url", "x-go-name": "SubURL" }, "title": { @@ -23969,7 +23968,6 @@ }, "sub_url": { "type": "string", - "format": "url", "x-go-name": "SubURL" }, "title": { -- cgit v1.2.3 From c4220b67e2d19341d5786b1e6c0bccb48c93ccea Mon Sep 17 00:00:00 2001 From: Cyborus Date: Sun, 4 Aug 2024 12:08:11 -0400 Subject: fix: add url format to new definitions --- swagger.v1.json | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/swagger.v1.json b/swagger.v1.json index 0ad38a8..4fe69c0 100644 --- a/swagger.v1.json +++ b/swagger.v1.json @@ -17022,6 +17022,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "workflow_id": { @@ -17899,6 +17900,7 @@ }, "target_url": { "type": "string", + "format": "url", "x-go-name": "TargetURL" }, "updated_at": { @@ -22247,6 +22249,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "merged": { @@ -23643,6 +23646,7 @@ "html_url": { "description": "URL to the user's gitea page", "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { -- cgit v1.2.3 From 5796178ec2dfacfff9bd4e77f8c02b35036adad3 Mon Sep 17 00:00:00 2001 From: Cyborus Date: Sun, 4 Aug 2024 12:08:52 -0400 Subject: chore: generate --- src/generated/structs.rs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/src/generated/structs.rs b/src/generated/structs.rs index 0b62017..b99c2a0 100644 --- a/src/generated/structs.rs +++ b/src/generated/structs.rs @@ -413,7 +413,8 @@ pub struct CommitStatus { pub description: Option, pub id: Option, pub status: Option, - pub target_url: Option, + #[serde(deserialize_with = "crate::none_if_blank_url")] + pub target_url: Option, #[serde(with = "time::serde::rfc3339::option")] pub updated_at: Option, #[serde(deserialize_with = "crate::none_if_blank_url")] @@ -2847,8 +2848,7 @@ pub struct WikiPage { pub html_url: Option, pub last_commit: Option, pub sidebar: Option, - #[serde(deserialize_with = "crate::none_if_blank_url")] - pub sub_url: Option, + pub sub_url: Option, pub title: Option, } @@ -2858,8 +2858,7 @@ pub struct WikiPageMetaData { #[serde(deserialize_with = "crate::none_if_blank_url")] pub html_url: Option, pub last_commit: Option, - #[serde(deserialize_with = "crate::none_if_blank_url")] - pub sub_url: Option, + pub sub_url: Option, pub title: Option, } -- cgit v1.2.3