From 103d38375d57fdd99a797e1fdb3801d4f58711c8 Mon Sep 17 00:00:00 2001 From: Jessica Black Date: Fri, 18 Oct 2024 18:06:59 -0700 Subject: [PATCH] Update with new schema format --- src/locator.rs | 48 +++++++++++++++++++++--------------------- src/locator_package.rs | 46 +++++++++++++++++++--------------------- src/locator_strict.rs | 47 ++++++++++++++++++++--------------------- 3 files changed, 69 insertions(+), 72 deletions(-) diff --git a/src/locator.rs b/src/locator.rs index de60089..d40ab9d 100644 --- a/src/locator.rs +++ b/src/locator.rs @@ -6,11 +6,7 @@ use getset::{CopyGetters, Getters}; use lazy_static::lazy_static; use regex::Regex; use serde::{Deserialize, Serialize}; -use serde_json::json; -use utoipa::{ - openapi::{ObjectBuilder, SchemaType}, - ToSchema, -}; +use utoipa::ToSchema; use crate::{ parse_org_package, Error, Fetcher, OrgId, Package, PackageLocator, ParseError, Revision, @@ -115,11 +111,30 @@ macro_rules! locator { /// /// This parse function is based on the function used in FOSSA Core for maximal compatibility. #[derive( - Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented, + Clone, + Eq, + PartialEq, + Ord, + PartialOrd, + Hash, + Debug, + Builder, + Getters, + CopyGetters, + Documented, + ToSchema, +)] +#[schema( + examples( + json!("git+github.com/fossas/example$1234"), + json!("npm+lodash$1.0.0"), + json!("npm+lodash"), + json!("mvn+1234/org.custom.mylib:mylib$1.0.0:jar")), )] pub struct Locator { /// Determines which fetcher is used to download this package. #[getset(get_copy = "pub")] + #[schema(ignore)] fetcher: Fetcher, /// Specifies the organization ID to which this package is namespaced. @@ -142,6 +157,7 @@ pub struct Locator { /// - A private NPM package that is hosted on NPM but requires credentials is namespaced. #[builder(into)] #[getset(get_copy = "pub")] + #[schema(ignore)] org_id: Option, /// Specifies the unique identifier for the package by fetcher. @@ -150,6 +166,7 @@ pub struct Locator { /// uses a value in the form of `{user_name}/{package_name}`. #[builder(into)] #[getset(get = "pub")] + #[schema(ignore)] package: Package, /// Specifies the version for the package by fetcher. @@ -159,6 +176,7 @@ pub struct Locator { /// and the fetcher disambiguates. #[builder(into)] #[getset(get = "pub")] + #[schema(ignore)] revision: Option, } @@ -316,24 +334,6 @@ impl Serialize for Locator { } } -impl<'a> ToSchema<'a> for Locator { - fn schema() -> ( - &'a str, - utoipa::openapi::RefOr, - ) { - ( - "Locator", - ObjectBuilder::new() - .description(Some(Self::DOCS)) - .example(Some(json!("git+github.com/fossas/example$1234"))) - .min_length(Some(3)) - .schema_type(SchemaType::String) - .build() - .into(), - ) - } -} - impl From for Locator { fn from(package: PackageLocator) -> Self { let (fetcher, org_id, package) = package.explode(); diff --git a/src/locator_package.rs b/src/locator_package.rs index 19ab4c6..72decdd 100644 --- a/src/locator_package.rs +++ b/src/locator_package.rs @@ -4,11 +4,7 @@ use bon::Builder; use documented::Documented; use getset::{CopyGetters, Getters}; use serde::{Deserialize, Serialize}; -use serde_json::json; -use utoipa::{ - openapi::{ObjectBuilder, SchemaType}, - ToSchema, -}; +use utoipa::ToSchema; use crate::{Error, Fetcher, Locator, OrgId, Package, StrictLocator}; @@ -72,11 +68,29 @@ macro_rules! package { /// /// This implementation ignores the `revision` segment if it exists. If this is not preferred, use [`Locator`] instead. #[derive( - Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented, + Clone, + Eq, + PartialEq, + Ord, + PartialOrd, + Hash, + Debug, + Builder, + Getters, + CopyGetters, + Documented, + ToSchema, +)] +#[schema( + examples( + json!("git+github.com/fossas/example"), + json!("npm+lodash"), + json!("mvn+1234/org.custom.mylib:mylib")), )] pub struct PackageLocator { /// Determines which fetcher is used to download this package. #[getset(get_copy = "pub")] + #[schema(ignore)] fetcher: Fetcher, /// Specifies the organization ID to which this package is namespaced. @@ -99,6 +113,7 @@ pub struct PackageLocator { /// - A private NPM package that is hosted on NPM but requires credentials is namespaced. #[builder(into)] #[getset(get_copy = "pub")] + #[schema(ignore)] org_id: Option, /// Specifies the unique identifier for the package by fetcher. @@ -107,6 +122,7 @@ pub struct PackageLocator { /// uses a value in the form of `{user_name}/{package_name}`. #[builder(into)] #[getset(get = "pub")] + #[schema(ignore)] package: Package, } @@ -169,24 +185,6 @@ impl<'de> Deserialize<'de> for PackageLocator { } } -impl<'a> ToSchema<'a> for PackageLocator { - fn schema() -> ( - &'a str, - utoipa::openapi::RefOr, - ) { - ( - "PackageLocator", - ObjectBuilder::new() - .description(Some(Self::DOCS)) - .example(Some(json!("git+github.com/fossas/example"))) - .min_length(Some(3)) - .schema_type(SchemaType::String) - .build() - .into(), - ) - } -} - impl Serialize for PackageLocator { fn serialize(&self, serializer: S) -> Result where diff --git a/src/locator_strict.rs b/src/locator_strict.rs index d1c5f7c..c27d60a 100644 --- a/src/locator_strict.rs +++ b/src/locator_strict.rs @@ -4,11 +4,7 @@ use bon::Builder; use documented::Documented; use getset::{CopyGetters, Getters}; use serde::{Deserialize, Serialize}; -use serde_json::json; -use utoipa::{ - openapi::{ObjectBuilder, SchemaType}, - ToSchema, -}; +use utoipa::ToSchema; use crate::{Error, Fetcher, Locator, OrgId, Package, PackageLocator, ParseError, Revision}; @@ -72,11 +68,29 @@ macro_rules! strict { /// {fetcher}+{org_id}/{package}${revision} /// ``` #[derive( - Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug, Builder, Getters, CopyGetters, Documented, + Clone, + Eq, + PartialEq, + Ord, + PartialOrd, + Hash, + Debug, + Builder, + Getters, + CopyGetters, + Documented, + ToSchema, +)] +#[schema( + examples( + json!("git+github.com/fossas/example$1234"), + json!("npm+lodash$1.0.0"), + json!("mvn+1234/org.custom.mylib:mylib$1.0.0:jar")), )] pub struct StrictLocator { /// Determines which fetcher is used to download this package. #[getset(get_copy = "pub")] + #[schema(ignore)] fetcher: Fetcher, /// Specifies the organization ID to which this package is namespaced. @@ -99,6 +113,7 @@ pub struct StrictLocator { /// - A private NPM package that is hosted on NPM but requires credentials is namespaced. #[builder(into)] #[getset(get_copy = "pub")] + #[schema(ignore)] org_id: Option, /// Specifies the unique identifier for the package by fetcher. @@ -107,6 +122,7 @@ pub struct StrictLocator { /// uses a value in the form of `{user_name}/{package_name}`. #[builder(into)] #[getset(get = "pub")] + #[schema(ignore)] package: Package, /// Specifies the version for the package by fetcher. @@ -116,6 +132,7 @@ pub struct StrictLocator { /// and the fetcher disambiguates. #[builder(into)] #[getset(get = "pub")] + #[schema(ignore)] revision: Revision, } @@ -195,24 +212,6 @@ impl Serialize for StrictLocator { } } -impl<'a> ToSchema<'a> for StrictLocator { - fn schema() -> ( - &'a str, - utoipa::openapi::RefOr, - ) { - ( - "StrictLocator", - ObjectBuilder::new() - .description(Some(Self::DOCS)) - .example(Some(json!("git+github.com/fossas/example$1234"))) - .min_length(Some(3)) - .schema_type(SchemaType::String) - .build() - .into(), - ) - } -} - impl AsRef for StrictLocator { fn as_ref(&self) -> &StrictLocator { self