-
-
Notifications
You must be signed in to change notification settings - Fork 239
fix(releases): Serialize project IDs as integers #3068
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| //! This module contains some custom serialization logic for the API. | ||
| use std::sync::LazyLock; | ||
|
|
||
| use regex::Regex; | ||
| use serde::ser::SerializeSeq as _; | ||
| use serde::{Serialize, Serializer}; | ||
|
|
||
| /// A container for either a numeric ID or an alphanumeric slug. | ||
| /// | ||
| /// IDs are serialized as integers, while slugs are serialized as strings. | ||
| #[derive(Serialize)] | ||
| #[serde(untagged)] | ||
| enum IdSlug<'s> { | ||
| Id(i64), | ||
| Slug(&'s str), | ||
| } | ||
|
|
||
| /// Serializes a sequence of strings, which may contain either numeric IDs or alphanumeric slugs. | ||
| /// | ||
| /// We check each element in the sequence. If the element only contains digits and can be parsed as a 64-bit signed integer, | ||
| /// we consider the value to be an ID. Otherwise, we consider the value to be a slug. | ||
| /// | ||
| /// IDs are serialized as integers, while slugs are serialized as strings. | ||
| pub fn serialize_id_slug_list<I, S>(list: I, serializer: S) -> Result<S::Ok, S::Error> | ||
| where | ||
| I: IntoIterator, | ||
| I::Item: AsRef<str>, | ||
| S: Serializer, | ||
| { | ||
| let mut seq = serializer.serialize_seq(None)?; | ||
| for item in list { | ||
| let item = item.as_ref(); | ||
| let id_slug = IdSlug::from(&item); | ||
| seq.serialize_element(&id_slug)?; | ||
| } | ||
| seq.end() | ||
| } | ||
|
|
||
| impl<'a, S> From<&'a S> for IdSlug<'a> | ||
| where | ||
| S: AsRef<str>, | ||
| { | ||
| /// Convert from a string reference to an IdSlug. | ||
| /// | ||
| /// If the string contains only digits and can be parsed as a 64-bit signed integer, | ||
| /// we consider the value to be an ID. Otherwise, we consider the value to be a slug. | ||
| fn from(value: &'a S) -> Self { | ||
| /// Project ID regex | ||
| /// | ||
| /// Project IDs always contain only digits. | ||
| static PROJECT_ID_REGEX: LazyLock<Regex> = | ||
| LazyLock::new(|| Regex::new(r"^\d+$").expect("regex is valid")); | ||
|
|
||
| let value = value.as_ref(); | ||
|
|
||
| PROJECT_ID_REGEX | ||
| .is_match(value) | ||
| .then(|| value.parse().ok().map(IdSlug::Id)) | ||
szokeasaurusrex marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| .flatten() | ||
| .unwrap_or(IdSlug::Slug(value)) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
|
|
||
| /// A test struct which serializes with serialize_id_slug_list | ||
| #[derive(Serialize)] | ||
| struct IdSlugListSerializerTest<const N: usize> { | ||
| #[serde(serialize_with = "serialize_id_slug_list")] | ||
| value: [&'static str; N], | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_serialize_id_slug_list_empty() { | ||
| let to_serialize = IdSlugListSerializerTest { value: [] }; | ||
|
|
||
| let serialized = serde_json::to_string(&to_serialize).unwrap(); | ||
| let expected = serde_json::json!({ "value": [] }).to_string(); | ||
|
|
||
| assert_eq!(serialized, expected) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_serialize_id_slug_list_single_id() { | ||
| let to_serialize = IdSlugListSerializerTest { value: ["123"] }; | ||
|
|
||
| let serialized = serde_json::to_string(&to_serialize).unwrap(); | ||
| let expected = serde_json::json!({ "value": [123] }).to_string(); | ||
|
|
||
| assert_eq!(serialized, expected) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_serialize_id_slug_list_single_slug() { | ||
| let to_serialize = IdSlugListSerializerTest { value: ["abc"] }; | ||
|
|
||
| let serialized = serde_json::to_string(&to_serialize).unwrap(); | ||
| let expected = serde_json::json!({ "value": ["abc"] }).to_string(); | ||
|
|
||
| assert_eq!(serialized, expected) | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_serialize_id_slug_list_multiple_ids_and_slugs() { | ||
| let to_serialize = IdSlugListSerializerTest { | ||
| value: ["123", "abc", "456", "whatever"], | ||
| }; | ||
|
|
||
| let serialized = serde_json::to_string(&to_serialize).unwrap(); | ||
| let expected = serde_json::json!({ "value": [123, "abc", 456, "whatever"] }).to_string(); | ||
|
|
||
| assert_eq!(serialized, expected) | ||
| } | ||
|
|
||
| /// Slugs of "-0" are possible. This test ensures that we serialize "-0" as a slug, | ||
| /// rather than as an ID 0. | ||
| #[test] | ||
| fn test_serialize_id_slug_minus_zero_edge_case() { | ||
| let to_serialize = IdSlugListSerializerTest { value: ["-0"] }; | ||
|
|
||
| let serialized = serde_json::to_string(&to_serialize).unwrap(); | ||
| let expected = serde_json::json!({ "value": ["-0"] }).to_string(); | ||
|
|
||
| assert_eq!(serialized, expected) | ||
| } | ||
| } | ||
6 changes: 6 additions & 0 deletions
6
tests/integration/_cases/releases/releases-new-mixed-projects.trycmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ``` | ||
| $ sentry-cli releases new -p 123 -p my-project -p 456 test-release | ||
| ? success | ||
| Created release test-release | ||
|
|
||
| ``` |
6 changes: 6 additions & 0 deletions
6
tests/integration/_cases/releases/releases-new-multiple-numeric-projects.trycmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ``` | ||
| $ sentry-cli releases new -p 123 -p 456 test-release | ||
| ? success | ||
| Created release test-release | ||
|
|
||
| ``` |
6 changes: 6 additions & 0 deletions
6
tests/integration/_cases/releases/releases-new-numeric-project.trycmd
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| ``` | ||
| $ sentry-cli releases new -p 123 test-release | ||
| ? success | ||
| Created release test-release | ||
|
|
||
| ``` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.