diff options
-rw-r--r-- | generator/src/main.rs | 1 | ||||
-rw-r--r-- | generator/src/methods.rs | 1 | ||||
-rw-r--r-- | generator/src/structs.rs | 66 | ||||
-rw-r--r-- | src/generated/structs.rs | 26 | ||||
-rw-r--r-- | swagger.v1.json | 98 |
5 files changed, 153 insertions, 39 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<url::Url>" { - 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<time::OffsetDateTime>" { - 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::<url::Url>().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::<f32>().map_err(|_| StructureError::HeaderParseFailed)") diff --git a/src/generated/structs.rs b/src/generated/structs.rs index 7399273..b99c2a0 100644 --- a/src/generated/structs.rs +++ b/src/generated/structs.rs @@ -2848,8 +2848,7 @@ pub struct WikiPage { pub html_url: Option<url::Url>, pub last_commit: Option<WikiCommit>, pub sidebar: Option<String>, - #[serde(deserialize_with = "crate::none_if_blank_url")] - pub sub_url: Option<url::Url>, + pub sub_url: Option<String>, pub title: Option<String>, } @@ -2859,8 +2858,7 @@ pub struct WikiPageMetaData { #[serde(deserialize_with = "crate::none_if_blank_url")] pub html_url: Option<url::Url>, pub last_commit: Option<WikiCommit>, - #[serde(deserialize_with = "crate::none_if_blank_url")] - pub sub_url: Option<url::Url>, + pub sub_url: Option<String>, pub title: Option<String>, } @@ -3009,7 +3007,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for RegistrationTokenHeaders { pub struct ErrorHeaders { pub message: Option<String>, - pub url: Option<String>, + pub url: Option<url::Url>, } impl TryFrom<&reqwest::header::HeaderMap> for ErrorHeaders { @@ -3027,7 +3025,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::<url::Url>() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) @@ -3036,7 +3035,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for ErrorHeaders { pub struct ForbiddenHeaders { pub message: Option<String>, - pub url: Option<String>, + pub url: Option<url::Url>, } impl TryFrom<&reqwest::header::HeaderMap> for ForbiddenHeaders { @@ -3054,7 +3053,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::<url::Url>() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) @@ -3093,7 +3093,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for InvalidTopicsErrorHeaders { pub struct RepoArchivedErrorHeaders { pub message: Option<String>, - pub url: Option<String>, + pub url: Option<url::Url>, } impl TryFrom<&reqwest::header::HeaderMap> for RepoArchivedErrorHeaders { @@ -3111,7 +3111,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::<url::Url>() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) @@ -3120,7 +3121,7 @@ impl TryFrom<&reqwest::header::HeaderMap> for RepoArchivedErrorHeaders { pub struct ValidationErrorHeaders { pub message: Option<String>, - pub url: Option<String>, + pub url: Option<url::Url>, } impl TryFrom<&reqwest::header::HeaderMap> for ValidationErrorHeaders { @@ -3138,7 +3139,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::<url::Url>() + .map_err(|_| StructureError::HeaderParseFailed) }) .transpose()?; Ok(Self { message, url }) diff --git a/swagger.v1.json b/swagger.v1.json index a32491a..4fe69c0 100644 --- a/swagger.v1.json +++ b/swagger.v1.json @@ -16930,6 +16930,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17021,6 +17022,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "workflow_id": { @@ -17242,6 +17244,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "verification": { @@ -17264,6 +17267,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17275,6 +17279,7 @@ "properties": { "browser_download_url": { "type": "string", + "format": "url", "x-go-name": "DownloadURL" }, "created_at": { @@ -17606,6 +17611,7 @@ }, "contents_url": { "type": "string", + "format": "url", "x-go-name": "ContentsURL" }, "deletions": { @@ -17619,6 +17625,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "previous_filename": { @@ -17627,6 +17634,7 @@ }, "raw_url": { "type": "string", + "format": "url", "x-go-name": "RawURL" }, "status": { @@ -17642,6 +17650,7 @@ "properties": { "commit_url": { "type": "string", + "format": "url", "x-go-name": "CommitURL" }, "repository": { @@ -17668,6 +17677,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17695,6 +17705,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -17704,6 +17715,7 @@ }, "issue_url": { "type": "string", + "format": "url", "x-go-name": "IssueURL" }, "original_author": { @@ -17717,6 +17729,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "PRURL" }, "updated_at": { @@ -17757,6 +17770,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "parents": { @@ -17775,6 +17789,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17827,6 +17842,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17884,6 +17900,7 @@ }, "target_url": { "type": "string", + "format": "url", "x-go-name": "TargetURL" }, "updated_at": { @@ -17893,6 +17910,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -17956,6 +17974,7 @@ }, "download_url": { "type": "string", + "format": "url", "x-go-name": "DownloadURL" }, "encoding": { @@ -17965,10 +17984,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 +18016,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 +18031,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -18341,7 +18364,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } }, "x-go-package": "code.gitea.io/gitea/modules/structs" @@ -18839,6 +18863,7 @@ }, "target_url": { "type": "string", + "format": "url", "x-go-name": "TargetURL" } }, @@ -19179,6 +19204,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20067,6 +20093,7 @@ "external_tracker_url": { "description": "URL of external issue tracker.", "type": "string", + "format": "url", "x-go-name": "ExternalTrackerURL" } }, @@ -20079,6 +20106,7 @@ "external_wiki_url": { "description": "URL of external wiki.", "type": "string", + "format": "url", "x-go-name": "ExternalWikiURL" } }, @@ -20101,6 +20129,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "message": { @@ -20123,6 +20152,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20491,6 +20521,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20523,6 +20554,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20561,6 +20593,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20597,6 +20630,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20676,6 +20710,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -20766,6 +20801,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -20830,6 +20866,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "user": { @@ -20868,6 +20905,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21060,6 +21098,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21112,6 +21151,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21131,6 +21171,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21553,14 +21594,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 +21619,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21610,6 +21655,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -21671,6 +21717,7 @@ "properties": { "avatar_url": { "type": "string", + "format": "url", "x-go-name": "AvatarURL" }, "description": { @@ -21786,6 +21833,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -21897,6 +21945,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "verification": { @@ -22007,6 +22056,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "user": { @@ -22072,6 +22122,7 @@ }, "diff_url": { "type": "string", + "format": "url", "x-go-name": "DiffURL" }, "draft": { @@ -22087,6 +22138,7 @@ "$ref": "#/definitions/PRBranchInfo" }, "html_url": { + "format": "url", "type": "string", "x-go-name": "HTMLURL" }, @@ -22140,6 +22192,7 @@ }, "patch_url": { "type": "string", + "format": "url", "x-go-name": "PatchURL" }, "pin_order": { @@ -22174,6 +22227,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "user": { @@ -22195,6 +22249,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "merged": { @@ -22232,6 +22287,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22245,6 +22301,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "HTMLPullURL" }, "stale": { @@ -22296,6 +22353,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22328,6 +22386,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "HTMLPullURL" }, "resolver": { @@ -22438,6 +22497,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -22479,6 +22539,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22505,6 +22566,7 @@ }, "tarball_url": { "type": "string", + "format": "url", "x-go-name": "TarURL" }, "target_commitish": { @@ -22513,13 +22575,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 +22656,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "verification": { @@ -22673,10 +22739,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 +22828,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -22784,6 +22853,7 @@ }, "languages_url": { "type": "string", + "format": "url", "x-go-name": "LanguagesURL" }, "link": { @@ -22822,6 +22892,7 @@ }, "original_url": { "type": "string", + "format": "url", "x-go-name": "OriginalURL" }, "owner": { @@ -22852,6 +22923,7 @@ }, "ssh_url": { "type": "string", + "format": "ssh-url", "x-go-name": "SSHURL" }, "stars_count": { @@ -22877,6 +22949,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" }, "watchers_count": { @@ -23055,10 +23128,12 @@ }, "tarball_url": { "type": "string", + "format": "url", "x-go-name": "TarballURL" }, "zipball_url": { "type": "string", + "format": "url", "x-go-name": "ZipballURL" } }, @@ -23221,6 +23296,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -23230,6 +23306,7 @@ }, "issue_url": { "type": "string", + "format": "url", "x-go-name": "IssueURL" }, "label": { @@ -23269,6 +23346,7 @@ }, "pull_request_url": { "type": "string", + "format": "url", "x-go-name": "PRURL" }, "ref_action": { @@ -23531,6 +23609,7 @@ "avatar_url": { "description": "URL to the user's avatar", "type": "string", + "format": "url", "x-go-name": "AvatarURL" }, "created": { @@ -23567,6 +23646,7 @@ "html_url": { "description": "URL to the user's gitea page", "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "id": { @@ -23782,6 +23862,7 @@ }, "repository_url": { "type": "string", + "format": "url", "x-go-name": "RepositoryURL" }, "subscribed": { @@ -23790,6 +23871,7 @@ }, "url": { "type": "string", + "format": "url", "x-go-name": "URL" } }, @@ -23855,6 +23937,7 @@ }, "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "last_commit": { @@ -23881,6 +23964,7 @@ "properties": { "html_url": { "type": "string", + "format": "url", "x-go-name": "HTMLURL" }, "last_commit": { @@ -24876,7 +24960,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } }, @@ -24887,7 +24972,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } }, @@ -24924,7 +25010,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } }, @@ -24941,7 +25028,8 @@ "type": "string" }, "url": { - "type": "string" + "type": "string", + "format": "url" } } } |