Skip to content

Commit 1c431f5

Browse files
SchoolGuymarv7000
andauthored
Add partial structure generation for "PATCH" (#35)
* Add partial structure generation for "PATCH" * Update version to 0.1.0-beta.17 --------- Co-authored-by: Marvin Friedrich <contact@marvinf.com>
1 parent e2eef0f commit 1c431f5

File tree

4 files changed

+29
-14
lines changed

4 files changed

+29
-14
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "thanix"
33
authors = ["Tiara Hock <tiara.dev@proton.me>"]
4-
version = "0.1.0-beta.16"
4+
version = "0.1.0-beta.17"
55
publish = true
66
edition = "2024"
77
description = "A yaml-to-rust code generator for generating Rust code from yaml config files e.g. as found in openAPI."
@@ -13,7 +13,7 @@ license = "GPL-3.0"
1313

1414
[dependencies]
1515
check_keyword = "0.2.0"
16-
clap = {version = "4.4.2", features = ["derive"]}
16+
clap = { version = "4.4.2", features = ["derive"] }
1717
convert_case = "0.6.0"
1818
openapiv3 = "2.0.0"
1919
serde = { version = "1.0.195", features = ["derive"] }

Thanix.changes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
------------------------------------------------------------------
2+
Mon Jun 03 13:45:00 UTC 2025 - Marvin Friedrich <contact@marvinf.com>
3+
4+
- 0.1.0_beta.17
5+
* Fix generation of PATCH structs
6+
17
------------------------------------------------------------------
28
Mon Jun 02 02:45:00 UTC 2025 - Marvin Friedrich <contact@marvinf.com>
39

Thanix.spec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818

1919
Name: Thanix
20-
Version: 0.1.0_beta.16
20+
Version: 0.1.0_beta.17
2121
Release: 0.1
2222
Summary: Rust to yaml code generator
2323
# FIXME: Select a correct license from https://github.com/openSUSE/spec-cleaner#spdx-licenses

src/structgen.rs

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,22 +56,31 @@ pub fn generate(name: &str, schema: &Schema, workaround_mode: bool) -> Option<St
5656
}
5757
}
5858
result += "\t";
59-
result += &format!("pub {}", &prop_name.clone().into_safe());
59+
60+
// Patch requests need to accept partial data.
61+
if name.starts_with("Patched") {
62+
result += "#[serde(skip_serializing_if = \"Option::is_none\")]\n\t";
63+
}
64+
65+
result += "pub ";
66+
result += &prop_name.clone().into_safe();
6067
result += ": ";
6168

6269
// The NetBox schema may be incorrect and we can't rely on what we get as a response.
6370
// Therefore, we must make every response field nullable, even if it's technically not correct.
64-
if workaround_mode && !name.ends_with("Request") {
65-
if prop_name == "id" {
66-
result += &type_name;
67-
} else if !type_name.contains("Option<") && !result.ends_with("\tpub id:") {
68-
result += &format!("Option<{}>", type_name);
69-
} else {
70-
result += &type_name;
71-
}
71+
let typ = if name.starts_with("Patched") {
72+
format!("Option<{}>", type_name)
73+
} else if workaround_mode
74+
&& !name.ends_with("Request")
75+
&& !type_name.contains("Option")
76+
&& prop_name != "id"
77+
{
78+
format!("Option<{}>", type_name)
7279
} else {
73-
result += &type_name;
74-
}
80+
type_name
81+
};
82+
83+
result += &typ;
7584
result += ",\n";
7685
}
7786

0 commit comments

Comments
 (0)