Skip to content
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

introduce StringOrBool to workaround illegal implementation #84

Merged
merged 1 commit into from Sep 6, 2022
Merged

introduce StringOrBool to workaround illegal implementation #84

merged 1 commit into from Sep 6, 2022

Conversation

Justice4Joffrey
Copy link
Contributor

@Justice4Joffrey Justice4Joffrey commented Sep 5, 2022

This is a kludge I'm using to work with Apple OAuth. They send string bools for these fields, sadly.

It's not really a merge-worthy fix, but if you can suggest a more appropriate way of integrating this (I'm sure it's a large use-case for your consumers of this library), I'm happy to throw it together.

@Justice4Joffrey Justice4Joffrey changed the title introduce StringOrBool to workaround illegal implementation Draft: introduce StringOrBool to workaround illegal implementation Sep 5, 2022
@Justice4Joffrey Justice4Joffrey marked this pull request as draft September 5, 2022 18:27
@Justice4Joffrey Justice4Joffrey changed the title Draft: introduce StringOrBool to workaround illegal implementation introduce StringOrBool to workaround illegal implementation Sep 5, 2022
@ramosbugs
Copy link
Owner

hey, thanks for the PR!

In order to avoid introducing a breaking change, I'd suggest only changing the Deserialize implementation, rather than the actual field type. Also, for this crate to remain standards-compliant by default, relaxations like this should be behind a non-default feature flag.

For this change, let's follow the example of the accept-rfc3339-timestamps feature flag:

openidconnect-rs/src/types.rs

Lines 1192 to 1216 in 769f796

pub mod serde_utc_seconds {
use crate::types::Timestamp;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let seconds: Timestamp = Deserialize::deserialize(deserializer)?;
super::timestamp_to_utc(&seconds).map_err(|_| {
serde::de::Error::custom(format!(
"failed to parse `{}` as UTC datetime (in seconds)",
seconds
))
})
}
pub fn serialize<S>(v: &DateTime<Utc>, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
super::utc_to_seconds(v).serialize(serializer)
}
}

Internally, it uses a Timestamp type that deserializes from either an integer or a string depending on whether the feature flag is enabled:

openidconnect-rs/src/types.rs

Lines 1004 to 1010 in 769f796

#[derive(Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub(crate) enum Timestamp {
Seconds(serde_json::Number),
#[cfg(feature = "accept-rfc3339-timestamps")]
Rfc3339(String),
}

In this case, however, we should only need to specify a deserialize_with annotation since the underlying type (bool) already matches the Serialize implementation we want (vs. needing to convert a chrono::DateTime back to seconds).

@Justice4Joffrey
Copy link
Contributor Author

Thanks for the pointers. Might be some stylistic things to clear up, but think this is largely what you were suggesting.

Let me know!

@Justice4Joffrey Justice4Joffrey marked this pull request as ready for review September 6, 2022 18:20
@codecov
Copy link

codecov bot commented Sep 6, 2022

Codecov Report

Merging #84 (ac12554) into main (d676faa) will decrease coverage by 0.07%.
The diff coverage is 53.33%.

@@            Coverage Diff             @@
##             main      #84      +/-   ##
==========================================
- Coverage   72.28%   72.21%   -0.08%     
==========================================
  Files          16       16              
  Lines        3753     3779      +26     
==========================================
+ Hits         2713     2729      +16     
- Misses       1040     1050      +10     
Impacted Files Coverage Δ
src/claims.rs 37.03% <0.00%> (ø)
src/macros.rs 17.36% <14.28%> (+0.04%) ⬆️
src/types.rs 69.83% <71.42%> (+0.12%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@ramosbugs
Copy link
Owner

thanks! looks ready to merge once the 1.45.0 build passes. thanks for adding a test! feel free to ignore the codecov target

@ramosbugs ramosbugs merged commit 2f79ac2 into ramosbugs:main Sep 6, 2022
@Justice4Joffrey Justice4Joffrey deleted the string-or-bool branch September 8, 2022 09:54
@ramosbugs
Copy link
Owner

This has now been released in 2.4.0!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants