diff --git a/utoipa-gen/CHANGELOG.md b/utoipa-gen/CHANGELOG.md index dbd0f468..6e7dcc84 100644 --- a/utoipa-gen/CHANGELOG.md +++ b/utoipa-gen/CHANGELOG.md @@ -15,6 +15,10 @@ * Fix tagged enum with flatten fields (https://github.com/juhaku/utoipa/pull/1208) +### Changed + +* `Info::from_env()` sets `License::identifier` (https://github.com/juhaku/utoipa/pull/1233) + ## 5.2.0 - Nov 2024 ### Fixed diff --git a/utoipa-gen/src/openapi/info.rs b/utoipa-gen/src/openapi/info.rs index 76729550..aa908751 100644 --- a/utoipa-gen/src/openapi/info.rs +++ b/utoipa-gen/src/openapi/info.rs @@ -41,7 +41,15 @@ impl Info<'_> { Some(contact) } }); - let license = std::env::var("CARGO_PKG_LICENSE").ok().map(License::from); + let license = std::env::var("CARGO_PKG_LICENSE") + .ok() + .map(|spdx_expr| License { + name: Cow::Owned(spdx_expr.clone()), + // CARGO_PKG_LICENSE contains an SPDX expression as described in the Cargo Book. + // It can be set to `info.license.identifier`. + identifier: Cow::Owned(spdx_expr), + ..Default::default() + }); Info { title: name.map(|name| name.into()), @@ -427,4 +435,39 @@ mod tests { assert!(contact.name.is_none(), "Contact name should be empty"); assert!(contact.email.is_none(), "Contact email should be empty"); } + + #[test] + fn info_from_env() { + let info = Info::from_env(); + + match info.title { + Some(LitStrOrExpr::LitStr(title)) => assert_eq!(title.value(), env!("CARGO_PKG_NAME")), + _ => panic!(), + } + + match info.version { + Some(LitStrOrExpr::LitStr(version)) => { + assert_eq!(version.value(), env!("CARGO_PKG_VERSION")) + } + _ => panic!(), + } + + match info.description { + Some(LitStrOrExpr::LitStr(description)) => { + assert_eq!(description.value(), env!("CARGO_PKG_DESCRIPTION")) + } + _ => panic!(), + } + + assert!(matches!(info.terms_of_service, None)); + + match info.license { + Some(license) => { + assert_eq!(license.name, env!("CARGO_PKG_LICENSE")); + assert_eq!(license.identifier, env!("CARGO_PKG_LICENSE")); + assert_eq!(license.url, None); + } + None => panic!(), + } + } }