Skip to content

Commit

Permalink
fix: return null instead of an empty string when data isn't included …
Browse files Browse the repository at this point in the history
…for event response
  • Loading branch information
dav1do committed Jun 27, 2024
1 parent 3a9dec0 commit 673719f
Show file tree
Hide file tree
Showing 8 changed files with 26 additions and 31 deletions.
2 changes: 1 addition & 1 deletion api-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To see how to make this your own, look here:
[README]((https://openapi-generator.tech))

- API version: 0.25.0
- Build date: 2024-06-26T12:32:38.465059-06:00[America/Denver]
- Build date: 2024-06-26T12:41:21.980959-06:00[America/Denver]



Expand Down
1 change: 0 additions & 1 deletion api-server/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,6 @@ components:
description: Multibase encoding of event data.
type: string
required:
- data
- id
title: A Ceramic Event
type: object
Expand Down
2 changes: 1 addition & 1 deletion api-server/docs/Event.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**id** | **String** | Multibase encoding of event root CID bytes. |
**data** | **String** | Multibase encoding of event data. |
**data** | **String** | Multibase encoding of event data. | [optional] [default to None]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
18 changes: 8 additions & 10 deletions api-server/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -287,13 +287,14 @@ pub struct Event {

/// Multibase encoding of event data.
#[serde(rename = "data")]
pub data: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub data: Option<String>,
}

impl Event {
#[allow(clippy::new_without_default)]
pub fn new(id: String, data: String) -> Event {
Event { id, data }
pub fn new(id: String) -> Event {
Event { id, data: None }
}
}

Expand All @@ -305,8 +306,9 @@ impl std::string::ToString for Event {
let params: Vec<Option<String>> = vec![
Some("id".to_string()),
Some(self.id.to_string()),
Some("data".to_string()),
Some(self.data.to_string()),
self.data
.as_ref()
.map(|data| ["data".to_string(), data.to_string()].join(",")),
];

params.into_iter().flatten().collect::<Vec<_>>().join(",")
Expand Down Expand Up @@ -374,11 +376,7 @@ impl std::str::FromStr for Event {
.into_iter()
.next()
.ok_or_else(|| "id missing in Event".to_string())?,
data: intermediate_rep
.data
.into_iter()
.next()
.ok_or_else(|| "data missing in Event".to_string())?,
data: intermediate_rep.data.into_iter().next(),
})
}
}
Expand Down
1 change: 0 additions & 1 deletion api/ceramic.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -402,7 +402,6 @@ components:
type: object
required:
- id
- data
properties:
id:
type: string
Expand Down
23 changes: 11 additions & 12 deletions api/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,13 @@ const INSERT_REQUEST_TIMEOUT: std::time::Duration = std::time::Duration::from_se
// Helper to build responses consistent as we can't implement for the api_server::models directly
pub struct BuildResponse {}
impl BuildResponse {
pub fn event(id: Cid, data: Vec<u8>) -> models::Event {
pub fn event(id: Cid, data: Option<Vec<u8>>) -> models::Event {
let id = id.to_string();
let data = if data.is_empty() {
String::default()
} else {
multibase::encode(multibase::Base::Base64, data)
};
models::Event::new(id, data)
let mut res = models::Event::new(id);
if data.as_ref().map_or(false, |e| !e.is_empty()) {
res.data = Some(multibase::encode(multibase::Base::Base64, data.unwrap()));
}
res
}
}

Expand Down Expand Up @@ -196,12 +195,12 @@ impl EventInsertResult {
pub struct EventDataResult {
/// The CID of the event
pub id: ceramic_core::Cid,
/// The data as a car file
pub data: Vec<u8>,
/// The data as a car file. Can be none if not requested.
pub data: Option<Vec<u8>>,
}

impl EventDataResult {
pub fn new(id: ceramic_core::Cid, data: Vec<u8>) -> Self {
pub fn new(id: ceramic_core::Cid, data: Option<Vec<u8>>) -> Self {
Self { id, data }
}
}
Expand Down Expand Up @@ -548,7 +547,7 @@ where
.await
.map_err(|err| ErrorResponse::new(format!("failed to get keys: {err}")))?
.into_iter()
.map(|(id, data)| BuildResponse::event(id, data))
.map(|(id, data)| BuildResponse::event(id, Some(data)))
.collect::<Vec<_>>();

let event_cnt = events.len();
Expand Down Expand Up @@ -671,7 +670,7 @@ where
};
match data {
Ok(Some(data)) => {
let event = BuildResponse::event(cid, data);
let event = BuildResponse::event(cid, Some(data));
Ok(EventsEventIdGetResponse::Success(event))
}
Ok(None) => Ok(EventsEventIdGetResponse::EventNotFound(format!(
Expand Down
6 changes: 3 additions & 3 deletions api/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,7 @@ async fn get_events_for_interest_range() {
r: Success(EventsGet { events: [Event { id: "fce0105ff012616e0f0c1e987ef0f772afbe2c7f05c50102bc800", data: "" }], resume_offset: 1, is_complete: false })
*/
let mock_interest = MockAccessInterestStoreTest::new();
let expected = BuildResponse::event(cid, vec![]);
let expected = BuildResponse::event(cid, None);
let mut mock_event_store = MockEventStoreTest::new();
mock_event_store
.expect_range_with_values()
Expand Down Expand Up @@ -589,7 +589,7 @@ async fn test_events_event_id_get_by_event_id_success() {
event.id,
multibase::encode(multibase::Base::Base32Lower, event_cid.to_bytes())
);
assert_eq!(event.data, event_data_base64);
assert_eq!(event.data.unwrap(), event_data_base64);
}

#[test(tokio::test)]
Expand Down Expand Up @@ -619,5 +619,5 @@ async fn test_events_event_id_get_by_cid_success() {
event.id,
multibase::encode(multibase::Base::Base32Lower, event_cid.to_bytes())
);
assert_eq!(event.data, event_data_base64);
assert_eq!(event.data.unwrap(), event_data_base64);
}
4 changes: 2 additions & 2 deletions service/src/event/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ impl ceramic_api::EventStore for CeramicEventService {
CeramicOneEvent::new_events_since_value(&self.pool, highwater, limit).await?;
let res = cids
.into_iter()
.map(|cid| ceramic_api::EventDataResult::new(cid, vec![]))
.map(|cid| ceramic_api::EventDataResult::new(cid, None))
.collect();
(hw, res)
}
Expand All @@ -174,7 +174,7 @@ impl ceramic_api::EventStore for CeramicEventService {
for (cid, value) in data {
res.push(ceramic_api::EventDataResult::new(
cid,
value.encode_car().await?,
Some(value.encode_car().await?),
));
}
(hw, res)
Expand Down

0 comments on commit 673719f

Please sign in to comment.