Skip to content

Commit

Permalink
feat: permissive deserialization (#505)
Browse files Browse the repository at this point in the history
* feat(core): permissive deserialization

* chore: update changelog
  • Loading branch information
gadomski authored Oct 30, 2024
1 parent af8af8b commit 921f4d7
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 4 deletions.
1 change: 1 addition & 0 deletions crates/core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Added

- `version` ([#476](https://github.com/stac-utils/stac-rs/pull/476))
- Permissive deserialization ([#505](https://github.com/stac-utils/stac-rs/pull/505))

### Removed

Expand Down
11 changes: 10 additions & 1 deletion crates/core/src/catalog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use serde_json::{Map, Value};
#[serde(tag = "type")]
pub struct Catalog {
/// The STAC version the `Catalog` implements.
#[serde(rename = "stac_version")]
#[serde(rename = "stac_version", default)]
pub version: Version,

/// A list of extension identifiers the `Catalog` implements.
Expand All @@ -28,6 +28,7 @@ pub struct Catalog {
pub extensions: Vec<String>,

/// Identifier for the `Catalog`.
#[serde(default)]
pub id: String,

/// A short descriptive one-line title for the `Catalog`.
Expand All @@ -37,9 +38,11 @@ pub struct Catalog {
/// Detailed multi-line description to fully explain the `Catalog`.
///
/// [CommonMark 0.29](http://commonmark.org/) syntax MAY be used for rich text representation.
#[serde(default)]
pub description: String,

/// A list of references to other documents.
#[serde(default)]
pub links: Vec<Link>,

/// Additional fields not part of the Catalog specification.
Expand Down Expand Up @@ -131,6 +134,7 @@ impl Migrate for Catalog {}
mod tests {
use super::Catalog;
use crate::STAC_VERSION;
use serde_json::json;

#[test]
fn new() {
Expand All @@ -157,4 +161,9 @@ mod tests {

roundtrip!(catalog, "examples/catalog.json", Catalog);
}

#[test]
fn permissive_deserialization() {
let _: Catalog = serde_json::from_value(json!({})).unwrap();
}
}
13 changes: 12 additions & 1 deletion crates/core/src/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const DEFAULT_LICENSE: &str = "proprietary";
#[serde(tag = "type")]
pub struct Collection {
/// The STAC version the `Collection` implements.
#[serde(rename = "stac_version")]
#[serde(rename = "stac_version", default)]
pub version: Version,

/// A list of extension identifiers the `Collection` implements.
Expand All @@ -35,6 +35,7 @@ pub struct Collection {
pub extensions: Vec<String>,

/// Identifier for the `Collection` that is unique across the provider.
#[serde(default)]
pub id: String,

/// A short descriptive one-line title for the `Collection`.
Expand All @@ -44,6 +45,7 @@ pub struct Collection {
/// Detailed multi-line description to fully explain the `Collection`.
///
/// [CommonMark 0.29](http://commonmark.org/) syntax MAY be used for rich text representation.
#[serde(default)]
pub description: String,

/// List of keywords describing the `Collection`.
Expand All @@ -53,6 +55,7 @@ pub struct Collection {
/// `Collection`'s license(s), either a SPDX [License
/// identifier](https://spdx.org/licenses/), `"various"` if multiple licenses
/// apply or `"proprietary"` for all other cases.
#[serde(default)]
pub license: String,

/// A list of [providers](Provider), which may include all organizations capturing or
Expand All @@ -64,6 +67,7 @@ pub struct Collection {
pub providers: Option<Vec<Provider>>,

/// Spatial and temporal extents.
#[serde(default)]
pub extent: Extent,

/// A map of property summaries, either a set of values, a range of values
Expand All @@ -72,6 +76,7 @@ pub struct Collection {
pub summaries: Option<Map<String, Value>>,

/// A list of references to other documents.
#[serde(default)]
pub links: Vec<Link>,

/// Dictionary of asset objects that can be downloaded, each with a unique key.
Expand Down Expand Up @@ -413,6 +418,7 @@ impl Migrate for Collection {}
#[cfg(test)]
mod tests {
use super::{Collection, Extent, Provider};
use serde_json::json;

mod collection {
use super::Collection;
Expand Down Expand Up @@ -537,4 +543,9 @@ mod tests {
Collection
);
}

#[test]
fn permissive_deserialization() {
let _: Collection = serde_json::from_value(json!({})).unwrap();
}
}
12 changes: 11 additions & 1 deletion crates/core/src/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const TOP_LEVEL_ATTRIBUTES: [&str; 8] = [
#[serde(tag = "type", rename = "Feature")]
pub struct Item {
/// The STAC version the `Item` implements.
#[serde(rename = "stac_version")]
#[serde(rename = "stac_version", default)]
pub version: Version,

/// A list of extensions the `Item` implements.
Expand All @@ -46,6 +46,7 @@ pub struct Item {
/// Provider identifier.
///
/// The ID should be unique within the [Collection](crate::Collection) that contains the `Item`.
#[serde(default)]
pub id: String,

/// Defines the full footprint of the asset represented by this item,
Expand All @@ -66,12 +67,15 @@ pub struct Item {
pub bbox: Option<Bbox>,

/// A dictionary of additional metadata for the `Item`.
#[serde(default)]
pub properties: Properties,

/// List of link objects to resources and related URLs.
#[serde(default)]
pub links: Vec<Link>,

/// Dictionary of asset objects that can be downloaded, each with a unique key.
#[serde(default)]
pub assets: HashMap<String, Asset>,

/// The `id` of the STAC [Collection](crate::Collection) this `Item`
Expand Down Expand Up @@ -675,6 +679,7 @@ mod tests {
use super::{Builder, FlatItem, Item};
use crate::{Asset, STAC_VERSION};
use geojson::{feature::Id, Feature};
use serde_json::json;

#[test]
fn new() {
Expand Down Expand Up @@ -869,4 +874,9 @@ mod tests {
let flat_item: FlatItem = serde_json::from_value(value).unwrap();
assert_eq!(flat_item.geometry, None);
}

#[test]
fn permissive_deserialization() {
let _: Item = serde_json::from_value(json!({})).unwrap();
}
}
8 changes: 7 additions & 1 deletion crates/core/src/item_collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub struct ItemCollection {
/// The list of [Items](Item).
///
/// The attribute is actually "features", but we rename to "items".
#[serde(rename = "features")]
#[serde(rename = "features", default)]
pub items: Vec<Item>,

/// List of link objects to resources and related URLs.
Expand Down Expand Up @@ -118,6 +118,7 @@ impl TryFrom<Value> for ItemCollection {
mod tests {
use super::ItemCollection;
use crate::Item;
use serde_json::json;

#[test]
fn item_collection_from_vec() {
Expand All @@ -130,4 +131,9 @@ mod tests {
let items = vec![Item::new("a"), Item::new("b")];
let _ = ItemCollection::from_iter(items.into_iter());
}

#[test]
fn permissive_deserialization() {
let _: ItemCollection = serde_json::from_value(json!({})).unwrap();
}
}

0 comments on commit 921f4d7

Please sign in to comment.