Skip to content

Commit

Permalink
chrono::Date<chrono::offset::Utc> does not support deserialization (#434
Browse files Browse the repository at this point in the history
)
  • Loading branch information
pcrumley authored Nov 7, 2023
1 parent 7ff1129 commit a128f3e
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,6 @@ The resulting types won't enforce those value constraints.

A string schema with `format` set to `uuid` will result in the `uuid::Uuid`
type; similarly, a `format` of `date` translates to
`chrono::Date<chrono::offset::Utc>`. For users that don't want dependencies on
`chrono::naive::NaiveDate`. For users that don't want dependencies on
`uuid` or `chrono` it would be useful for Typify to optionally represent those
as `String` (or as some other, consumer-specified type).
as `String` (or as some other, consumer-specified type).
2 changes: 1 addition & 1 deletion typify-impl/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ impl TypeSpace {
self.uses_chrono = true;
Ok((
TypeEntry::new_native(
"chrono::Date<chrono::offset::Utc>",
"chrono::naive::NaiveDate",
&[TypeSpaceImpl::Display, TypeSpaceImpl::FromStr],
),
metadata,
Expand Down
12 changes: 11 additions & 1 deletion typify/tests/schemas/maps.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"type": "string",
"format": "^a*$"
},
"MapWithDateKeys": {
"MapWithDateTimeKeys": {
"$comment": "test that a type isn't needed for propertyNames",
"type": "object",
"additionalProperties": {
Expand All @@ -27,6 +27,16 @@
"propertyNames": {
"format": "date-time"
}
},
"MapWithDateKeys": {
"$comment": "test that a type isn't needed for propertyNames",
"type": "object",
"additionalProperties": {
"$ref": "#/definitions/Value"
},
"propertyNames": {
"format": "date"
}
}
},
"$comment": "usual case of a map whose name must come from its title",
Expand Down
37 changes: 30 additions & 7 deletions typify/tests/schemas/maps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,29 +58,52 @@ impl ToString for Eh {
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MapWithDateKeys(
pub struct MapWithDateKeys(pub std::collections::HashMap<chrono::naive::NaiveDate, Value>);
impl std::ops::Deref for MapWithDateKeys {
type Target = std::collections::HashMap<chrono::naive::NaiveDate, Value>;
fn deref(&self) -> &std::collections::HashMap<chrono::naive::NaiveDate, Value> {
&self.0
}
}
impl From<MapWithDateKeys> for std::collections::HashMap<chrono::naive::NaiveDate, Value> {
fn from(value: MapWithDateKeys) -> Self {
value.0
}
}
impl From<&MapWithDateKeys> for MapWithDateKeys {
fn from(value: &MapWithDateKeys) -> Self {
value.clone()
}
}
impl From<std::collections::HashMap<chrono::naive::NaiveDate, Value>> for MapWithDateKeys {
fn from(value: std::collections::HashMap<chrono::naive::NaiveDate, Value>) -> Self {
Self(value)
}
}
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct MapWithDateTimeKeys(
pub std::collections::HashMap<chrono::DateTime<chrono::offset::Utc>, Value>,
);
impl std::ops::Deref for MapWithDateKeys {
impl std::ops::Deref for MapWithDateTimeKeys {
type Target = std::collections::HashMap<chrono::DateTime<chrono::offset::Utc>, Value>;
fn deref(&self) -> &std::collections::HashMap<chrono::DateTime<chrono::offset::Utc>, Value> {
&self.0
}
}
impl From<MapWithDateKeys>
impl From<MapWithDateTimeKeys>
for std::collections::HashMap<chrono::DateTime<chrono::offset::Utc>, Value>
{
fn from(value: MapWithDateKeys) -> Self {
fn from(value: MapWithDateTimeKeys) -> Self {
value.0
}
}
impl From<&MapWithDateKeys> for MapWithDateKeys {
fn from(value: &MapWithDateKeys) -> Self {
impl From<&MapWithDateTimeKeys> for MapWithDateTimeKeys {
fn from(value: &MapWithDateTimeKeys) -> Self {
value.clone()
}
}
impl From<std::collections::HashMap<chrono::DateTime<chrono::offset::Utc>, Value>>
for MapWithDateKeys
for MapWithDateTimeKeys
{
fn from(
value: std::collections::HashMap<chrono::DateTime<chrono::offset::Utc>, Value>,
Expand Down

0 comments on commit a128f3e

Please sign in to comment.