-
Notifications
You must be signed in to change notification settings - Fork 63
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
allOf with overlapping schemas fails to deserialize #370
Comments
I think I'd ideally like #[derive(Clone, Debug, Deserialize, Serialize)]
pub struct JsonResponseBase {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub result: Option<String>,
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct JsonSuccessBase {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ignored_parameters_unsupported: Option<IgnoredParametersUnsupported>,
pub msg: String,
pub result: JsonSuccessBaseResult,
}
#[derive(Clone, Copy, Debug, Deserialize, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize)]
pub enum JsonSuccessBaseResult {
#[serde(rename = "success")]
Success,
}
impl From<JsonSuccessBase> for JsonResponseBase {
fn from(_: JsonSuccessBase) -> Self {
Self {
result: Some("success".to_string()),
}
}
} With regard to #[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct JsonSuccess {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub ignored_parameters_unsupported: Option<IgnoredParametersUnsupported>,
pub msg: String,
pub result: JsonSuccessBaseResult,
}
impl From<JsonSuccessBase> for JsonSuccess {
fn from(value: JsonSuccessBase) {
let JsonSuccessBase { ignored_parameters_unsupported, msg, result };
Self { ignored_parameters_unsupported, msg, result }
}
} Any thoughts on the proposed generation above? I think this is another case relevant to #176. |
Makes sense to me. |
Before this PR, the handling of allOf was basically wrong. It worked in a small number of cases, but on the whole it was flawed in concept. Most of the time, it would result in a struct with all subschemas embedded with a #[serde(flatten)] directive. This was broken in many ways, but in particular because allOf constructs are often used to augment, narrow, and expand other types. This PR instead "merges" the subschemas of an allOf construction. This results in many more types, but also (per our testing), types that compile and are usable which was often not the case previously. This implements much of what is discussed in #176, but doesn't include the impl From that issue imagines to go from the "merged" type to the component type (i.e. the type that appeared in the allOf array). Fixes #315 and #370
Thanks! |
If you give it another shot please let me know if the new code improves things. |
Tried it and haven't hit any of the deserialization failures I hit previously. |
For example the following schemas derived from Zulip's api:
would result in the following code:
(some parts omitted for brevity)
This fails to deserialize
{ "result": "success", "msg": "" }
asJsonResponseBase
already consumed theresult
thatJsonSuccessBaseSubtype1
wants to read too.Another pattern used by Zulip is:
results in
which duplicates all fields in
JsonSuccessSubtype1
despite them only being present there to avoid disallowing theJsonSuccessBase
fields from existing.For this pattern separate from the deserialization issue it would be nice if
JsonSuccessSubtype1
could be flattened intoJsonSuccess
.The text was updated successfully, but these errors were encountered: